关闭

用OC实现图书管理系统(基本包含OC大部分知识)

1589人阅读 评论(0) 收藏 举报

 

首先是两个类:Book和BookMIX,以下是相关代码:

 

#import <Foundation/Foundation.h>

@interface Book : NSObject <NSCoding>
{
    NSString *name;
    NSString *publishr;
    int number;   //书的编号
    int amount;   //书的数量
    int price;
}
@property NSString *name;    //书的名字
@property NSString *publishr;  //出版社
@property int number;   //书的编号
@property int amount;   //书的数量
@property int price;     //  书的价格

-(void)inputBook;
-(void)printInfo;

@end

#import "Book.h"

@implementation Book

@synthesize name;
@synthesize price;
@synthesize publishr;
@synthesize number;
@synthesize amount;

-(void)inputBook{        //录入图书的信息
    char nam[20];
    char pub[40];
    printf("请输入图书的编号 书名 出版社 价格 剩余数量\n");
    scanf("%d %s %s %d %d",&number,nam,pub,&price,&amount);
    name=[NSString stringWithUTF8String:nam];
    publishr=[NSString stringWithUTF8String:pub];
}
-(void)printInfo{       //打印出图书信息
    NSLog(@"图书的编号%d 书名%@  出版社%@ 价格%d 剩余数量%d\n",number,name,publishr,price,amount) ;
}
//实现coding协议的两种方法(编码和解码)
-(void) encodeWithCoder:(NSCoder *) aCoder{    // 编码
    [aCoder encodeObject:name forKey:@"name"];
    [aCoder encodeObject:publishr forKey:@"publisher"];
    [aCoder encodeInt:price forKey:@"price"];
    [aCoder encodeInt:number forKey:@"number"];
    [aCoder encodeInt:amount forKey:@"amout"];
}
-(id) initWithCoder:(NSCoder *)aDecoder{     // 解码
    
    self.name=[aDecoder decodeObjectForKey:@"name"];
    self.publishr=[aDecoder decodeObjectForKey:@"publisher"];
    self.number=[aDecoder decodeIntForKey:@"number"];
    self.price=[aDecoder decodeIntForKey:@"price"];
    self.amount=[aDecoder decodeIntForKey:@"amount"];
   
    return self;
   
}

@end

 

#import <Foundation/Foundation.h>
#import "Book.h"

@interface BookMIX : NSObject{
    NSMutableArray *allBook; //  所有图书
    NSMutableArray *record;   // 出售纪录
}
-(id)init;
-(void)addBook:(Book *) Book;        // 增加一本书
-(void)printallBook;                // 打印出所有的书
-(Book*)findBookNumber:(int) num;    //按学号查找图书
-(Book*)findBookName:(NSString *) nam;  //按书名查找图书
-(void)saleBook:(int) num;              //销售图书
-(void)printRecord;   //打印出增加和销售图书的纪录
-(void)saveBook;    //保存图书
-(void)readBook;   //读取图书
@end

#import "BookMIX.h"

@implementation BookMIX

-(id)init{
    if (self=[super init]) {
        allBook=[[NSMutableArray alloc]init];
        record=[[NSMutableArray alloc]init];
    }
    else{
        return nil;
    }
    return self;
}
-(void)addBook:(Book *) Book{      // 增加一本书
   
    [allBook addObject:Book];   //将新增加的书存到整体
   
    NSDate *date=[[NSDate alloc]initWithTimeIntervalSinceNow:(8*60*60)];
    [record addObject:[NSString stringWithFormat:@"\n%@增加了一本图书书名为%@",date,Book.name]];
}
-(void)printallBook{             //  输出所有的书
    Book *temp=[[Book alloc]init];
    for (int i=0; i<[allBook count]; i++) {
        temp=[allBook objectAtIndex:i];
        [temp printInfo];
    }
}
-(Book*)findBookNumber:(int) num{      // 按编号查找图书
    Book *temp;
    int i;
    for (i=0; i<[allBook count]; i++) {
        temp=[allBook objectAtIndex:i];
        if ((temp.number==num)) {
        [temp printInfo];
            break;
        }
    }
    if (i==[allBook count]) {
        printf("查无此书\n");
    }
    return nil;
}
-(Book*)findBookName:(NSString *) nam{       //按书名查找图书
    Book *temp;
    int i;
    for (i=0; i<[allBook count]; i++) {
        temp=[allBook objectAtIndex:i];
        if ([temp.name isEqualToString: nam]) {
            //return temp;
        [temp printInfo];
            break;
        }
    }
    if (i==[allBook count]) {
        printf("查无此书\n");
    }
    return nil;

   
}
-(void)saleBook:(int) num{          //销售图书
    Book *temp;
    int i;
    for (i=0; i<[allBook count]; i++) {
        (temp=[allBook objectAtIndex:i]);
        if (temp.number==num){
            if (temp.amount>0) {
                temp.amount--;
            NSDate *date=[[NSDate alloc]initWithTimeIntervalSinceNow:(8*60*60)];
            [record addObject:[NSString stringWithFormat:@"\n%@卖出了一本图书书名为%@",date,temp.name]];
                printf("\n卖书成功");
                break;
            }else{
                printf("\n对不起,书卖完了");
                break;
            }
           
        }
    }
    if (i==[allBook count]) {
        printf("查无此书\n");
    }
}
-(void)printRecord{            //输出所有添加和销售图书的纪录
    for (int i=0; i<[record count]; i++) {
       NSLog(@"%@",[record objectAtIndex:i] );
    }
   
}

