My Silver Challenge Solution


#1

Here is my code for the silver challenge.
If there any errors or problems, please let me know :slight_smile:

main.m

[code]@autoreleasepool {

    // Create a mutable array object, store its address in items variable
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    /*BNRItem *item = [[BNRItem alloc]initWithItemName:@"Red Sofa" valueInDollars:100 serialNumber:@"A1B2C"];
    NSLog(@"%@", item);
     */
    BNRItem *newItem = [[BNRItem alloc]initWithItemName:@"Black Office Chair" serialNumber:@"X7Y9Z"];
    NSLog(@"%@", newItem);
    
    // Destroy the mutable array object
    items = nil;
    
}
return 0;

[/code]

BNRItem.h

[code]#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
{
NSString *_itemName;
NSString *_serialNumber;
int _valueInDollars;
NSDate *_dateCreated;
}

// Designated initializer for BNRItem

  • (instancetype)initWithItemName: (NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber;

  • (instancetype)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber;
    //- (instancetype)initWithItemName:(NSString *)name;

  • (void)setItemName: (NSString *)str;

  • (NSString *)itemName;

  • (void)setSerialNumber: (NSString *)str;

  • (NSString *)serialNumber;

  • (void)setValueInDollars: (int)v;

  • (int)valueInDollars;

  • (NSDate *)dateCreated;

@end
[/code]

BNRItem.m

[code]#import “BNRItem.h”

@implementation BNRItem

  • (instancetype) initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber
    {
    // Call the superclass’s designated initializer
    self = [super init];

    // Did the superclass’s designated initializer succeed?
    if (self) {
    // Give the instance variable initial values
    _itemName = name;
    _serialNumber = sNumber;
    _valueInDollars = value;
    // Set _dateCreated to the current date and time
    _dateCreated = [[NSDate alloc] init];
    }
    // Return the address of the newly initialized object
    return self;
    }

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

  • (NSString *)description
    {
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"\nItem:%@ \nSerial number:%@", self.itemName, self.serialNumber];
    return descriptionString;
    }

  • (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;
    }

@end
[/code]