Gold Challenge Chapter 2


#1

[code][///
// main.m
// Golden_chap2
//
// Created by Siavash on 7/19/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

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

@autoreleasepool {
    

    BNRItem *p1=[[BNRItem alloc] initWithName:@"Sofa" serialNumber:@"Ds2S4" valueInDollar:123];
    BNRItem *p2=[[BNRItem alloc] initWithName:@"Table" serialNumber:@"WE4r5" valueInDollar:23];
    BNRItem *p3=[[BNRItem alloc] initWithName:@"Chair" serialNumber:@"R4DF6" valueInDollar:99];
    BNRContainer *con1=[[BNRContainer alloc] initWithContainerName:@"First" valueInDollars:12];
    [con1 addItem:p1];
    [con1 addItem:p2];
    [con1 addItem:p3];
    [con1 list];
   
    
}
return 0;

}]

[//
// BNRItem.h
// RandomPossession
//
// Created by Siavash on 7/18/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
@property int valueInDollar;
@property (nonatomic,copy) NSString *itemName,*serialNumber;
@property NSDate *dateCreated;

  • (id) randomItem;
  • (id) initWithName:(NSString *)ItemName serialNumber:(NSString *)sNumber valueInDollar:(int)i;

@end][///
// BNRItem.m
// RandomPossession
//
// Created by Siavash on 7/18/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “BNRItem.h”

@implementation BNRItem

@synthesize itemName,serialNumber,valueInDollar,dateCreated;

  • (id) initWithName:(NSString *)ItemName serialNumber:(NSString *)sNumber valueInDollar:(int)i
    {
    self=[super init];
    if(self){
    [self setItemName];
    [self setSerialNumber:sNumber];
    [self setValueInDollar:i];
    dateCreated=[[NSDate alloc] init];
    }
    return self;
    }
  • (id) init
    {
    return [self initWithName:@“Item” serialNumber:@" " valueInDollar:0];
    }
  • (id) randomItem
    {
    NSArray *randomAdjectiveList=[NSArray arrayWithObjects:@“Fluffy”,@“Rusty”,@“Shine”, nil];
    NSArray *randomNounList=[NSArray arrayWithObjects:@“Bear”,@“Spork”,@“Mac”, nil];
    NSInteger adjectiveIndex=rand() % [randomAdjectiveList count];
    NSInteger nounIndex=rand() % [randomNounList count];
    NSString *randomName=[NSString stringWithFormat:@" %@ %@",[randomNounList objectAtIndex:nounIndex],[randomAdjectiveList objectAtIndex:adjectiveIndex]];
    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] initWithName:randomName serialNumber:randomSerialNumber valueInDollar:randomValue];
    return newItem;

}

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

@end]
[//
// BNRContainer.h
// Golden_chap2
//
// Created by Siavash on 7/19/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

@interface BNRContainer : BNRItem
@property NSMutableArray *containerElement;
@property NSString *containerName;
@property int value;

  • (id) initWithContainerName:(NSString *)n valueInDollars:(int)i;
  • (void) addItem:(id) Item;
  • (void) list;

@end
][///
// BNRContainer.m
// Golden_chap2
//
// Created by Siavash on 7/19/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “BNRContainer.h”
#import “BNRItem.h”

@implementation BNRContainer
{
int totalValue;
int d;
}
@synthesize containerElement,value,containerName;

  • (id) initWithContainerName:(NSString *)n valueInDollars:(int)i
    {
    self=[super init];

    if (self) {
    containerElement=[NSMutableArray array];
    containerName=[NSString stringWithString:n];
    [self setValue:i];
    [self setContainerName:n];

    }
    return self;
    }

  • (void) addItem:(id)Item
    {
    [containerElement addObject:Item];
    }

  • (void) list
    {
    totalValue=value;
    d=1;
    NSLog(@"=========================Container:%@===========================",containerName);

    for(BNRItem *ele in containerElement)
    {

      NSLog(@"|%i) %@|" ,d,ele);
      NSLog(@"-------------------------------------------------------------------");
      totalValue+=ele.valueInDollar;
      d++;
    

    }

    NSLog(@" |Container value:%i Container Total price:%i|",value,totalValue);
    NSLog(@" ---------------------------------------------------- ");

}]

2012-07-19 11:12:30.628 Golden_chap2[9336:403] =========================Container:First===========================
2012-07-19 11:12:30.632 Golden_chap2[9336:403] |1) Sofa (Ds2S4): Worth $123, recorded on 2012-07-19 06:42:30 +0000|
2012-07-19 11:12:30.633 Golden_chap2[9336:403] -------------------------------------------------------------------
2012-07-19 11:12:30.633 Golden_chap2[9336:403] |2) Table (WE4r5): Worth $23, recorded on 2012-07-19 06:42:30 +0000|
2012-07-19 11:12:30.634 Golden_chap2[9336:403] -------------------------------------------------------------------
2012-07-19 11:12:30.635 Golden_chap2[9336:403] |3) Chair (R4DF6): Worth $99, recorded on 2012-07-19 06:42:30 +0000|
2012-07-19 11:12:30.635 Golden_chap2[9336:403] -------------------------------------------------------------------
2012-07-19 11:12:30.635 Golden_chap2[9336:403] |Container value:$12 Container Total price:$257|
2012-07-19 11:12:30.636 Golden_chap2[9336:403] ----------------------------------------------------


#2

Being more of a noob, I very much enjoyed reading this code. Very easy to follow.

I am wondering about the requirement that the container should be able to hold other containers. Maybe this needs a method that stores or calculates the value of the container rather than doing it in main.m.

Otherwise in main, I think you need to check if the element is a container itself and calculate the values for it in order to add it to the total.


#3

Because BNRContainer is (per the challenge) supposed to inherit BNRItem, it must answer to the same messages that its parent does.

So, if you send the message valueInDollars to a container, it should (I would argue must) return the sum of all nested items, including any other containers.

The advantage is that any block of code that expects a BNRItem object will be perfectly happy to take a BNRContainer object instead, without ever knowing the difference.

Thus, in pseudocode:

BNRItem i1;
BNRItem i2;
BNRContainer c1;
BNRContainer c2;

[i1 setValueInDollar:3.0];
[i2 setValueInDollar:5.3];
[c1 addItem:i1];
[c2 addItem:i2];
[c2 addItem:c1];

[bragAbout i1];
[bragAbout i2];
[bragAbout c1];
[bragAbout c2];

void bragAbout:BNRItem i
{
  NSLog(@"I'm worth %0.2f bucks", [i valueInDollars]);
}

You should see the following output:

I'm worth 3.00 bucks
I'm worth 5.30 bucks
I'm worth 3.00 bucks
I'm worth 8.30 bucks