Rookie's solution to Gold Challenge. Pls give me feedback


#1

Hello everyone,
I am very new at this language and also framework. Wasn’t a C programmer before.
I have to re-write a successful commercial application to work on iPhone and iPad as fast as I can.
Attached is the Gold challenge of this chapter and it works.
Please help me if you can. I truly appreciate if anyone can tell me the mistakes I have made or ways I have done things inefficiently.

My main class:

#import <Foundation/Foundation.h>
#import "ePxItem.h"
#import "ePxContainer.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {     
        ePxContainer *container = [[ePxContainer alloc] initWithItemName:@"First Container" valueInDollars:280 numberOfItemsInContainer:10];     
        NSLog(@"%@", container);       
        container = nil;
    }
    return 0;
}

BNRItem Header:

#import <Foundation/Foundation.h>

@interface ePxItem : NSObject

{
    NSString * _itemName;
    NSString * _serialNumber;
    int _valueInDollars;
    NSDate * _dateCreated;
}

+ (instancetype) randomItem;

- (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

BNRItem Implementation:

#import "ePxItem.h"

@implementation ePxItem

+ (instancetype) randomItem
{
    NSArray * randomAdjectiveList = @[@"Fluffy", @"Rusty", @"Shiny"];
    NSArray * randomNounList = @[@"Bear", @"Spork", @"Mac"];
    
    NSInteger adjectiveIndex = arc4random() % [randomAdjectiveList count];
    NSInteger nounIndex = arc4random() % [randomNounList count];
    
    NSString * randomName = [NSString stringWithFormat:@"%@ %@",
                             [randomAdjectiveList objectAtIndex:adjectiveIndex],
                             [randomNounList objectAtIndex: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];
    
    ePxItem * newItem = [[ePxItem alloc] initWithItemName:randomName
                                           valueInDollars:randomValue
                                             serialNumber:randomSerialNumber];
    
    return newItem;
}


//Designated initializer for ePxItem class
- (instancetype) initWithItemName: (NSString *) name
                   valueInDollars: (int) value
                     serialNumber: (NSString *) sNumber ;
{
    self = [super init];
    
    if (self)
    {
        _itemName = name;
        _valueInDollars = value;
        _serialNumber = sNumber;
        _dateCreated = [[NSDate alloc] init];
    }
    
    return self;
}

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

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

- (instancetype) init
{
    return [self initWithItemName:@"Item"];
}

- (void) setItemName: (NSString *)str
{
    _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;
    
    descriptionString = [[NSString alloc] initWithFormat:@"%@ (%@): worth $%d, recorded on %@",
     self.itemName, self.serialNumber, self.valueInDollars, self.dateCreated];
    
    return descriptionString;
}

@end

BNRContainer Header:

#import "ePxItem.h"

@interface ePxContainer : ePxItem
{
    NSMutableArray * _containerItems;
    int _containerItemCount;
}

- (instancetype) initWithItemName:(NSString *)name
                   valueInDollars:(int) value
         numberOfItemsInContainer:(int) nItems;

- (int) containerItemCount;
- (NSMutableArray *) containerItems;

- (ePxItem *) getItemFromContainer: (int) index;

- (int) getFullDollarValue;

@end

BNRContainer Implementation:

#import "ePxContainer.h"

@implementation ePxContainer

//Designated initializer for ePxItem class
- (instancetype) initWithItemName:(NSString *)name
                   valueInDollars:(int) value
         numberOfItemsInContainer:(int) nItems;
{
    self = [super initWithItemName:name valueInDollars:value serialNumber:@""];
    
    if (self)
    {
        _containerItemCount = nItems;
        _containerItems = [[NSMutableArray alloc] init];
        for (int i=0; i<= _containerItemCount; i++)
        {
            [_containerItems addObject:[ePxItem randomItem]];
        }
    }
    
    return self;
}

- (instancetype) initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber
{
    return [self initWithItemName:name valueInDollars:value numberOfItemsInContainer:1];
}

- (instancetype) initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber
{
    return [self initWithItemName:name valueInDollars:1 numberOfItemsInContainer:1];
}

- (instancetype) initWithItemName:(NSString *)name
{
    return [self initWithItemName:name valueInDollars:1 numberOfItemsInContainer:1];
}

- (instancetype) init
{
    return [self initWithItemName:@"Container" valueInDollars:1 numberOfItemsInContainer:1];
}

- (int) containerItemCount
{
    return _containerItemCount;
}

- (NSMutableArray *) containerItems
{
    return _containerItems;
}

- (ePxItem *) getItemFromContainer: (int) index
{
    return [self.containerItems objectAtIndex:index];
}

- (int) getFullDollarValue
{
    int value = 0;
    
    for (int i=0;i<= self.containerItemCount;i++)
    {
        value = value + [self.containerItems[i] valueInDollars];
    }
    
    return value;
}

- (NSString *) description
{
    NSString * descriptionString;
    NSMutableArray * descriptionArray;
    
    descriptionArray = [[NSMutableArray alloc] init];
    
    descriptionString = [[NSString alloc] initWithFormat:@"\nContainer %@ holds %d items, valued at $%d, recorded on %@",
                         self.itemName, self.containerItemCount, self.valueInDollars, self.dateCreated];
    
    [descriptionArray addObject:descriptionString];
    
    for (int i=0; i<= self.containerItemCount; i++)
    {
        
        [descriptionArray addObject:[[self getItemFromContainer:i] description]];
    }
    
    descriptionString = [[NSString alloc] initWithFormat:@"\n Sum of all dollar amounts are: $%d",
                         self.getFullDollarValue];
    
    [descriptionArray addObject:descriptionString];
    
    descriptionString = [descriptionArray componentsJoinedByString:@"\n"];
    
    return descriptionString;
}
@end