首页 > 编程语言 > 详细

数组 客户订单实例

时间:2015-12-15 22:33:50      阅读:343      评论:0      收藏:0      [点我收藏+]

订单类

//
//  Order.h

#import <Foundation/Foundation.h>
#import "Customer.h"
@interface Order : NSObject

@property(nonatomic)int oid;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)Customer *customer;

@end
//
//  Order.m

#import "Order.h"

@implementation Order
-(NSString*)description{
    return [NSString stringWithFormat:@"%d,%@",_oid,_name];
}
@end

客户类

//
//  Customer.h

#import <Foundation/Foundation.h>

@interface Customer : NSObject

@property int cid;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *email;
@property(nonatomic,strong)NSString *address;

@end
//
//  Customer.m

#import "Customer.h"

@implementation Customer
-(NSString*)description{
    return [NSString stringWithFormat:@"%d,%@,%@,%@",_cid,_name,_email,_address];
}
@end

管理客户和订单类

//
//  CustomerAndOrderManager.h
//  数组的客户订单
//
//  Created by MAC on 15/12/15.
//  Copyright © 2015年 MAC. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Customer.h"
#import "Order.h"
@interface CustomerAndOrderManager : NSObject
@property(nonatomic,strong)NSMutableArray *customerList;
@property(nonatomic,strong)NSMutableArray *orderList;

//1.管理客户
-(void)addCustomer:(Customer*)c;
-(void)deleteCustomer:(Customer*)c;
-(Customer*)getCustomerById:(int)cid1;
-(void)listCustomer;

//2.管理订单
-(void)addOrder:(Order*)o;
-(void)deleteOrder:(Order*)o;
-(NSArray*)getOrderByCustomer:(Customer*)c;
-(void)listOrder;
@end

//
//  CustomerAndOrderManager.m
//  数组的客户订单
//
//  Created by MAC on 15/12/15.
//  Copyright © 2015年 MAC. All rights reserved.
//

#import "CustomerAndOrderManager.h"

@implementation CustomerAndOrderManager
-(instancetype)init{
    if (self=[super init]) {
        self.customerList = [NSMutableArray arrayWithCapacity:10];
        self.orderList = [NSMutableArray arrayWithCapacity:10];
    }
    return self;
}

//1.管理客户
-(void)addCustomer:(Customer*)c{
    [self.customerList addObject:c];
}
-(void)deleteCustomer:(Customer*)c{
    [self.customerList removeObject:c];
}
-(Customer*)getCustomerById:(int)cid1{
    for (Customer *c in self.customerList) {
        if (c.cid==cid1) {
            return c;
        }
    }
    return nil;
}
-(void)listCustomer{
    for (Customer *c in self.customerList) {
        NSLog(@"%@",c);
    }
}

//2.管理订单
-(void)addOrder:(Order*)o{
    [self.orderList addObject:o];
}
-(void)deleteOrder:(Order*)o{
    [self.orderList removeObject:o];
}
-(NSArray*)getOrderByCustomer:(Customer*)c{
    NSMutableArray *a =[[NSMutableArray alloc]init];
    for (Order *o in self.orderList) {
        if (o.customer==c){
        [a addObject:o];
        }
    }
    return a;
}
-(void)listOrder{
    for (Order *o in self.orderList) {
        NSLog(@"%@",o);
    }
}

@end

 

实现类 .m

//
//  main.m
//  数组的客户订单
//
//  Created by MAC on 15/12/15.
//  Copyright © 2015年 MAC. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Order.h"
#import "Customer.h"
#import "CustomerAndOrderManager.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Customer *c1 = [[Customer alloc]init];
         Customer *c2 = [[Customer alloc]init];
         Customer *c3 = [[Customer alloc]init];
        Order *o1 = [[Order alloc]init];
       Order *o2 = [[Order alloc]init];
        Order *o3 = [[Order alloc]init];
        CustomerAndOrderManager *a = [[CustomerAndOrderManager alloc]init];
        c1.cid = 18;
       c1.name = @"xiaowang";
        c1.email = @"www@qq.com";
        c1.address = @"625";
        //加入到客户列表
        [a addCustomer:c1];
        c2.cid = 20;
        c2.name = @"xiaohei";
        c2.email = @"www@163.com";
        c2.address = @"624";
        //加入到客户列表
        [a addCustomer:c2];
        c3.cid = 22;
        c3.name = @"xiaobai";
        c3.email = @"www@126.com";
        c3.address = @"623";
        //加入到客户列表
        [a addCustomer:c3];
        
        
        o1.customer = c1;
        o1.oid = 1001;
        o1.name = @"shampoo";
          //加入到订单列表
        [a addOrder:o1];
        o2.customer = c2;
        o2.oid = 1002;
        o2.name = @"Washing powder";
      //加入到订单列表
        [a addOrder:o2];
        
        o3.customer = c1;
        o3.oid = 1005;
        o3.name = @"WashingPowder";
        [a addOrder:o3];
        //遍历所有的订单和客户
        [a listCustomer];
        [a listOrder];
        //查询cid为20的客户信息
      Customer *c=[a getCustomerById:20];
        NSLog(@"%@",c);
//        //删除客户
//        [a deleteCustomer:c1];
//        [a listCustomer];
        NSLog(@"------------");
        //查询客户的全部订单
       NSArray *array = [a getOrderByCustomer:c1];
        NSLog(@"%@",array);
        
        [a deleteOrder:o3];
        [a listOrder];
        
        
        
        
        
    }
    return 0;
}

 

数组 客户订单实例

原文:http://www.cnblogs.com/WJR12/p/5049605.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!