// // main.m // Bus-公交 /* 公交一卡通充值系統,有兩種功能選擇,第一種投入硬幣或者紙幣 選擇購票,第二種一卡通充值,充值面額是20,50,100 */ #import#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#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#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
#importenum 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