My Gold Challenge solution. feedback is highly appreciated!


#1

Here is my solution.

this is my first post, any feedback and comments are highly appreciated

BNRContainer.h

#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
    NSMutableArray *_containerList;
    NSString *_containerName;
    int _containerValue;
}
    //designated initializer
- (instancetype)initWithContainerName:(NSString *)name;

    //_containerName accessor
- (void)setContainerName:(NSString *)name;
- (NSString *)containerName;

    //_containerValue accessor
- (int)containerValue;

    //Instance methods
- (void)addItem:(id)anItem;
- (void)containerSum:(id)item;

@end

I am having difficult to implement the description method to display the contents of the container object in the container.

any suggestions?

BNRContainer.m

#import "BNRContainer.h"

@implementation BNRContainer

- (instancetype)initWithContainerName:(NSString *)name
{
    if (self = [super init]) {
        _containerName = name;
        _containerValue = 0;
        _containerList = [[NSMutableArray alloc] init];
    }
    return self;
}

- (instancetype)init
{
    return [self initWithContainerName:@"unknow"];
}

- (void)setContainerName:(NSString *)name
{
    _containerName = name;
}

- (NSString *)containerName
{
    return _containerName;
}

- (void)setContainerValue:(int)value;
{
    _containerValue = value;
}
- (int)containerValue;
{
    return _containerValue;
}

- (void)setContainerList:(NSArray *)theContainer
{
    _containerList = [theContainer mutableCopy];
}

- (NSArray *)containerList;
{
    return [_containerList copy];
}

- (void)addItem:(id)anItem
{
    if (! self.containerList) {
        self.containerList = [[NSMutableArray alloc] init];
    }
        //must use the ivar to add item to the container
    [_containerList addObject:anItem];
    [self containerSum:anItem];
}

- (void)containerSum:(id)item;
{
        //check the class type before adding the value to the containerValue
    switch ([item isKindOfClass:[BNRContainer class]]) {
        case YES:
            self.containerValue += [item containerValue];
            break;
            
        case NO:
            self.containerValue += [item valueInDollars];            
            break;
            
        default:
            break;
    }
    
}
- (NSString *)description
{
    NSString *descriptionSting = [[NSString alloc] initWithFormat:@"<%@> Total Value $%d containing %@", self.containerName, self.containerValue, self.containerList];

    return descriptionSting;
}

@end

main.m

#import <Foundation/Foundation.h>

    //BNRContainer inherit from it's superclass BNRItem no need to import BNRItem header
#import "BNRContainer.h"

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

    @autoreleasepool {
        
        NSMutableArray *items = [[NSMutableArray alloc] init];
        
            // instance of container class
        BNRContainer *Inventory = [[BNRContainer alloc] initWithContainerName:@"Home Inventory"];
        BNRContainer *newInventory = [[BNRContainer alloc] initWithContainerName:@"Global Inventory"];
        
            //create item with random information and populate container
        for (int i = 0; i < 10; i++) {
            BNRItem *item = [BNRItem randomItem];
            [items addObject:item];
            [Inventory addItem:item];
            [newInventory addItem:item];
        }
        
            //add an container object to the container object
        [newInventory addItem:Inventory];
        
            // display
        NSLog(@"%@", Inventory);
        NSLog(@"\n\n\n");
        NSLog(@"%@", newInventory);
        NSLog(@"\n\n\n");
        
        items = nil;
    }
    return 0;
}

#2

[quote][code]#import “BNRItem.h”

@interface BNRContainer : BNRItem
{
NSMutableArray *_containerList;
NSString *_containerName;
int _containerValue;
}
//designated initializer

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

    //_containerName accessor

  • (void)setContainerName:(NSString *)name;

  • (NSString *)containerName;

    //_containerValue accessor

  • (int)containerValue;

    //Instance methods

  • (void)addItem:(id)anItem;

  • (void)containerSum:(id)item;

@end[/code][/quote]
Although the code above is correct, you can improve it by moving the instance variables to the implementation file:

// BNRContainer.m
...
@implementation BNRContainer
{
    NSMutableArray *_containerList;
    NSString *_containerName;
    int _containerValue;
}
...
@end

Then your interface file will shrink in size:

[code]
// BNRContainer.h

#import “BNRItem.h”

@interface BNRContainer : BNRItem

//designated initializer
  • (instancetype)initWithContainerName:(NSString *)name;

    //_containerName accessor

  • (void)setContainerName:(NSString *)name;

  • (NSString *)containerName;

    //_containerValue accessor

  • (int)containerValue;

    //Instance methods

  • (void)addItem:(id)anItem;

  • (void)containerSum:(id)item;

@end[/code]
Small files are not only easier to read by humans, they are also faster to compile.

[Accelerate your learning and become a competent programmer: pretty-function.org]