OC----简单的购物系统----

今天下午OC上机考试,虽然考试的时候没写完, 但是课下写完了.

main.m

#import <Foundation/Foundation.h>
#import "Shops.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool { Shops *shop = [Shops shopsWithPen: cup: rubber:]; // 初始化商店
[shop disPlayAllGoods];
Person *per = [Person personWithShop:shop];
[per pay:]; // 用户付款 }
return ;
}

Shops.h

#import <Foundation/Foundation.h>

@interface Shops : NSObject

@property (nonatomic, assign)NSInteger pen;
@property (nonatomic, assign)NSInteger cup;
@property (nonatomic, assign)NSInteger rubber;
@property (nonatomic, assign)NSInteger code;
@property (nonatomic, assign)NSInteger number;
@property (nonatomic, assign)NSInteger count; - (instancetype)initWithPen:(NSInteger)pen
cup:(NSInteger)cup
rubber:(NSInteger)rubber;
+ (instancetype)shopsWithPen:(NSInteger)pen
cup:(NSInteger)cup
rubber:(NSInteger)rubber; - (void)disPlayAllGoods;
- (void)inputCode;
- (void)showAllBuyGoods;
- (void)deleteGoods;
- (void)buyCode:(NSInteger)code;
- (void)isGoOn:(NSInteger)number;
@end

Shops.m

//
// Shops.m
// 26- 马峰
//
// Created by dllo on 16/3/2.
// Copyright © 2016年 dllo. All rights reserved.
// #import "Shops.h" @implementation Shops
/** 自定义初始化 */
- (instancetype)initWithPen:(NSInteger)pen cup:(NSInteger)cup rubber:(NSInteger)rubber{
self = [super init];
if (self) {
_cup = cup;
_rubber = rubber;
_pen = pen;
}
return self;
}
/** 便利构造器 */
+ (instancetype)shopsWithPen:(NSInteger)pen cup:(NSInteger)cup rubber:(NSInteger)rubber{
Shops *shop = [[Shops alloc]initWithPen:pen cup:cup rubber:rubber];
return shop;
} /** 展示所有的商品 */
- (void)disPlayAllGoods{ NSLog(@"欢迎来到1+1自助超市, 我们这里有以下商品:\n1.笔\n2.水杯\n3.橡皮\n4.退出\n请输入对应的编号选择购买.");
[self inputCode];
}
/** 输入购买商品的编号 */
- (void)inputCode{
scanf("%ld", &_code);
/** 判断是否输入有误 */
if (_code > || _code < ) {
_count++;
if (_count == ) {
NSLog(@"错误三次,程序退出");
}else{
NSLog(@"输入错误,请重新输入:");
[self inputCode];
}
}
[self buyCode:_code];
}
/** 显示已经购买的商品 */
- (void)showAllBuyGoods{
NSLog(@"你选择了%ld只笔, %ld个水杯, %ld个橡皮擦, 一共%ld元.还需要其他的吗. 需要请输入1, 不需要输入2",_pen, _cup, _rubber, _rubber * + _pen * + _cup * );
scanf("%ld", &_number );
/** 判断是否输入有误 */
if (_number > || _number < ) {
_count++;
if (_count == ) {
NSLog(@"错误三次,程序退出");
}else{
NSLog(@"输入错误,请重新输入:");
[self showAllBuyGoods];
}
}
[self isGoOn:_number]; }
/** 删除货物 */
- (void)deleteGoods{ NSLog(@"请选择要删除的货物\n1.笔\n2.水杯\n3.橡皮擦");
scanf("%ld", &_number);
/** 判断是否输入有误 */
if (_number > || _number < ) {
_count++;
if (_count == ) {
NSLog(@"错误三次,程序退出");
}else{
NSLog(@"输入错误,请重新输入:");
[self deleteGoods];
}
}
/** 判断是哪一种货物 */
switch (_number) {
case :
NSLog(@"请输入数量:");
scanf("%ld", &_number);
_pen -= _number;
[self showAllBuyGoods];
break;
case :
NSLog(@"请输入数量:");
scanf("%ld", &_number);
_cup -= _number;
[self showAllBuyGoods]; break;
case :
NSLog(@"请输入数量:");
scanf("%ld", &_number);
_rubber -= _number;
[self showAllBuyGoods];
break;
default: break;
} } /** 根据用户输入判断是否买完 */
- (void)isGoOn:(NSInteger)number{
switch (number) {
case :
[self disPlayAllGoods];
break;
case :
NSLog(@"请付款");
default:
break;
}
}
/** 根据用户输入判断用户要买哪一种商品 */
- (void)buyCode:(NSInteger)code{
switch (code) {
case :
NSLog(@"请输入数量:");
scanf("%ld", &_number);
_pen += _number;
[self showAllBuyGoods];
break;
case :
NSLog(@"请输入数量:");
scanf("%ld", &_number);
_cup += _number;
[self showAllBuyGoods];
break;
case :
NSLog(@"请输入数量:");
scanf("%ld", &_number);
_rubber += _number;
[self showAllBuyGoods];
break;
case :
NSLog(@"程序已经退出!");
break;
default: break;
}
}
@end

Person.h

#import <Foundation/Foundation.h>
#import "Shops.h" @interface Person : NSObject @property (nonatomic, retain)Shops *shop; - (instancetype)initWithShop:(Shops *)shop; + (instancetype)personWithShop:(Shops *)shop; - (void)pay:(NSInteger)money; @end

Person.m

//
// Person.m
// 26- 马峰
//
// Created by dllo on 16/3/2.
// Copyright © 2016年 dllo. All rights reserved.
// #import "Person.h" @implementation Person
/** 自 定 义 初 始 化 */
- (instancetype)initWithShop:(Shops *)shop{
self = [super init];
if (self) {
_shop = shop;
}
return self;
}
/** 便 利 构 造 器 */
+ (instancetype)personWithShop:(Shops *)shop{
Person *per = [[Person alloc]initWithShop:shop];
return per;
} /** 付 款 函 数 */
- (void)pay:(NSInteger)money{ if ((_shop.cup * + _shop.rubber * + _shop.pen * ) > money) {
NSLog(@"你买的东西超过了100块钱!!");
[_shop deleteGoods];
[self pay:money]; }
else{
NSLog(@"付款成功!谢谢惠顾!!");
} } @end
上一篇:类库探源——System.ValueType


下一篇:C# WinForm实现任务栏程序图标闪烁