FJBook.h
#import <Foundation/Foundation.h>
/**图书类*/
@interface FJBook : NSObject
/**图书编号*/
@property (nonatomic,copy,readonly) NSString *bookId;
/**图书名字*/
@property (nonatomic,copy,readonly) NSString *name;
/**图书每日租金*/
@property (nonatomic,assign) double price;
/**图书是否借出*/
@property (nonatomic,assign) BOOL lended;
/**图书借出的时间*/
@property (nonatomic,strong) NSDate *lendDate;//为借出时候的时间
/**图书借出的次数*/
@property (nonatomic,assign) NSUInteger counter;
/**初始化方法*/
+ (instancetype) bookWithId:(NSString *) bookId name:(NSString *)name
price:(double) price;
- (instancetype) bookWithId:(NSString *) bookId name:(NSString *)name
price:(double) price;
/**借出图书*/
- (BOOL)lendOut;
/**归还图书并且计算租金*/
- (double) returnBack;
@end
FJBook.m
#import "FJBook.h"
static const int SECONDS_PER_DAY = 60*60*24;
@implementation FJBook
+ (instancetype) bookWithId:(NSString *)bookId name:(NSString *)name price:(double)price{
return [[self alloc]initWithId:(NSString *)bookId name:(NSString *)name price:(double)price];
}
- (instancetype)initWithId:(NSString *)bookId name:(NSString *)name price:(double)price{
if (self = [super init]) {
_bookId = bookId;
_name = name;
_price = price;
}
return self;
}
- (BOOL)lendOut{
if (!_lendDate) {
_lended = YES;
_lendDate = [NSDate date];
_counter++;
return YES;
}
return NO;
}
- (double)returnBack{
double totalPrice = 0;
if (_lended) {
/**图书借出时间是现在到借出时候的时间间隔*/
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:_lendDate];
/**ceil函数为向上取整:图书借出不足一天按照一天计算租金*/
NSUInteger days = (NSUInteger) ceil(interval/SECONDS_PER_DAY);
totalPrice = days * _price;
_lended = NO;
_lendDate = nil;
}
return totalPrice;
}
- (NSString *) description{
static NSDateFormatter *formatter = nil;
if (!formatter) {
/**时间格式化*/
formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy年MM月dd日";
formatter.timeZone = [NSTimeZone systemTimeZone];
}
/**图书输入格式*/
return [NSString stringWithFormat:@"%@ %@ %@ %.2f %@ %ld",_bookId
,_name,_lended? @"已经借出":@"没有借出",_price,
_lendDate?[formatter stringFromDate:_lendDate]:@"-----",_counter];
}
@end
FJBookManager.h
#import <Foundation/Foundation.h>
@class FJBook;
@interface FJBookManager : NSObject{
NSMutableArray *_booksArray;
}
/**图书管理只有一个,所以这个管理类是一个单例,此处为单例*/
+ (instancetype) defaultManager;
/**添加图书*/
- (BOOL) add:(FJBook *)book;
/**根据编号删除图书*/
- (BOOL) deleteById:(NSString *) bookId;
/**显示所有的图书*/
- (NSArray *) allBooks;
/**根据编号查找图书*/
- (FJBook *) findById:(NSString *)bookId;
/**根据关键字查找图书*/
- (NSArray *) findByName: (NSString *) keyWord;
/**根据借出的次数对图书排行*/
- (NSArray *) top10;
/**把图书借出去*/
- (BOOL) lendBookOut:(NSString *) bookId;
/**根据编号判读是否归还图书成功*/
- (BOOL) returnBookBack:(NSString *) bookId;
@end
FJBookManager.m
#import "FJBookManager.h"
#import "FJBook.h"
@implementation FJBookManager
/**初始化一个图书管理类,初始化一个数组装所有的图书*/
- (instancetype) init{
if (self = [super init]) {
_booksArray = [NSMutableArray array];
/**初始化图书*/
[self loadTest];
}
return self;
}
/**初始化一个单例,固定写法*/
+ (instancetype) defaultManager{
static FJBookManager *bookManager = nil;
if (bookManager == nil) {
bookManager = [[FJBookManager alloc]init];
}
return bookManager;
}
- (void) loadTest{
FJBook *b1 = [FJBook bookWithId:@"1" name:@"盗墓笔记" price:9.7];
FJBook *b2 = [FJBook bookWithId:@"2" name:@"鬼吹灯" price:7.7];
FJBook *b3 = [FJBook bookWithId:@"7" name:@"偶像日常" price:7.7];
b1.lended = YES;
b2.lended = NO;
b3.lended = NO;
b1.counter = 177;
b2.counter = 100;
b3.counter = 777;
b1.lendDate = [NSDate dateWithTimeInterval:77777 sinceDate:[NSDate date]];
[_booksArray addObject:b1];
[_booksArray addObject:b2];
[_booksArray addObject:b3];
}
- (NSArray *)allBooks{
return [_booksArray copy];
}
- (FJBook *)findById:(NSString *)bookId{
for (FJBook *book in _booksArray) {
if ([book.bookId isEqualToString:bookId]) {
return book;
}
}
return nil;
}
- (BOOL)add:(FJBook *)book{
FJBook *tempBook = [self findById:book.bookId];
if (!tempBook) {
[_booksArray addObject:book];
return YES;
}
return NO;
}
- (BOOL)deleteById:(NSString *)bookId{
FJBook *tempBook = [self findById:bookId];
if (tempBook && !tempBook.lended) {
[_booksArray removeObject:tempBook];
return YES;
}
return NO;
}
- (NSArray *)findByName:(NSString *)keyWord{
NSMutableArray *books = [NSMutableArray array];
for (FJBook *book in _booksArray) {
if ([book.name containsString:keyWord]) {
[books addObject:book];
}
}
return [books copy];
}
- (NSArray *)top10{
NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"counter" ascending:NO];
NSArray *bookArray = [_booksArray sortedArrayUsingDescriptors:@[desc]];
NSUInteger length = bookArray.count < 10 ? bookArray.count : 10;
return [bookArray subarrayWithRange:NSMakeRange(0, length)];
}
- (BOOL)lendBookOut:(NSString *)bookId{
FJBook *book = [self findById:bookId];
return book ? [book lendOut] : NO;
}
- (BOOL)returnBookBack:(NSString *)bookId{
FJBook *book = [self findById:bookId];
return book ? [book returnBack] : NO;
}
@end
main.m
#import <Foundation/Foundation.h>
#import "FJBook.h"
#import "FJBookManager.h"
/**因为每次输入都会读入字符的回车符,所以要将输入符的最后一个回车转换成空格才能成功读取*/
char *foo (char *str){
str[strlen(str)-1] = '\0';
return str;
}
/**显示所有的图书信息*/
void disply_all_books(NSArray *bookArray){
printf("----图书信息-------\n");
for ( FJBook *book in bookArray) {
printf("%s\n",book.description.UTF8String);
}
printf("\n");
}
/**添加图书*/
void add_new_book(){
printf("输入编号:\n");
char bookId[6];
fgets(bookId, 6, stdin);
printf("请输入图书名字:\n");
char name[102];
fgets(name, 102, stdin);
printf("请输入本书的日租金:\n");
double price;
scanf("%lf",&price);
FJBook *book = [FJBook bookWithId:[NSString stringWithUTF8String:foo(bookId)] name:[NSString stringWithUTF8String:foo(name)] price:price];
if ([[FJBookManager defaultManager] add:book]) {
printf("添加成功!");
}else{
printf("添加失败!");
}
}
/**删除图书*/
void delete_book(){
printf("输入您要删除的图书编号:\n");
char bookId[6];
fgets(bookId, 6, stdin);
BOOL success = [[FJBookManager defaultManager] deleteById:[NSString stringWithUTF8String:foo(bookId)]];
if (success) {
printf("删除成功\n");
}else{
printf("删除失败\n");
}
}
/**查找图书*/
void search_books()
{
printf("输入查找的图书名字:\n");
char bookName[102];
fgets(bookName, 102, stdin);
NSArray *array = [[FJBookManager defaultManager ] findByName:[NSString stringWithUTF8String:foo(bookName)]];
disply_all_books(array);
}
/**借出图书*/
void lendbook(){
printf("输入您想借的图书编号:\n");
char bookId[6];
fgets(bookId, 102, stdin);
BOOL lend_success = [[FJBookManager defaultManager] lendBookOut:[NSString stringWithUTF8String:foo(bookId)]];
if (lend_success) {
printf("借出成功");
}else{
printf("借出失败");
}
}
/**归还图书*/
void returnbook()
{
printf("输入您所还的图书编号:\n");
char bookId[6];
fgets(bookId, 6, stdin);
BOOL return_success = [[FJBookManager defaultManager] returnBookBack:[NSString stringWithUTF8String:foo(bookId)]];
FJBook *book = [[FJBookManager defaultManager] findById:[NSString stringWithUTF8String:foo(bookId)]];
double totalPrice = [book returnBack];
if(return_success){
printf("还书成功,借书金额是:%lf",totalPrice);
}else{
printf("还书失败");
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
FJBookManager *manager = [FJBookManager defaultManager];
while (1) {
printf("1.新增图书\n");
printf("2.删除图书\n");
printf("3.查找图书\n");
printf("4.借出图书\n");
printf("5.归还图书\n");
printf("6.显示图书\n");
printf("7.排行图书\n");
printf("8.退出系统\n");
int choice;char dis;
do{
printf("输入您的选择:\n");
scanf("%d%c",&choice,&dis);
}while (choice < 1 || choice > 8) ;
switch (choice) {
case 1:
add_new_book();
break;
case 2:
delete_book();
break;
case 3:
search_books();
break;
case 4:
lendbook();
break;
case 5:
returnbook();
break;
case 6:
disply_all_books([manager allBooks]);
break;
case 7:
disply_all_books([manager top10]);
break;
case 8:
printf("退出本系统!");
exit(0);
}
}
}
return 0;
}