关闭

object c 图书管理系统

673人阅读 评论(0) 收藏 举报
分类:


//  main.m
#import <Foundation/Foundation.h>
#import "Book.h"
#import "BookMIS.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {//创建系统目录
        printf("欢迎进入图书管理系统\n");
        char c;
        BookMIS *ab=[[BookMIS alloc]init];
        while (YES) {
            printf("1.添加图书信息\n");
            printf("2.打印图书信息\n");
            printf("3.保存图书信息\n");
            printf("4.读取图书信息\n");
            printf("5.按编号查找图书信息\n");
            printf("6.按书名查找图书信息\n");
            printf("7.卖出图书信息\n");
            printf("8.记录图书信息\n");
            printf("9.删除图书信息\n");
            printf("0.退出\n");
            scanf(" %c",&c);
            switch (c) {
                case '1':
               {
                   Book * temp=[[Book alloc]init];
                    [temp inputBook];
                   [ab addNewBook:temp];
                }
                    break;
                case '2':
                  [ab printAllBook];
                    break;
                case '3':
                    [ab saveBook];
                    break;
                case '4':
                   [ab readBook];
                    break;
                case '5':
                {
                    int num;
                    printf("请输入编号\n");
                    scanf(" %d",&num);
                    Book * tmp;
                    tmp=[ab findBook1:num];
                    [tmp printInfo];
                }
                    break;
                case '6':
                {
                    char nam[20];
                    NSString * bname;
                    printf("请输入书名\n");
                    scanf(" %s",nam);
                    bname=[NSString stringWithUTF8String:nam];
                    Book * tmp;
                    tmp=[ab findBook2:bname];
                    [tmp printInfo];
                }
                    break;
                case '7':
                {   int num;
                    printf("请输入编号\n");
                    scanf(" %d",&num);
                    [ab saleBook:num];
                }
                    break;
                case '8':
                {
                    [ab bookRecord];
                    }break;
                case '9':
                {   int num;
                    printf("请输入编号\n");
                    scanf(" %d",&num);
                    [ab deleteBook:num];
                }break;
                case '0':
                   return 0;
                    break;
                default:
                    break;
            }
        }
    }
    return 0;
}
//创建书类
//  Book.h
#import <Foundation/Foundation.h>

@interface Book : NSObject<NSCoding>
@property NSString *bookName;
@property int number;
@property int price;
@property int bookNumber;
@property NSString *publicer;
-(void)inputBook;
-(void)printInfo;
@end
//
//  Book.m
#import "Book.h"

@implementation Book
@synthesize bookName;
@synthesize number;
@synthesize bookNumber;
@synthesize price;
@synthesize publicer;

//添加图书
-(void)inputBook{
    char nam[20];
    char pub[30];
    printf("请输入图书 编号 名字 出版社 价格 剩余数量\n");
    scanf(" %d %s %s %d %d",&number,nam,pub,&price,&bookNumber);
    bookName=[NSString stringWithUTF8String:nam];
    publicer=[NSString stringWithUTF8String:pub];
}

//打印图书
-(void)printInfo{
    NSLog(@" %d %@ %@ %d %d",number,bookName,publicer,price,bookNumber);
}

-(void)encodeWithCoder:(NSCoder *)aCoder

{
   
    [aCoder encodeObject:bookName forKey:@"bookName"];
   
    [aCoder encodeObject:publicer forKey:@"publicer"];
   
    [aCoder encodeInt:number forKey:@"number"];
   
    [aCoder encodeInt:price forKey:@"price"];
   
    [aCoder encodeInt:bookNumber forKey:@"bookNumber"];
   
}

-(id)initWithCoder:(NSCoder *)aDecoder

{
   
    self.bookName=[aDecoder decodeObjectForKey:@"bookName"];
   
     self.publicer=[aDecoder decodeObjectForKey:@"publicer"];
   
    self.number=[aDecoder decodeIntForKey:@"number"];
   
    self.price=[aDecoder decodeIntForKey:@"price"];
   
    self.bookNumber=[aDecoder decodeIntForKey:@"bookNumber"];
   
    return  self;
   
}@end
//创建图书管理类
//  BookMIS.h
#import <Foundation/Foundation.h>
#import "Book.h"
@interface BookMIS : NSObject
{
    NSMutableArray *allBook;
    NSMutableArray *record;//记录
}
-(void)addNewBook:(Book *)temp;
-(void)printAllBook;
-(Book *)findBook1:(int)num;
-(Book *)findBook2:(NSString*)nam;
-(void)deleteBook:(int)num;
-(void)saveBook;
-(void)readBook;
-(void)saleBook:(int)num;
-(void)bookRecord;
@end
//
//  BookMIS.m

