//
// main.m
// OC6_练习
//
// Created by YIem. on 12/1/15.
// Copyright (c) 2015 www.yiem.net YIem博客. All rights reserved.
//
import <Foundation/Foundation.h>
import "Book.h"
import "Libaray.h"
import "Contact.h"
int main(int argc, const char * argv[]) {
if 0
/* 实现图书管理系统:
1、使⽤数组管理所有书籍。
2、数组可以添加、删除。
3、可以从数组根据书名查找书籍,并修改书籍的价格。
4、展⽰所有书籍清单(书名)。*/
// 创建对象
Book *b1 = [Book bookWithBookName:@"Objective-C" price:10];
Book *b2 = [Book bookWithBookName:@"C Programming Language" price:20];
Book *b3 = [Book bookWithBookName:@"C Primer Plus" price:15];
// 创建可变数组
// NSMutableArray *bookArr = [@[b1, b2, b3] mutableCopy];
NSMutableArray *bookArr = [NSMutableArray array];
// 添加书本
[bookArr addObject:b1];
[bookArr addObject:b2];
[bookArr addObject:b3];
// 删除
// [bookArr removeLastObject];
// 查找书名 -> 修改价格
// 1.遍历数组
for (Book *b in bookArr) {
// 2.判断书名
if ([b.bookName isEqualToString:@"Objective-C"]) {
// 3.改变价格
b.price = 100;
}
}
NSLog(@"%@", bookArr);
// 清单
for (Book *b in bookArr) {
NSLog(@"%@", b.bookName);
}
endif
if 0
// 创建对象
Book *b1 = [Book bookWithBookName:@"Objective-C" price:10];
Book *b2 = [Book bookWithBookName:@"C Programming Language" price:20];
Book *b3 = [Book bookWithBookName:@"C Primer Plus" price:15];
// 创建Libaray对象
Libaray *lib = [[Libaray alloc] init];
// 添加
[lib addBook:b1];
[lib addBook:b2];
[lib addBook:b3];
// 删除
// [lib removeBookAtIndex:0];
// 修改
[lib changePriceByName:@"Objective-C" price:100];
NSLog(@"%@", lib.bookArr);
// 清单
[lib printBookList];
endif
if 1
/*作业2:
完成以下需求
需求:1、定义 联系人类Contact。实例变量:姓名(拼⾳, ⾸字母大写)、性别、电话号码、住址、分组名称、年龄。 ⽅法:自定义初始化方法(姓名、电话号码)、显示联系⼈信息(重写description方法, 打印所有成员变量)。
2、在main.m中定义字典,分组管理所有联系人。分组名 为26个大写的英文字母。
3、可以添加联系人对象,如果姓名或电话号码为空,添加失败(NSLog错误信息)。添加联系⼈到匹配的分组。
4、删除某个分组的全部联系⼈。
*/
// 2.创建 联系人字典(字典套数组)
NSMutableDictionary *contactDic = [NSMutableDictionary dictionary];
// 循环'A'->'Z'
for (char i = 'A'; i <= 'Z'; i++) {
// key
NSString *key = [NSString stringWithFormat:@"%c", i];
// value
NSMutableArray *arr = [NSMutableArray array];
// 添加到字典
[contactDic setObject:arr forKey:key];
// [contactDic setObject:[NSMutableArray array] forKey:[NSString stringWithFormat:@"%c", i]];
}
NSLog(@"%@", contactDic);
// 3.添加
Contact *c1 = [Contact contactWithName:@"Dawa" phone:@"123"];
Contact *c2 = [Contact contactWithName:@"Tuige" phone:@"223"];
Contact *c3 = [Contact contactWithName:@"Beta" phone:@"333"];
Contact *c4 = [Contact contactWithName:@"Suibian" phone:@""];
// 构建数组
NSArray *cArr = @[c1, c2, c3, c4];
// 遍历
for (Contact *c in cArr) {
// 判断
if (c.name.length == 0 || c.phone.length == 0) {
NSLog(@"添加失败");
} else {
// 成功
// 获取key
NSString *key = [c.name substringToIndex:1];
// 获取数组
NSMutableArray *arr = [contactDic objectForKey:key];
// 添加
[arr addObject:c];
// [[contactDic objectForKey:[c.name substringToIndex:1]] addObject:c];
}
}
// 删除
[contactDic removeObjectForKey:@"A"];
NSLog(@"%@", contactDic);
endif
return 0;
}
//
// Book.h
// OC6_练习
//
// Created by YIem. on 12/1/15.
// Copyright (c) 2015 www.yiem.net YIem的博客. All rights reserved.
//
import <Foundation/Foundation.h>
@interface Book : NSObject
@property (nonatomic, copy) NSString bookName;/< 书名 /
@property (nonatomic, assign) CGFloat price; /*< 价格 /
// 初始化
- (instancetype)initWithBookName:(NSString *)bookName price:(CGFloat)price;
// 便利构造器 - (instancetype)bookWithBookName:(NSString *)bookName price:(CGFloat)price;
@end
//
// Book.m
// OC6_练习
//
// Created by YIem. on 12/1/15.
// Copyright (c) 2015 www.yiem.net YIem博客. All rights reserved.
//
import "Book.h"
@implementation Book
pragma mark - 初始化
(instancetype)initWithBookName:(NSString *)bookName price:(CGFloat)price
{
self = [super init];
if (self) {self.bookName = bookName; self.price = price;
}
return self;
}
pragma mark - 便利构造器
- (instancetype)bookWithBookName:(NSString *)bookName price:(CGFloat)price
{
Book *b = [[Book alloc] initWithBookName:bookName price:price];
return b;
}
pragma mark - 描述方法
- (NSString *)description
{
return [NSString stringWithFormat:@"name: %@, price: %.2f", self.bookName, self.price];
}
@end
//
// Libaray.h
// OC6_练习
//
// Created by YIem. on 12/1/15.
// Copyright (c) 2015 www.yiem.net YIem的博客. All rights reserved.
//
import <Foundation/Foundation.h>
// 引用类型
@class Book;
@interface Libaray : NSObject
@property (nonatomic, retain) NSMutableArray *bookArr;// 书本数组
/// 添加
- (void)addBook:(Book *)book;
/// 删除 - (void)removeBookAtIndex:(NSUInteger)index;
/// 修改价格 - (void)changePriceByName:(NSString *)name price:(CGFloat)price;
/// 打印清单 - (void)printBookList;
@end
//
// Libaray.m
// OC6_练习
//
// Created by YIem. on 12/1/15.
// Copyright (c) 2015 www.yiem.net YIem博客. All rights reserved.
//
import "Libaray.h"
import "Book.h"
@implementation Libaray
pragma mark - 初始化
(instancetype)init
{
self = [super init];
if (self) {// 可变数组初始化 self.bookArr = [NSMutableArray array];
}
return self;
}
pragma mark - 添加
(void)addBook:(Book *)book
{[self.bookArr addObject:book];
}
pragma mark - 删除
(void)removeBookAtIndex:(NSUInteger)index
{[self.bookArr removeObjectAtIndex:index];
}
pragma mark - 改价格
(void)changePriceByName:(NSString *)name price:(CGFloat)price
{
// 遍历数组
for (Book *b in self.bookArr) {// 判断 if ([b.bookName isEqualToString:name]) { // 修改 b.price = price; }
}
}
pragma mark - 清单
(void)printBookList
{
for (Book *b in self.bookArr) {NSLog(@"%@", b.bookName);
}
}
@end
//
// Contact.h
// OC6_练习
//
// Created by YIem. on 12/1/15.
// Copyright (c) 2015 www.yiem.net YIem的博客. All rights reserved.
//
import <Foundation/Foundation.h>
@interface Contact : NSObject
{
NSString *_name;
}
@property (nonatomic, copy) NSString name;/< 姓名 /
@property (nonatomic, copy) NSString sex;/< 性别 /
@property (nonatomic, copy) NSString phone;/< 电话号 /
@property (nonatomic, copy) NSString address;/< 地址 /
@property (nonatomic, copy) NSString groupName;/< 分组名 /
@property (nonatomic, assign) NSInteger age;/*< 年龄 /
// 初始化
- (instancetype)initWithName:(NSString )name phone:(NSString )phone;
// 便利构造器 - (instancetype)contactWithName:(NSString )name phone:(NSString )phone;
@end
//
// Contact.m
// OC6_练习
//
// Created by YIem. on 12/1/15.
// Copyright (c) 2015 www.yiem.net YIem的博客. All rights reserved.
//
import "Contact.h"
@implementation Contact
warning 同时重写getter和setter 不会自动产生成员变量 需要重新设置synthesize
//@synthesize name = _name;
pragma mark - 初始化
(instancetype)initWithName:(NSString )name phone:(NSString )phone
{
self = [super init];
if (self) {self.name = name; self.phone = phone;
}
return self;
}- (void)setName:(NSString *)name
{
_name = name;
} - (NSString *)name
{
return _name;
}
pragma mark - 便利构造器
- (instancetype)contactWithName:(NSString )name phone:(NSString )phone
{
Contact *c = [[Contact alloc] initWithName:name phone:phone];
return c;
}
pragma mark - 描述方法
- (NSString *)description
{
return [NSString stringWithFormat:@"name: %@, phone: %@, sex: %@, address: %@, group: %@, age: %ld", self.name, self.phone, self.sex, self.address, self.groupName, self.age];
}
@end