超市购物系统

来源:互联网 发布:淘宝评价管理二维码 编辑:程序博客网 时间:2024/06/12 01:38
#import <Foundation/Foundation.h>

@interface Shops : NSObject

+ (id)defaultsShop;

- (void)disPlayAllGoods;

- (void)showAllBuyGoods;

- (void)deleteGoods;

- (int)accounts;

- (int)calculatePrice;

@end


#import "Shops.h"
//#import "Goods.h"

@interface Shops ()

@property (nonatomic) int penCount;
@property (nonatomic) int cupCount;
@property (nonatomic) int eraserCount;
@property (nonatomic) int sum;

@end

@implementation Shops

static Shops *s = nil;
+ (id)defaultsShop {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
s=[[Shops alloc]init];
});
return s;
}

- (id)init {
if (self = [super init]) {
self.penCount = 0;
self.cupCount = 0;
self.eraserCount = 0;
self.sum = 0;
}
return self;
}

- (void)disPlayAllGoods {
NSLog(@"欢迎来到1+1自助超市,我们这里有一下商品");
NSLog(@"1.笔 ");
NSLog(@"2.水杯");
NSLog(@"3.橡皮");
NSLog(@"4.退出");
NSLog(@"请输入对应的编号选择购买");
int inputNum=0;
int a = 1;
int errCount = 0;
while (a) {
scanf("%d",&inputNum);
if (inputNum!=1&&inputNum!=2&&inputNum!=3&&inputNum!=4) {
errCount++;
if (errCount==3) {
NSLog(@"你输入错误次数大于三次,谢谢光顾");
break;
}
NSLog(@"输入错误请从新输入");

} else {
switch (inputNum) {
case 1:
{
[self buyPen];
a=0;
break;
}
case 2:
{
[self buyCup];
a=0;
break;
}
case 3:
{
[self buyEraser];
a=0;
break;
}

default:
break;
}
}
}
}

- (void)buyPen {
NSLog(@"每只笔5元,请输入你想买几只笔");
int penC = 0;
scanf("%d",&penC);
self.penCount = self.penCount + penC;
self.sum = self.sum+penC*5;

NSLog(@"你选择了%d只笔,%d个水杯,%d个橡皮擦,一共%d元。还需要其他的嘛,如果需要请输入Y,不需要输入N",self.penCount,self.cupCount,self.eraserCount, self.sum);
char s = '0';
scanf("%c%c",&s,&s);

if (s == 'Y'||s == 'y') {
[self disPlayAllGoods];
} else {
NSLog(@"谢谢你的光顾,请去结账!");
[self accounts];
}
}

-(void)buyCup {
NSLog(@"每个水杯4元,请输入你想买几个水杯");
int cupC = 0;
scanf("%d",&cupC);
self.cupCount = self.cupCount + cupC;
self.sum = self.sum+cupC*4;

NSLog(@"你选择了%d只笔,%d个水杯,%d个橡皮擦,一共%d元。还需要其他的嘛,如果需要请输入Y,不需要输入N",self.penCount,self.cupCount,self.eraserCount, self.sum);

char s = '0';
scanf("%c%c",&s,&s);
if (s == 'Y'||s == 'y') {
[self disPlayAllGoods];
} else {
[self accounts];
}
}

- (void)buyEraser {
NSLog(@"每个橡皮擦3元,请输入你想买几个橡皮擦");
int eraserC = 0;
scanf("%d",&eraserC);
self.eraserCount = self.eraserCount + eraserC;
self.sum = self.sum+eraserC*3;
NSLog(@"你选择了%d只笔,%d个水杯,%d个橡皮擦,一共%d元。还需要其他的嘛,如果需要请输入Y,不需要输入N",self.penCount,self.cupCount,self.eraserCount, self.sum);

char s = '0';
scanf("%c%c",&s,&s);
if (s == 'Y'||s == 'y'){
[self disPlayAllGoods];
} else {
NSLog(@"谢谢你的光顾,请去结账!");
[self accounts];
}
}

- (void)deleteGoods {
NSLog(@"请选择删除货物");
NSLog(@"1.笔");
NSLog(@"2.水杯");
NSLog(@"3.橡皮擦");
int inputNum = 0;
scanf("%d",&inputNum);
int a = 1;
while (a){
switch (inputNum) {
case 1:
{
[self deletePen];
a=0;
break;
}
case 2:
{
[self deleteCup];
a=0;
break;
}
case 3:
{
[self deleteEraser];
a=0;
break;
}

default:
{
[self deleteGoods];
}
break;
}
}
}

- (int)accounts {
[self showAllBuyGoods];
return self.sum;
}

- (void)showAllBuyGoods {
NSLog(@"你一共买了%d个笔,%d个水杯,%d个橡皮,一共%d元",self.penCount,self.cupCount,self.eraserCount,[self calculatePrice]);
}

- (int)calculatePrice {
self.sum = self.penCount*5+self.cupCount*4+self.eraserCount*3;
return self.sum;
}

- (void)deletePen {
NSLog(@"你当前已经买了%d只笔,请问你想删除几只",self.penCount);
int penC = 0;
scanf("%d",&penC);
if (penC>self.penCount) {
NSLog(@"你输入的数字大于购买的数量,请重新输入");
[self deleteGoods];
} else {
self.penCount = self.penCount - penC;
[self showAllBuyGoods];
}
}

- (void)deleteCup {
NSLog(@"你当前已经买了%d个水杯,请问你想删除几个",self.cupCount);
int cupC = 0;
scanf("%d",&cupC);
if (cupC>self.cupCount) {
NSLog(@"你输入的数字大于购买的数量,请重新输入");
[self deleteGoods];
} else {
self.cupCount = self.cupCount - cupC;
[self showAllBuyGoods];
}
}

- (void)deleteEraser {
NSLog(@"你当前已经买了%d个橡皮,请问你想删除几个",self.eraserCount);
int eraC = 0;
scanf("%d",&eraC);
if (eraC>self.eraserCount) {
NSLog(@"你输入的数字大于购买的数量,请重新输入");
[self deleteGoods];
} else {
self.eraserCount = self.eraserCount - eraC;
[self showAllBuyGoods];
}
}

@end




#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(nonatomic)int money;

- (void)pay:(int)money;
- (void)deleteGoods;

@end

#import "Person.h"
#import "Shops.h"

@implementation Person

- (id)init {
if (self=[super init]) {
self.money = 100;
}
return self;
}

- (void)pay:(int)money {
int a = 1;
while (a) {
if (money<=100) {
NSLog(@"付款成功");
a=0;
}
else {
NSLog(@"金钱不够,去购物车删除一些货物");
[self deleteGoods];
int price = [[Shops defaultsShop] calculatePrice];
if (price > 100) {
[self pay:price];
}
else {
NSLog(@"付款成功");
a=0;
}
}
}
}

- (void)deleteGoods {
[[Shops defaultsShop] deleteGoods];
}

@end#import <Foundation/Foundation.h>
#import "Shops.h"
#import "Person.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

Shops *shop = [Shops defaultsShop];

[shop disPlayAllGoods];
int money =[shop accounts];

Person *p = [[Person alloc] init];
[p pay:money];

}
return 0;
}

1 0