Gold Solution but a few questions


#1

Here is my Gold Solution

#import "YANItem.h"

@interface YANContainer : YANItem
{
    NSMutableArray *_subItems;
}

// Designated initializer for YANContainer
- (instancetype) initWithContainerName:(NSString *)name
                   valueinDollars:(int)value
                     serialNumber:(NSString *)sNumber
                         itemList:(NSMutableArray *)list;

- (void)setSubItems:(NSMutableArray *)itemList;
- (NSMutableArray *)subItems;

- (void)addItem:(YANItem *)item;

- (void)delItem:(YANItem *)item;

- (int)fullValue;

- (NSString *)showItems;


@end

[code]#import “YANContainer.h”

@implementation YANContainer

// Initializers

  • (instancetype) initWithContainerName:(NSString *)name
    valueinDollars:(int)value
    serialNumber:(NSString *)sNumber
    itemList:(NSMutableArray *)list
    {
    _itemName = name;
    _valueInDollars = value;
    _serialNumber = sNumber;
    _subItems = list;
    _dateCreated = [[NSDate alloc] init];

    return self;
    }

  • (instancetype)initWithItemName:(NSString *)name
    valueinDollars:(int)value
    serialNumber:(NSString *)sNumber
    {
    return [self initWithContainerName:name valueinDollars:value serialNumber:sNumber itemList:[[NSMutableArray alloc] init]];
    }

// Accessor methods

  • (void)setSubItems:(NSMutableArray *)itemList
    {
    _subItems = itemList;
    }

  • (NSMutableArray *)subItems
    {
    return _subItems;
    }

// Methods to add or delete items from de container

  • (void)addItem:(YANItem *)item
    {
    [self.subItems addObject:item];
    }

  • (void)delItem:(YANItem *)item
    {
    [self.subItems removeObject:item];
    }

// Methods to show the total value and the list of items in the container

  • (int)fullValue
    {
    int totalValue = self.valueInDollars;

    if ([self.subItems count] != 0) {
    for (YANItem *item in self.subItems)
    {

          totalValue += item.valueInDollars;
    
      }
    

    }
    else {
    return totalValue;
    }

    return totalValue;
    }

  • (NSString *)showItems
    {
    NSString *list = @“Items in the container:\n”;
    if ([self.subItems count] != 0) {
    int i = 0;
    for (YANItem *item in self.subItems)
    {
    i++;
    list = [list stringByAppendingFormat:@"%d.- %@\n", i, item];
    }
    }
    else {
    list = [list stringByAppendingFormat:@“The container has no items\n”];
    }

    return list;
    }

// Override description method

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

    return descriptionString;
    }

@end
[/code]

I have two problems.

If the items of my container are containers the value of my main container only shows as the total value the empty containers, and not the total value including the items of every container plus the values of the empty containers.

I was thinking in override the accessor get method of valueInDollars but this will have a problem with my set method cause any user of this class could change the current value and the get method will not show the same value.

The other solution that I thought was to change my fullValue method:

- (int)fullValue
{
    int totalValue = self.valueInDollars;
    
    if ([self.subItems count] != 0) {
        
        
        for (YSItem *item in self.subItems)
        {
            
            if ([item isKindOfClass:[YANContainer class]]) {
                totalValue += [item fullValue];
            }
            else {
                totalValue += item.valueInDollars;
            }
        }
    }
    
    return totalValue;
}

But this doesn’t work because the YANItem doesn’t have the fullValue method. If I change my fast enumeration to an id pointer everything works fine, but I think this leaves a lot of problems because subItems can have any type of objects and this method thinks that every object in the array have a fullValue method. The other solution using id is to try to catch the error in the init and addItem methods to see if the items in the array are only YANItems and if not reject the items.

I don’t know if I’m doing it right, or maybe I’m overthinking the problem :S

Thanks in advance