#import "BookMIS.h"

@implementation BookMIS
-(id)init{
   
    if (self=[super init]) {//初始化
        allBook=[[NSMutableArray alloc]init];
        record=[[NSMutableArray alloc]init];
    }else
    {return  nil;}
    return  self;
}

//添加新书

-(void)addNewBook:(Book *)temp{
    [allBook addObject:temp];
    NSDate *date=[[NSDate alloc]initWithTimeIntervalSinceNow:(8*60*60)];
    [record addObject:[NSString stringWithFormat
                       :@"\n%@增加了一本图书书名为%@",date,temp.bookName]];
}

//打印所有书

-(void)printAllBook{
    for(int i=0;i<[allBook count];i++)
       
    {   Book *temp=[allBook objectAtIndex:i];
       
        [temp printInfo];
    }
}

//按编号查找书
-(Book *)findBook1:(int)num{
    for(int i=0;i<[allBook count];i++)//循环找到数组中的数据然后返回
       
    {   Book *temp=[allBook objectAtIndex:i];
       
        if (temp.number==num) {
           
            return temp;
           
            break;
        }
    }
    return nil;
}

//按书名查找书
-(Book *)findBook2:(NSString*)nam{
    for(int i=0;i<[allBook count];i++)
       
    {   Book *temp=[allBook objectAtIndex:i];
       
        if ([temp.bookName isEqualToString:nam])
        {
            return temp;
           
            break;
           
        }
    }
    return nil;
}

//保存信息

-(void)saveBook
{
    NSData *data=[[NSData alloc]init];
    NSData *data1=[[NSData alloc]init];
    data=[NSKeyedArchiver archivedDataWithRootObject:allBook];//归档
    [data writeToFile:@"//Users//mac//Desktop//book.txt" atomically:YES];
    data1=[NSKeyedArchiver archivedDataWithRootObject:allBook];
    [data1 writeToFile:@"//Users//mac//Desktop//book.txt" atomically:YES];
    NSLog(@"\n信息保存成功");
    }

//读取信息

-(void)readBook
{   [allBook removeAllObjects];
    [record removeAllObjects];
    NSData *data=[NSData dataWithContentsOfFile
                  :@"//Users//mac//Desktop//book.txt"];
    NSArray *array=[NSKeyedUnarchiver unarchiveObjectWithData:data ];//读档
    [allBook addObjectsFromArray:array];
    NSData *data1=[NSData dataWithContentsOfFile
                  :@"//Users//mac//Desktop//book.txt"];
    NSArray *array1=[NSKeyedUnarchiver unarchiveObjectWithData:data1 ];
    [record addObjectsFromArray:array1];
    NSLog(@"\n图书信息读取成功");
}

//删除图书信息

-(void)deleteBook:(int)num{
    for(int i=0;i<[allBook count];i++)
    { Book * nows=[allBook objectAtIndex:i];
        if(nows.number==num){
            [allBook removeObjectAtIndex:i];
            return;
        }
    }
   
}

//卖出书

-(void)saleBook:(int)num{
    for(int i=0;i<[allBook count];i++)
    {
        Book *temp=[allBook objectAtIndex:i];
        if (temp.number==num)
        {
         if (temp.bookNumber>0)
         {
            temp.bookNumber--;
            NSLog(@"售出");
            NSDate *date=[[NSDate alloc]initWithTimeIntervalSinceNow:(8*60*60)];
           [record addObject:[NSString stringWithFormat
                                :@"\n%@增加了一本图书书名为%@",date,temp.bookName]];
         }else
         {
            NSLog(@"没货了");
             [allBook removeObjectAtIndex:i];
            break;
         }
        }
    }
}

//图书记录
-(void)bookRecord{
   
    for(int i=0;i<[record count];i++)//循环打印
    {
        NSLog(@"%@",[record objectAtIndex:i]);
    }
   
}
@end

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23076次
    • 积分:485
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:13篇
    • 译文:0篇
    • 评论:0条
    文章分类