-(void)saveBook{

    NSData *data=[[NSData alloc]init];    //归档图书信息
    data=[NSKeyedArchiver archivedDataWithRootObject:allBook];   //data用来保存数组allstudent的编码数据
    [data writeToFile:@"/Users/lileigang/Desktop/book.txt" atomically:YES];  //把归档后的数据存入文件中(注意路径是否正确)
    NSLog(@"\n图书信息保存成功"); 
   
    NSData *data2=[[NSData alloc]init];   //把销售和增加图书记录归档
    data2=[NSKeyedArchiver archivedDataWithRootObject:record];//data用来保存数组record的编码数据

    [data2 writeToFile:@"/Users/lileigang/Desktop/bookrecord.txt" atomically:YES];//把归档后的数据存入文件中(注意路径是否正确)
    NSLog(@"\n记录信息保存成功");
   
}
-(void)readBook{
   
    [allBook removeAllObjects];//先清空数组中的内容再读取。
    [record removeAllObjects];
   
    //从文件中读出归档过的数据
    NSData *data=[NSData dataWithContentsOfFile
                  :@"/Users/lileigang/Desktop/book.txt"];
    //用array保存把data解码后的数据
    NSArray *array=[NSKeyedUnarchiver unarchiveObjectWithData:data ];
    [allBook addObjectsFromArray:array];//把数据赋给allstudent
    NSLog(@"\n图书信息读取成功");
   
    NSData *data2=[NSData dataWithContentsOfFile
                   :@"/Users/lileigang/Desktop/bookrecord.txt"];
    //用array保存把data解码后的数据
    NSArray *array2=[NSKeyedUnarchiver unarchiveObjectWithData:data2 ];
    [record addObjectsFromArray:array2];//把数据赋给allstudent
    NSLog(@"\n记录信息读取成功");
   
   
}

@end

然后以下是main函数的代码:

  

#import <Foundation/Foundation.h>
#import "Book.h"
#import "BookMIX.h"

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

    @autoreleasepool {
        printf("\n欢迎进入图书管理系统");
        printf("llg2013-2014作品\n");
       
        char c;
        BookMIX *bm=[[BookMIX alloc] init];    //定义一个图书系统变量bm;
        while(YES){
            printf("\n1、添加图书");
            printf("\n2、输出所有图书");
            printf("\n3、查找图书");
            printf("\n4、销售图书");
            printf("\n5、销售纪录");
            printf("\n6、保存图书");
            printf("\n7、读取图书");
            printf("\n8、退出\n");
           
        scanf(" %c",&c);
            switch (c) {
                case '1':{
                    Book *temp=[[Book alloc]init];
                    [temp inputBook];
                    [bm addBook:temp];
                }
                     break;
                case '2':{
                    [bm printallBook];
                }
                    break;
           
                case '3':{
                    char name[20];
                    printf("\n请输入图书名字:");
                    scanf(" %s",name);
                    NSString *str=[NSString stringWithUTF8String:name];
                    Book *temp;
                    if ([str intValue]) {
                        temp=[bm findBookNumber:[str intValue]];
                    }else{
                        temp=[bm findBookName:str];
                    }
                    [temp printInfo];
                   
                }
                    break;
                case '4':{
                    int num;
                    printf("\n请输入图书编号:");
                    scanf(" %d",&num);
                   
                    [bm saleBook:num];
                   
                }
                    break;
                case '5':{
                    [bm printRecord];
                }
                    break;
                case '6':{
                    [bm saveBook];
                   
                }
                    break;
                   
                case '7':{
                    [bm readBook];
                   
                }
                    break;

                case '8':{
                    return 0;
                }
                    break;
                default:
                    break;
            }
           

        }
    }
    return 0;
}

 

程序可以正常运行,注意保存和读取时一定要注意路径的问题!(比如说你将一个文件保存在桌面上,一定要与自己电脑的桌面路径相同,因为不同的计算机桌面路径一般不同。

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:60029次
    • 积分:1161
    • 等级:
    • 排名:千里之外
    • 原创:53篇
    • 转载:31篇
    • 译文:1篇
    • 评论:6条
    最新评论