#import <Foundation/Foundation.h>
@interface Account : NSObject
{
int _balance;
}
-(void)setBalance:(int) balance;
-(void)getMoneyOfInput:(int)money;
-(void)printAccount;
@end
@implementation Account
-(void)setBalance:(int) balance{
if (balance>0) {
_balance += balance;
}else{
NSLog(@"error");
}
}
-(void)getMoneyOfInput:(int)money{
if (money>_balance) {
NSLog(@"账户余额不足!");
}else{
_balance = _balance-money;
}
}
-(void)printAccount{
NSLog(@"账户余额为%d",_balance);
}
@end
@interface Bank : NSObject
{
NSString *_name;
Account *_account1;
Account *_account2;
}
-(void)setName:(NSString *)name;
-(void)setAccount1:(Account *)account1;
-(void)setAccount2:(Account *)account2;
-(void)printName;
-(void)printAllAccount;
@end
@implementation Bank
-(void)setName:(NSString *)name{
_name = name;
}
-(void)setAccount1:(Account *)account1{
_account1 = account1;
}
-(void)setAccount2:(Account *)account2{
_account2 = account2;
}
-(void)printName{
NSLog(@"银行名称为%@",_name);
}
-(void)printAllAccount{
[_account1 printAccount];
[_account2 printAccount];
}
@end
int main(){
Account *a1 = [Account new];
Account *a2 = [Account new];
Bank *b = [Bank new];
[b setName:@"渣打银行"];
[a1 setBalance:1000];
[a2 setBalance:2000];
[a1 printAccount];
[a2 printAccount];
[b printName];
[b setAccount1:a1];
[b setAccount1:a2];
[a1 getMoneyOfInput:90];
[a2 getMoneyOfInput:201];
[b printAllAccount];
} |
|