首页 > 代码库 > ios开发之公交卡系统的设计与实现
ios开发之公交卡系统的设计与实现
// // main.m // Bus-公交 /* 公交一卡通充值系统。有两种功能选择。第一种投入硬币或者纸币 选择购票,另外一种一卡通充值,充值面额是20,50。100 */ #import <Foundation/Foundation.h> #import "Card.h" #import "ChargeMachine.h" #import "Money.h" int main(int argc, const char * argv[]) { ChargeMachine * machine = [[ChargeMachine alloc]init]; //[machine sellTicket]; //[ChargeMachine recharge:KindOfMoney100];//这里赋值的时候赋值成为100,导致程序出错 //[ChargeMachine recharge:KindOfMoney20]; [ChargeMachine recharge:KindOfMoney50]; [ChargeMachine recharge:KindOfMoneyCoinMoney2]; //[machine recharge:KindOfMoneyCoinMoney2]; NSLog(@"%@",machine); return 0; }
#import <Foundation/Foundation.h> #import "Money.h" @interface Card : NSObject { /*公交卡卡编号*/ NSString * _numberOfCard; /**公交卡存放金额*/ int MoneyOfCard; } @property NSString * numberOfCard; @property int MoneyOfCard; @end
#import "Card.h" @implementation Card @synthesize numberOfCard = _numberOfCard ; @synthesize MoneyOfCard = _MoneyOfCard; @end
#import <Foundation/Foundation.h> #import "Money.h" @interface ChargeMachine : NSObject { /**用来记录销售总量*/ int countOfMoney; KindOfMoney _money; } @property int countOfMoney; /**打印票据*/ -(void)sellTicket; /**充值*/ +(int)recharge:(KindOfMoney)kindOfMoney; -(int)recordcounOfMony; @end
#import "ChargeMachine.h" @implementation ChargeMachine @synthesize countOfMoney = _countOfMoney; /**打印票据*/ -(void)sellTicket { NSLog(@"卖出一张车票"); } /**充值*/ -(void)recharge { NSLog(@"充值成功"); } -(int)recordcounOfMony { _countOfMoney += _money; return _countOfMoney; } +(int)recharge:(KindOfMoney)kindOfMoney { ChargeMachine * machine = [[ChargeMachine alloc]init]; int moneyKind = 0; //int countOfMoney = 0; switch (kindOfMoney) { case KindOfMoney20: moneyKind = 20; //countOfMoney += money; NSLog(@"您充值的金额是20元"); break; case KindOfMoney50: moneyKind = 50; NSLog(@"您充值的金额是50元"); //countOfMoney += money; break; case KindOfMoney100: moneyKind = 100; NSLog(@"您充值的金额是100元"); //countOfMoney += money; break; case KindOfMoneyCoinMoney2: moneyKind = 2; NSLog(@"您投的是硬币"); [machine sellTicket]; break; case KindOfMoneyPaperMoney2: moneyKind = 2; NSLog(@"您投的是纸币"); [machine sellTicket]; break; default: break; } return moneyKind; } -(NSString *)description { //经过多次调用如何保存一个变量的值? //ChargeMachine * machine = [[ChargeMachine alloc]init]; //int moneyKind = [ChargeMachine recharge:_money]; //int countOf_Money = [ChargeMachine recharge:_money]; return [NSString stringWithFormat:@"%d",_money]; } @end
#import <Foundation/Foundation.h> enum KindOfMoney { /**用来存储充值面额为20的变量*/ KindOfMoney20, /**用来存储充值面额为50的变量*/ KindOfMoney50, /**用来存储充值面额为100的变量*/ KindOfMoney100, KindOfMoneyCoinMoney2, KindOfMoneyPaperMoney2 }; typedef enum KindOfMoney KindOfMoney; @interface Money : NSObject { KindOfMoney _kindOfMoney; } -(Money*)initWithKindOfMoney:(KindOfMoney)kindOfMoney; typedef enum KindOfMoney KindOfMoney; @end
#import "Money.h" @implementation Money { } -(Money*)initWithKindOfMoney:(KindOfMoney)kindOfMoney { self = [super init]; if (self!=nil) { _kindOfMoney = kindOfMoney; } return self; } @end
ios开发之公交卡系统的设计与实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。