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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:0 cup:0 rubber:0];  // 初始化商店
        [shop disPlayAllGoods];
        Person *per = [Person personWithShop:shop];
        [per pay:100];        //    用户付款
   
    }
    return 0;
}

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 > 4 || _code < 1) {
        _count++;
        if (_count == 3) {
            NSLog(@"错误三次,程序退出");
        }else{
            NSLog(@"输入错误,请重新输入:");
            [self inputCode];
        }
    }
    [self buyCode:_code];
}
/**       显示已经购买的商品          */
- (void)showAllBuyGoods{
    NSLog(@"你选择了%ld只笔, %ld个水杯, %ld个橡皮擦, 一共%ld元.还需要其他的吗. 需要请输入1, 不需要输入2",_pen, _cup, _rubber, _rubber * 2 + _pen * 3 + _cup * 5);
    scanf("%ld", &_number );
/**        判断是否输入有误         */
    if (_number > 2 || _number < 1) {
        _count++;
        if (_count == 3) {
            NSLog(@"错误三次,程序退出");
        }else{
            NSLog(@"输入错误,请重新输入:");
            [self showAllBuyGoods];
        }
    }
    [self isGoOn:_number];
    
}
/**          删除货物             */
- (void)deleteGoods{
    
    NSLog(@"请选择要删除的货物\n1.笔\n2.水杯\n3.橡皮擦");
    scanf("%ld", &_number);
    /** 判断是否输入有误 */
    if (_number > 4 || _number < 1) {
        _count++;
        if (_count == 3) {
            NSLog(@"错误三次,程序退出");
        }else{
            NSLog(@"输入错误,请重新输入:");
            [self deleteGoods];
        }
    }
    /**   判断是哪一种货物  */
    switch (_number) {
        case 1:
            NSLog(@"请输入数量:");
            scanf("%ld", &_number);
            _pen -= _number;
            [self showAllBuyGoods];
            break;
        case 2:
            NSLog(@"请输入数量:");
            scanf("%ld", &_number);
            _cup -= _number;
            [self showAllBuyGoods];
            
            break;
        case 3:
            NSLog(@"请输入数量:");
            scanf("%ld", &_number);
            _rubber -= _number;
            [self showAllBuyGoods];
            break;
        default:
            
            
            break;
    }
    
    
}


/**        根据用户输入判断是否买完       */
- (void)isGoOn:(NSInteger)number{
    switch (number) {
        case 1:
            [self disPlayAllGoods];
            break;
        case 2:
            NSLog(@"请付款");
        default:
            break;
    }
}
/**    根据用户输入判断用户要买哪一种商品   */
- (void)buyCode:(NSInteger)code{
    switch (code) {
        case 1:
            NSLog(@"请输入数量:");
            scanf("%ld", &_number);
            _pen += _number;
            [self showAllBuyGoods];
            break;
        case 2:
            NSLog(@"请输入数量:");
            scanf("%ld", &_number);
            _cup += _number;
            [self showAllBuyGoods];
            break;
        case 3:
            NSLog(@"请输入数量:");
            scanf("%ld", &_number);
            _rubber += _number;
            [self showAllBuyGoods];
            break;
        case 4:
            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 * 5 + _shop.rubber * 2 + _shop.pen * 3) > money) {
        NSLog(@"你买的东西超过了100块钱!!");
        [_shop deleteGoods];
        [self pay:money];
        
    }
    else{
        NSLog(@"付款成功!谢谢惠顾!!");
    }
    
}



@end

 

以上是关于OC----简单的购物系统----的主要内容,如果未能解决你的问题,请参考以下文章

商城系统简单购物车结构设计代码实现

OC基础--OC内存管理原则和简单实例

DAY4:简单购物系统

简单阐述下OC中UIImage三种创建方式~~~

如何用servlet写一个简单的购物车系统

打开购物车管理界面开发