標籤:objective-c ios開發 公交卡 系統 mac
//// 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開發之公交卡系統的設計與實現