讲解将在代码注释中进行~
1.主函数代码:
//
// main.m
// OC图书馆租借系统
//
// Created by lichan on 13-11-29.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "BookSystem.h"
int main(int argc, const char * argv[])
{
// 1.图书入库
// 2.图书打印
// 3.图书出售
// 4.图书查询
// 5.图书排序 //将会实现返回上层菜单.
// 6.文件保存
// 7.数据读出
// 8.图书下架
// 9日志打印 主要是针对我们的操作进行记录,方便查询
// 0.退出系统
@autoreleasepool {
NSLog(@"欢迎进入图书馆租借系统(V1.0)");
BookSystem *sys = [[BookSystem alloc]init];
while (YES) {
printf("\n");
NSLog(@"请进行功能选择:");
NSLog(@"1:图书入库 2:图书打印");
NSLog(@"3:图书出售 4:图书查询");
NSLog(@"5:图书排序 6:图书下架");
NSLog(@"7:文件保存 8:数据读出");
NSLog(@"9:日志记录 0:退出系统");
int switchNum = 0;
scanf("%d",&switchNum);
switch (switchNum) {
case 1:
NSLog(@"图书入库:");
[sys addBookToSys];
break;
case 2:
[sys printBookOfSys];
break;
case 3:
[sys sellBookFromSys];
break;
case 4:
[sys queryBookFromSys];
break;
case 5:
[sys sortByIDBookOfSys];
break;
case 6:
[sys removeBookFromSys];
break;
case 7:
[sys writeBookOfSysToFile];
break;
case 8:
[sys readBookOfSysFromFile];
break;
case 9:
[sys printSysLog];
break;
case 0:
[sys writeBookOfSysToFile];
return 0;
default:
break;
}
}
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
2.book类对象
//
// Book.h
// OC图书馆租借系统
//
// Created by lichan on 13-11-29.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject<NSCoding>
{
int bookID;
NSString *bookName;
float bookPrice;
NSString *bookpublisher;
int bookNumber;
}
@property int bookID;
@property NSString *bookName;
@property float bookPrice;
@property NSString *bookpublisher;
@property int bookNumber;
-(id)init;
-(id)initWithBookID:(int)_id BookName:(NSString *)_name WithBookPrice:(float)_price WithPookBublisher:(NSString*)_bublisher WithBookNumber:(int)_number;
//-(void)motifyBookByBookID:(int)_id;
@end
2.
.m文件
//
// Book.m
// OC图书馆租借系统
//
// Created by lichan on 13-11-29.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "Book.h"
@implementation Book
@synthesize bookID,bookName,bookPrice,bookNumber,bookpublisher;
-(id)init
{
if (self = [super init]) {
bookID = -1,bookName = nil,bookPrice = 0.0 ,bookNumber = 0,bookpublisher = nil;
}
else
self = nil;
return self;
}
-(id)initWithBookID:(int)_id BookName:(NSString *)_name WithBookPrice:(float)_price WithPookBublisher:(NSString*)_bublisher WithBookNumber:(int)_number;
{
if (self = [self init]) {
bookID = _id;
bookName = _name;
bookPrice = _price ;
bookNumber = _number;
bookpublisher = _bublisher;
}
else
self = nil;
return self;
}
//实现了NSCoding协议,方便文件的读写.上次使用的是数组,今天使用的是字典
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:bookID forKey:@"id"];
[aCoder encodeObject:bookName forKey:@"name"];
[aCoder encodeFloat:bookPrice forKey:@"price"];
[aCoder encodeInt:bookNumber forKey:@"number"];
[aCoder encodeObject:bookpublisher forKey:@"publisher"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.bookID = [aDecoder decodeIntForKey:@"id"];
self.bookName = [aDecoder decodeObjectForKey:@"name"];
self.bookPrice = [aDecoder decodeFloatForKey:@"price"];
self.bookNumber = [aDecoder decodeIntForKey:@"number"];
self.bookpublisher = [aDecoder decodeObjectForKey:@"publisher"];
}
else
self = nil;
return self;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"%d %@ %.1f %@ %d",bookID,bookName,bookPrice,bookpublisher,bookNumber];
}
//-(void)motifyBookByBookID:(Book *)_book
//{
// NSLog(@"请输入要修改图书的图书ID");
// int bookid;
// scanf("%d",&bookid);
//
//
//}
@end
3.系统的代码:
//
// BookSystem.h
// OC图书馆租借系统
//
// Created by lichan on 13-11-29.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Book;
@interface BookSystem : NSObject
{
NSMutableDictionary *SystemDic;
NSMutableArray *SystemLog;
}
@property NSMutableDictionary *SystemDic;
@property NSMutableArray *SystemLog;
-(id)init;
// 1.图书入库
-(void)addBookToSys;
// 2.图书打印
-(void)printBookOfSys;
// 3.图书出售
-(void)sellBookFromSys;
// 4.图书查询
-(void)queryBookFromSys;
// 5.图书排序
-(void)sortByIDBookOfSys;
// 6.文件保存
-(void)writeBookOfSysToFile;
// 7.数据读出
-(void)readBookOfSysFromFile;
// 8.图书下架
-(void)removeBookFromSys;
// 9.日志打印
-(void)printSysLog;
@end
.m文件
//
// BookSystem.m
// OC图书馆租借系统
//
// Created by lichan on 13-11-29.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "BookSystem.h"
#import "Book.h"
@implementation BookSystem
@synthesize SystemDic,SystemLog;
-(id)init
{
if (self = [super init]) {
SystemDic = [[NSMutableDictionary alloc]init];
SystemLog = [[NSMutableArray alloc]init];
}
else
self = nil;
return self;
}
// 1.图书入库
-(void)addBookToSys
{
NSLog(@"请输入新进图书信息:");
NSLog(@"图书ID 图书名字 图书价格 图书出版社 图书数量(示例如下:)");
NSLog(@"0001 飘 100 北京出版社 20 ");
char name[100];
char publisher[100];
int bid,number;
float price;
scanf("%d %s %f %s %d",&bid,name,&price,publisher,&number);
Book *newBook = [[Book alloc]initWithBookID:bid BookName:[NSString stringWithUTF8String:name] WithBookPrice:price WithPookBublisher:[NSString stringWithUTF8String:publisher] WithBookNumber:number];
[SystemDic setValue:newBook forKey:[NSString stringWithFormat:@"%d",newBook.bookID]];
NSString *addLog = [NSString stringWithFormat:@"[%@]:add %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],newBook];
[SystemLog addObject:addLog];
NSLog(@"[%@]%@添加成功.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],newBook);
}
// 2.图书打印
-(void)printBookOfSys
{
NSArray *dicKeys = [SystemDic allKeys];
for (NSString *onekey in dicKeys) {
NSLog(@"%@",[SystemDic objectForKey:onekey]);
}
NSString *printLog = [NSString stringWithFormat:@"[%@]:print books of the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60]];
[SystemLog addObject:printLog];
NSLog(@"打印完毕!");
}
// 3.图书出售
-(void)sellBookFromSys
{
NSLog(@"请输入要出售的图书ID:");
int bid;
scanf("%d",&bid);
NSString *theKey = [NSString stringWithFormat:@"%d",bid];
if (theKey == nil) {
NSLog(@"未找到该图书");
return;
}
Book *theBook = [SystemDic objectForKey:theKey];
if([theBook bookNumber] > 0)
{
theBook.bookNumber--;
NSString *sellLog = [NSString stringWithFormat:@"[%@]:Sell %@ from the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],theBook];
[SystemLog addObject:sellLog];
}
else
{
NSLog(@"对不起,%@ 图书已售空.",[theBook bookName]);
return ;
}
}
// 4.图书查询
//@"1:按ID查找(最快最精确)
-(void)queryBookByIDFromSys
{
NSLog(@"请输入要查询的图书ID:");
int bid;
scanf("%d",&bid);
NSString *queryLog = [NSString stringWithFormat:@"[%@]:query ID: %d to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],bid];
[SystemLog addObject:queryLog];
NSString *theKey = [NSString stringWithFormat:@"%d",bid];
if (theKey == nil) {
NSLog(@"未找到该图书.");
return;
}
Book *theBook = [SystemDic objectForKey:theKey];
NSLog(@"你所要查询的图书信息:%@",theBook);
NSString *querySuccessLog = [NSString stringWithFormat:@"[%@]:QuerySuccess:query %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],theBook];
[SystemLog addObject:querySuccessLog];
}
//2:按书名查找");
-(void)queryBookByNameFromSys
{
NSLog(@"请输入要查询的图书名字:");
char bname[100];
scanf("%s",bname);
NSString *strName = [NSString stringWithUTF8String:bname];
NSString *queryLog = [NSString stringWithFormat:@"[%@]:query Name: %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],strName];
[SystemLog addObject:queryLog];
NSArray *nameArr = [SystemDic allKeys];
int num = 0;
for (NSString *str in nameArr) {
Book *theBook = [SystemDic objectForKey:str];
if ([theBook.bookName isEqualToString:strName]) {
NSLog(@"符合要求的图书信息:%@",theBook);
NSString *querySuccessLog = [NSString stringWithFormat:@"[%@]:QuerySuccess:query %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],theBook];
[SystemLog addObject:querySuccessLog];
num++;
}
}
if (num == 0) {
NSLog(@"未找到该图书.");
}
}
//(@"3:按价格查找 //
-(void)queryBookByPriceFromSys
{
NSLog(@"请输入要查询的图书价格区间(如10.7 20.4):");
float max;
float min;
scanf("%f %f",&max,&min);
if (max < min) {
int temp = max;
max = min;
min = temp;
}
NSString *queryLog = [NSString stringWithFormat:@"[%@]:query Price: %f~%f to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],min,max];
[SystemLog addObject:queryLog];
NSArray *nameArr = [SystemDic allKeys];
int num = 0;
for (NSString *str in nameArr) {
Book *theBook = [SystemDic objectForKey:str];
if (theBook.bookPrice >= min && theBook.bookPrice <= max) {
NSLog(@"符合要求的图书信息:%@",theBook);
NSString *querySuccessLog = [NSString stringWithFormat:@"[%@]:QuerySuccess:query %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],theBook];
[SystemLog addObject:querySuccessLog];
num++;
}
}
if (num == 0) {
NSLog(@"未找到该图书.");
}
}
//4:按出版社查找"
-(void)queryBookByPublisherFromSys
{
NSLog(@"请输入要查询的图书名字:");
char bname[100];
scanf("%s",bname);
NSString *strPublisher = [NSString stringWithUTF8String:bname];
NSString *queryLog = [NSString stringWithFormat:@"[%@]:query Name: %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],strPublisher];
[SystemLog addObject:queryLog];
NSArray *nameArr = [SystemDic allKeys];
int num = 0;
for (NSString *str in nameArr) {
Book *theBook = [SystemDic objectForKey:str];
if ([theBook.bookpublisher isEqualToString:strPublisher]) {
NSLog(@"符合要求的图书信息:%@",theBook);
NSString *querySuccessLog = [NSString stringWithFormat:@"[%@]:QuerySuccess:query %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],theBook];
[SystemLog addObject:querySuccessLog];
num++;
}
}
if (num == 0)
NSLog(@"未找到该图书.");
}
-(void)queryBookFromSys
{
int flog = 1;
while (flog) {
NSLog(@"查询项选择:");
NSLog(@"1:按ID查找(最快最精确) 2:按书名查找");
NSLog(@"3:按价格查找 4:按出版社查找");
NSLog(@"0:返回上级");
int queryNumber;
scanf("%d",&queryNumber);
switch (queryNumber) {
case 1:
[self queryBookByIDFromSys];
break;
case 2:
[self queryBookByNameFromSys];
break;
case 3:
[self queryBookByPriceFromSys];
break;
case 4:
[self queryBookByPublisherFromSys];
break;
case 0:
flog = 0;
break;
default:
break;
}
}
}
// 5.图书排序
-(void)sortByIDBookOfSys
{
NSString *sortLog = [NSString stringWithFormat:@"[%@]:SortBookSystem in the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60]];
[SystemLog addObject:sortLog];
NSArray *arr = [NSArray arrayWithArray:[SystemDic allKeys]];
NSArray *keyArr = [arr sortedArrayUsingComparator:^(id object1,id object2)
{
NSComparisonResult result = [object1 compare:object2];
return result;
} ];
NSMutableDictionary *newSystemDic = [[NSMutableDictionary alloc]init];
for (NSString *onekey in keyArr) {
Book *newBook = [[Book alloc]init];
newBook = [SystemDic objectForKey:onekey];
NSLog(@"%@",newBook);
[newSystemDic setObject:newBook forKey:onekey];
}
NSLog(@"%@",newSystemDic);
//SystemDic = [NSMutableDictionary dictionaryWithDictionary:newSystemDic];
// SystemDic = newSystemDic;
}
// 6.文件保存
-(void)writeBookOfSysToFile
{
NSString *path = [NSString stringWithFormat:@"/Users/len/Desktop"];
NSLog(@"请输入你所要写入的文件名字:");
char sfilename[20];
scanf("%s",sfilename);
NSString *filename = [NSString stringWithUTF8String:sfilename];
NSString *filepath = [path stringByAppendingPathComponent:filename];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:SystemDic];
[data writeToFile:filepath atomically:YES];
//日志的保存
NSDate *now =[NSDate dateWithTimeIntervalSinceNow:8*60*60];
NSCalendar *calender = [NSCalendar currentCalendar];
NSUInteger unitInteger = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
NSDateComponents *components = [calender components:unitInteger fromDate:now];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
NSString *ymdDate = [NSString stringWithFormat:@"%lu-%lu-%lu",year,month,day];
//获得当前的年月日
NSString *tfileLogName = [NSString stringWithUTF8String:sfilename] ;
NSString *fileLogName = [tfileLogName stringByAppendingString:[NSString stringWithFormat:@"%@Log",ymdDate]];
NSLog(@"%@",fileLogName);
NSString *Logfilepath = [path stringByAppendingPathComponent:fileLogName];
NSData *Logdata = [NSKeyedArchiver archivedDataWithRootObject:SystemLog];
[Logdata writeToFile:Logfilepath atomically:YES];
NSString *savetoFileLog = [NSString stringWithFormat:@"[%@]:wirte To filename:%@ fileLog: %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],filename,fileLogName];
[SystemLog addObject:savetoFileLog];
}
// 7.数据读出
-(void)readBookOfSysFromFile
{
NSString *path = [NSString stringWithFormat:@"/Users/len/Desktop"];
NSLog(@"请输入你所要读取的文件名字:");
char sfilename[20];
scanf("%s",sfilename);
NSString *filename = [NSString stringWithUTF8String:sfilename];
NSString *filepath = [path stringByAppendingPathComponent:filename];
NSMutableData *data = [[NSMutableData alloc]initWithContentsOfFile:filepath];
self.SystemDic = [NSKeyedUnarchiver unarchiveObjectWithData:data];
//日志读出
NSDate *now =[NSDate dateWithTimeIntervalSinceNow:8*60*60];
NSCalendar *calender = [NSCalendar currentCalendar];
NSUInteger unitInteger = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
NSDateComponents *components = [calender components:unitInteger fromDate:now];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
NSString *ymdDate = [NSString stringWithFormat:@"%lu-%lu-%lu",year,month,day];
//获得当前的年月日
NSString *tfileLogName = [NSString stringWithUTF8String:sfilename] ;
NSString *fileLogName = [tfileLogName stringByAppendingString:[NSString stringWithFormat:@"%@Log",ymdDate]];
NSLog(@"%@",fileLogName);
NSString *Logfilepath = [path stringByAppendingPathComponent:fileLogName];
//日志读出
NSMutableData *Logdata = [[NSMutableData alloc]initWithContentsOfFile:Logfilepath];
self.SystemLog = [NSKeyedUnarchiver unarchiveObjectWithData:Logdata];
NSString *readFromFileLog = [NSString stringWithFormat:@"[%@]:read from filename:%@ fileLog: %@ to the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],filename,fileLogName];
[SystemLog addObject:readFromFileLog];
}
// 8.图书下架
-(void)removeBookFromSys
{
NSLog(@"请输入要下架的图书ID:");
int bid;
scanf("%d",&bid);
NSString *theKey = [NSString stringWithFormat:@"%d",bid];
if (theKey == nil) {
NSLog(@"未找到该图书");
return;
}
//Book *theBook = [SystemDic objectForKey:theKey];
[SystemDic removeObjectForKey:theKey];
NSString *removeBookLog = [NSString stringWithFormat:@"[%@]:remove BookID: %@ from the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60],theKey];
[SystemLog addObject:removeBookLog];
}
// 9.日志打印
-(void)printSysLog
{
NSLog(@"%@",SystemLog);
NSString *printLog = [NSString stringWithFormat:@"[%@]:print Log of the SystemDic.",[NSDate dateWithTimeIntervalSinceNow:8*60*60]];
[SystemLog addObject:printLog];
}
@end