My Ch2 Golden Challenge


#1

My first challenge on this book. Discuss welcomed. :wink:

[code]//
// BNRItem.h
// RandomPossesions
//
// Created by Roy Law on 13-5-28.
//

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
{
NSString *itemName;
NSString *serialNumber;
int valueInDollars;
NSDate *dateCreated;

}

-(id)initWithItemName:(NSString *)aItemName serialNumber:(NSString *)aSerialNumber valueInDollars:(int)aValueInDollars;
-(id)initWithItemName:(NSString *)aItemName;

-(void)setItemName:(NSString *)newItemName;
-(NSString *)itemName;

-(void)setSerialNumber:(NSString *)newSerialNumber;
-(NSString *)serialNumber;

-(void)setValueInDollars:(int)newValueInDollars;
-(int)valueInDollars;

-(NSDate *)dateCreated;

+(id)randomItem;

@end
[/code]

[code]//
// BNRItem.m
// RandomPossesions
//
// Created by Roy Law on 13-5-28.
//

#import “BNRItem.h”

@implementation BNRItem

-(id)init
{
return [self initWithItemName:@“NO_NAME” serialNumber:@“NO_SN” valueInDollars:0];
}

-(id)initWithItemName:(NSString *)aItemName
serialNumber:(NSString *)aSerialNumber
valueInDollars:(int)aValueInDollars
{
self = [super init];
if(self)
{
self.itemName = aItemName;
self.serialNumber = aSerialNumber;
self.valueInDollars = aValueInDollars;

    dateCreated = [[NSDate alloc] init];
}
return self;

}

-(id)initWithItemName:(NSString *)aItemName
{
return [self initWithItemName:aItemName serialNumber:@"" valueInDollars:0];
}

-(void)setItemName:(NSString *)newItemName
{
itemName = newItemName;
}
-(NSString *)itemName
{
return itemName;
}

-(void)setSerialNumber:(NSString *)newSerialNumber
{
serialNumber = newSerialNumber;
}
-(NSString *)serialNumber
{
return serialNumber;
}

-(void)setValueInDollars:(int)newValueInDollars
{
valueInDollars = newValueInDollars;
}
-(int)valueInDollars
{
return valueInDollars;
}

-(NSDate *)dateCreated
{
return dateCreated;
}

-(NSString *)description
{
return [[NSString alloc] initWithFormat:@“NAME:%@ SN:%@ VALUE:%d DATE:%@”, itemName, serialNumber ,valueInDollars, dateCreated];
}

+(id)randomItem
{
NSArray *randomAdjList = [NSArray arrayWithObjects:@“Fluffy”, @“Shiny”, @“Rusty”, nil];
NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”, @“Spork”, @“Mac”, nil];

NSInteger adjIndex = rand()%randomAdjList.count;
NSInteger nounIndex = rand()%randomNounList.count;

NSString *name=[NSString stringWithFormat:@"%@ %@",randomAdjList[adjIndex], randomNounList[nounIndex]];

int randomValue = rand()%100;

NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",'0'+rand()%10,'A'+rand()%26,'0'+rand()%10,'A'+rand()%26,'0'+rand()%10];

BNRItem *newItem = [[self alloc] initWithItemName:name serialNumber:randomSerialNumber valueInDollars:randomValue];

return newItem;

}

@end
[/code]

[code]//
// main.m
// RandomPossesions
//
// Created by Roy Law on 13-5-28.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “BNRItem.h”
#import “BNRContainer.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    // insert code here...
    BNRContainer *aBNRContainer = [[BNRContainer alloc] init];
    
    aBNRContainer.contName = @"aContainer";
    
    [aBNRContainer setSubItems:[NSMutableArray arrayWithObjects:[BNRItem randomItem], [BNRItem randomItem], [BNRItem randomItem], nil]];
    
    [aBNRContainer addSubItem:[BNRItem randomItem]];
    
    NSLog(@"%@", aBNRContainer);
}
return 0;

}

[/code]

[code]//
// BNRContainer.h
// RandomPossesions
//
// Created by Roy Law on 13-5-28.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “BNRItem.h”

@interface BNRContainer : BNRItem
{
NSString *contName;
int contValueInDollars;
NSMutableArray *subItems;
}

-(void)setContName:(NSString *)aContName;
-(NSString *)contName;

-(void)setContValueInDollars;
-(int)contValueInDollars;

-(void)addSubItem:(id)aSubItem;
-(void)setSubItems:(NSMutableArray *)newSubItems;
-(id)subItems;
@end

[/code]

[code]//
// BNRContainer.m
// RandomPossesions
//
// Created by Roy Law on 13-5-28.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import “BNRContainer.h”

@implementation BNRContainer

-(void)setContName:(NSString *)aContName
{
contName = aContName;
}
-(NSString *)contName
{
return contName;
}

-(void)addSubItem:(id)aSubItem
{
[subItems addObject];
[self setContValueInDollars];
}

-(void)setSubItems:(NSMutableArray *)newSubItems
{
subItems = newSubItems;
[self setContValueInDollars];
}
-(id)subItems
{
return subItems;
}

-(NSString *)description
{
return [[NSString alloc] initWithFormat:@“Container With Name ‘%@’ Value’%d’and Items ‘%@’”, contName, contValueInDollars, subItems];
}

-(void)setContValueInDollars
{
contValueInDollars = 0;
for (int i =0; i<subItems.count; i++) {
contValueInDollars += [subItems[i] valueInDollars];
}
}

-(int)contValueInDollars
{
return contValueInDollars;
}

@end

[/code]

CMD OUTPUT

[code]2013-05-28 17:08:59.658 RandomPossesions[1149:303] Container With Name ‘aContainer’ Value’252’and Items ‘(
“NAME:Shiny Spork SN:8Q2U8 VALUE:73 DATE:2013-05-28 09:08:59 +0000”,
“NAME:Rusty Spork SN:5Y2V3 VALUE:40 DATE:2013-05-28 09:08:59 +0000”,
“NAME:Shiny Spork SN:2F9Z7 VALUE:40 DATE:2013-05-28 09:08:59 +0000”,
“NAME:Shiny Bear SN:8G5V6 VALUE:99 DATE:2013-05-28 09:08:59 +0000”
)’

[/code]