Gold Challenge:Another Class


#1

BNRContainer.h

[code]#import “BNRItem.h”

@interface BNRContainer : BNRItem
@property (nonatomic,strong)NSMutableArray *subitems;

  • (NSString *)description;
    @end
    [/code]

BNRContainer.m

[code]@implementation BNRContainer

  • (NSString *)description
    {
    NSString *string = [[NSString alloc] init];

    int sumtalto = 0;

      NSMutableArray *array = [self subitems];
      for (int i = 0;i < [array count];i++) {
          
          sumtalto += [array[i] valueInDollars] + [self valueInDollars];
         
      }
      string = [NSString stringWithFormat:@"BNRItem and BNRContainer is total %d\n %@ ",sumtalto,array];
      return string ;
    

}
@end

[/code]

BNRItem.h

[code]#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
{
NSString *_itemName;
NSString *_serialNumber;
int _valueInDollars;
NSDate *_dateCreated;

}

  • (instancetype)randomItem;
    //BNRItem类的指定初始化方法
  • (instancetype)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber;

  • (instancetype)initWithItemName:(NSString *)name;

  • (instancetype)initWithItemName:(NSString *)name
    serialNumber:(NSString *)sNumber;

  • (void)setItemName:(NSString *)str;

  • (NSString *)itemName;

  • (void)setSerialNumber:(NSString *)str;

  • (NSString *)serialNumber;

  • (void)setValueInDollars:(int)v;

  • (int)valueInDollars;

  • (NSDate *)dateCreated;
    @end
    [/code]

BNRItem.m

[code]#import “BNRItem.h”

@implementation BNRItem

  • (instancetype)randomItem
    {
    //创建不可变数组对象,包含三个形容词
    NSArray *randomAdjectiveList = @[@“Fluffy”,@“Rusty”,@“Shiny”];

    //创建不可变数组对象,包含三个名词

    NSArray *randomNounList = @[@“Bear”,@“Spork”,@“Mac”];

    //根据数组对象所含对象的个数,得到随机索引
    //注意:运算符%是模运算符,运算后得到的是余数
    //因此adjectiveIndex 是一个0到2(包括2)的随机数

    NSInteger adjectiveIndex = arc4random() % [randomAdjectiveList count];
    NSInteger nounIndex = arc4random() % [randomNounList count];

    //注意,类型为NSInteger的变量不是对象
    //NSInteger是一种针对unsigned long(无符号长整数)的类型定义

    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
    randomAdjectiveList[adjectiveIndex],
    randomNounList[nounIndex]];

    int randomValue = arc4random() % 100;

    NSString *randomserialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
    ‘0’ + arc4random() % 10,
    ‘A’ + arc4random() % 26,
    ‘0’ + arc4random() % 10,
    ‘A’ + arc4random() % 26,
    ‘0’ + arc4random() % 10];

    BNRItem *newItem = [[self alloc] initWithItemName:randomName
    valueInDollars:randomValue
    serialNumber:randomserialNumber];

    return newItem;

}

  • (instancetype)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber
    {
    //调用父类的指定初始化方法
    self = [super init];

    //父类的初始化方法是否成功创建了父类对象
    if (self) {
    //为实例变量设定初始值

      _itemName = name;
      _valueInDollars = value;
      _serialNumber = sNumber;
      
      //设置_dateCreated 的值为系统当前时间
      _dateCreated = [[NSDate alloc] init];
    

    }

    //返回初始化后对象的新地址

    return self;

}

  • (instancetype)initWithItemName:(NSString *)name
    {
    return [self initWithItemName:name
    valueInDollars:0
    serialNumber:@""];

}

  • (instancetype)init
    {
    return [self initWithItemName:@“Item”];

}

  • (instancetype)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber
    {
    return [self initWithItemName:name
    valueInDollars:0
    serialNumber:sNumber];

}

  • (void)setItemName:(NSString *)str
    {
    _itemName = str ;//与self.itemName = str ;有什么区别?

}

  • (NSString *)itemName
    {
    return _itemName;

}

  • (void)setSerialNumber:(NSString *)str
    {

    _serialNumber = str;

}

  • (NSString *)serialNumber
    {
    return _serialNumber;
    }

  • (void)setValueInDollars:(int)v
    {
    _valueInDollars = v;
    }

  • (int)valueInDollars
    {
    return _valueInDollars;
    }

  • (NSDate *)dateCreated
    {
    return _dateCreated;
    }

  • (NSString *)description
    {
    NSString *descriptionString =
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d,recorded on %@",
    self.itemName,
    self.serialNumber,
    self.valueInDollars,
    self.dateCreated ];

    return descriptionString;
    }
    @end
    [/code]

main.m

[code]#import <Foundation/Foundation.h>
#import "BNRContainer.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {

    //初始化BNRContainer
    BNRContainer *items = [[BNRContainer alloc] init];
    NSMutableArray *array = [[NSMutableArray alloc] init] ;
    for (int i = 0; i < 10; i++) {
        BNRItem *item = [BNRItem randomItem];
        [array addObject:item];
    }
    //将数组array 赋值给 BNRContainer的subitems对象
    items.subitems = array;
           NSLog(@"%@",items);
    //释放items所指向的NSMutableArray对象
    items = nil;
   
}
return 0;

}
[/code]