// // 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