OC-* 实现图书管理系统:数组可以添加、删除可以从数组根据书名查找书籍,并修改书籍的价格

main.m

/* 实现图书管理系统:
 1、使⽤数组管理所有书籍。
 2、数组可以添加、删除。
 3、可以从数组根据书名查找书籍,并修改书籍的价格。
 4、展⽰所有书籍清单(书名)。*/

// 创建 对象
Book *book1 = [Book bookWithBookName:@"YIem" price:20];
Book *book2 = [Book bookWithBookName:@"Bian" price:30];
Book *book3 = [Book bookWithBookName:@"bianyi" price:15];
Book *book4 = [Book bookWithBookName:@"YI" price:35];
Book *book5 = [Book bookWithBookName:@"卞一" price:66];
Book *book6 = [Book bookWithBookName:@"卞" price:23];
// 创建可变数组
NSMutableArray *bookArr = [NSMutableArray array];
// 添加书本
[bookArr addObject:book1];
[bookArr addObject:book2];
[bookArr addObject:book3];
[bookArr addObject:book4];
[bookArr addObject:book5];
[bookArr addObject:book6];
NSLog(@"%@", bookArr);
// 删除书本
[bookArr removeLastObject];
NSLog(@"%@", bookArr);
// 查找书名 修改价格
// 1.遍历数组
for (Book *b in bookArr) {
//判断书名
if ([b.bookName isEqualToString:@"YIem"]) {
// 3.改变价格
b.price = 100;
}
}
NSLog(@"%@", bookArr);
// 清单
for (Book *b in bookArr) {
NSLog(@"%@", b.bookName);
}

屏幕快照 2016-01-09 下午3.18.44.png
屏幕快照 2016-01-09 下午3.19.05.png
屏幕快照 2016-01-09 下午3.19.26.png

Book.h

#import <Foundation/Foundation.h>

@interface Book : NSObject
// 书名
@property (nonatomic, copy) NSString *bookName;
// 价格
@property (nonatomic, assign) CGFloat price;
// 初始化

  • (instancetype)initWithBookName:(NSString *)name
    price:(CGFloat)price;
    // 便利构造器
  • (instancetype)bookWithBookName:(NSString *)name
    price:(CGFloat)price;
    @end

屏幕快照 2016-01-09 下午3.19.43.png

Book.m

@implementation Book

// 初始化

  • (instancetype)initWithBookName:(NSString *)name price:(CGFloat)price
    {
    self = [super init];
    if (self) {
    self.bookName = name;
    self.price = price;
    }
    return self;
    }
    // 便利构造器
  • (instancetype)bookWithBookName:(NSString *)name price:(CGFloat)price
    {
    Book *b = [[Book alloc] initWithBookName:name price:price];
    return b;
    }
    // 描述方法
  • (NSString *)description
    {
    return [NSString stringWithFormat:@"name: %@, price: %.2f", self.bookName, self.price];
    }

屏幕快照 2016-01-09 下午3.20.05.png

Libaray.h
#import <Foundation/Foundation.h>
@class Book;
@interface Libaray : NSObject
// 书本数组
@property (nonatomic, copy) NSMutableArray *bookArr;
// 添加

  • (void)addBook:(Book *)book;
    // 删除
  • (void)removeBookArrAtIndexes:(NSIndexSet *)indexes;
    // 修改价格
  • (void)changPriceByName:(NSString *)name price:(CGFloat)price;
  • (void)printBookList;

@end

屏幕快照 2016-01-09 下午3.20.18.png

Libaray.m

#import "Libaray.h"

#import "Book.h"
@implementation Libaray
// 初始化

  • (instancetype)init
    {
    self = [super init];
    if (self) {
    self.bookArr = [NSMutableArray array];
    }
    return self;
    }
    // 添加
  • (void)addBook:(Book *)book
    {
    [self.bookArr addObject:book];
    }
    // 删除
  • (void)removeBookArrAtIndexes:(NSIndexSet *)indexes
    {
    [self.bookArr removeObjectsAtIndexes:indexes];
    }
    // 修改价格
  • (void)changPriceByName:(NSString *)name price:(CGFloat)price
    {
    // 遍历数组
    for (Book *b in self.bookArr) {
    // 判断
    if ([b.bookName isEqualToString:name]) {
    // 修改
    b.price = price;
    }
    }
    }
    // 清单
  • (void)printBookList
    {
    for (Book *b in self.bookArr) {
    NSLog(@"清单: %@", b.bookName);
    }
    }
    @end

屏幕快照 2016-01-09 下午3.20.38.png

屏幕快照 2016-01-09 下午3.20.49.png

YIem

标签: none

添加新评论

Title - Artist
0:00