Silver challenge (works)

#1

[code]//
// BNRItemsViewController.h
// Homepwner
//
// Created by Nelson Capes on 8/25/15.
// Copyright © 2015 Nelson Capes. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BNRItemsViewController : UITableViewController

@end
[/code]

[code]//
// BNRItemsViewController.m
// Homepwner
//
// Created by Nelson Capes on 8/25/15.
// Copyright © 2015 Nelson Capes. All rights reserved.
//

#import “BNRItemsViewController.h”
#import “BNRItemStore.h”
#import “BNRItem.h”

@implementation BNRItemsViewController

-(instancetype)init
{
// call the superclass’s designated initializer
self = [super initWithStyle:UITableViewStyleGrouped];
if (self){
for (int i = 0; i < 10; i++){
[[BNRItemStore sharedStore]createItem];
}
}
return self;
}

-(instancetype)initWithStyle:(UITableViewStyle)Style
{
return [super init];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// the tableView sends the above message to the view controller (self)
// so that the view controller will return the number of sections
// in the tableView. Here, we hard code the number of sections to 2
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// the tableView sends the above message to the view controller (self)
// so that the view controller will return the number of rows in each section
//of the tableView
// calculate the number of rows needed in the tableview for each section
// BNRItems with a valuInDollars < 50 go in the first section, itemValueLt50
// BNRItems with a valueInDollars of greater than or equal to 50 go in
// the second section, itemValueGe50

NSInteger s = section;
if (s == 0){
    BNRItemStore *store = [BNRItemStore sharedStore];
    NSMutableArray *array = [store itemValueLt50];
    NSInteger count = [array count];
    return count;
}
else{
    BNRItemStore *store = [BNRItemStore sharedStore];
    NSMutableArray *array = [store itemValueGe50];
    NSInteger count = [array count];
    return count;
}

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// get a new or recycled cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell” forIndexPath:indexPath];
// the tableView sends the view controller (self( the above message with an indexPath
// of section and row so that the view controller will return an initialized cell
// for that section and row to the tableView
//
// set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
// if the section number is 0, we return a cell from the array itemValueLt50 at
// the row passed in the NSPath parameter, otherwise we return a cell
// from the array itemValueGe50

NSIndexPath *path = indexPath;
NSInteger section = path.section;
NSInteger row = path.row;
NSString *noMoreItems = @"No More Items!";

BNRItemStore *store = [BNRItemStore sharedStore];
NSInteger totalCells = [store addToTotalCells];
NSInteger totalItems = [store totalItems];

if (totalCells == totalItems){
    cell.textLabel.text = noMoreItems;
    return cell;
}
else{
    

if (section == 0){
    BNRItemStore *store = [BNRItemStore sharedStore];
    NSMutableArray *array = [store itemValueLt50];
    BNRItem *item = array[row];
    cell.textLabel.text = [item description];
}
else{
    BNRItemStore *store = [BNRItemStore sharedStore];
    NSMutableArray *array = [store itemValueGe50];
    BNRItem *item = array[row];
    cell.textLabel.text = [item description];
}


return cell;
}

}

-(void)viewDidLoad
{
[super viewDidLoad];

[self.tableView registerClass:[UITableViewCell class]
       forCellReuseIdentifier:@"UITableViewCell"];

}

@end
[/code]

[code]//
// BNRItemStore.h
// Homepwner
//
// Created by Nelson Capes on 8/25/15.
// Copyright © 2015 Nelson Capes. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class BNRItem;

@interface BNRItemStore : NSObject

@property (nonatomic, readonly, copy) NSArray *allItems;
@property (nonatomic, copy) NSMutableArray *itemValueLt50;
@property (nonatomic, copy) NSMutableArray *itemValueGe50;
@property (nonatomic) NSInteger totalItems;
@property (nonatomic) NSInteger totalCells;
+(instancetype)sharedStore;
-(BNRItem *)createItem;
-(NSInteger)addToTotalCells;

@end
[/code]

[code]//
// BNRItemStore.m
// Homepwner
//
// Created by Nelson Capes on 8/25/15.
// Copyright © 2015 Nelson Capes. All rights reserved.
//

#import “BNRItemStore.h”
#import “BNRItem.h”
@interface BNRItemStore ()

@property (nonatomic) NSMutableArray *privateItems;

@end

@implementation BNRItemStore

+(instancetype)sharedStore
{
static BNRItemStore *sharedStore;

// do I need to create a shared store?
if(!sharedStore){
    sharedStore = [[self alloc]initPrivate];
}
return sharedStore;

}
// if a programmer calls [[BNRItemStore alloc]init].let him
// know the error of his ways
-(instancetype)init
{
[NSException raise:@“Singleton” format:@“Use +[BNRItemStore sharedStore]”];
return nil;
}

// here is a real (secret) initializer
-(instancetype)initPrivate
{
self = [super init];
if (self){
_privateItems = [[NSMutableArray alloc]init];
_itemValueLt50 = [[NSMutableArray alloc]init];
_itemValueGe50 = [[NSMutableArray alloc]init];

    // privateItems is a mutable array that has 2 objects: an array of BNRItems with a
    //valueInDollars less than 50 (_itemValueLt50) and an array of BNRItems with a
    // valueInDollars greater than or equal to 50 (_itemValueGe50)
    
    [_privateItems addObject:_itemValueLt50];
    [_privateItems addObject:_itemValueGe50];
    
    
}
return self;

}
-(NSArray *)allItems
{
return [self.privateItems copy];
}
-(BNRItem *)createItem
{
BNRItem *item = [BNRItem randomItem];
//[self.privateItems addObject:item];

// if the valueInDollars of the item is less than 50, add the item to
// the array itemValueLt50
// else
// add the item to the array itemValueGe50
// first, keep a log of the total number of items
// so that we will know when we have reached the last
// item to display.
self.totalItems = self.totalItems +1;
if (item.valueInDollars < 50){
    [self.itemValueLt50 addObject:item];
}
else{
    [self.itemValueGe50 addObject:item];
}
return item;

}
-(NSInteger)addToTotalCells
{
self.totalCells = self.totalCells + 1;
return self.totalCells;
}

@end
[/code]

[code]//
// BNRItem.m
// RandomItems
//
// Created by Nelson Capes on 8/17/15.
// Copyright © 2015 Big Nerd Ranch. All rights reserved.
//

#import “BNRItem.h”

@implementation BNRItem
+(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[adjectiveIndex],
                        randomNounList[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];

BNRItem *newItem = [[self alloc]initWithItemName:randomName
                              valueInDollars:randomvalue
                                serialNumber: randomSerialNumber];
return newItem;

}

+(instancetype)initWithItemName:(NSString )name
serialNumber:(NSString
)sNumber;
{
BNRItem *newItem = [[self alloc]initWithItemName:name valueInDollars:0 serialNumber:(NSString *)sNumber];
return newItem;
}

-(instancetype)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber
{
self =[super init];

if(self){
    _itemName = name;
    _serialNumber = sNumber;
    _valueInDollars = value;
    
    // set dateCreated to the current date and time
    _dateCreated = [[NSDate alloc]init];
}
return self;

}

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

}

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

-(NSString *)description
{
NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
self.itemName,
self.serialNumber,
self.valueInDollars,
self.dateCreated
];
return descriptionString;
}
-(void)dealloc
{
NSLog(@“Destroyed %@”, self);
}

@end
[/code]

[code]//
// BNRItem.h
// RandomItems
//
// Created by Nelson Capes on 8/17/15.
// Copyright © 2015 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject

@property (nonatomic, copy)NSString *itemName;
@property (nonatomic, copy)NSString *serialNumber;
@property (nonatomic)int valueInDollars;
@property (nonatomic, readonly, strong)NSDate *dateCreated;

+(instancetype)randomItem;
+(instancetype)initWithItemName:(NSString )name
serialNumber:(NSString
)sNumber;
// designated initializer
-(instancetype)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber;

-(instancetype)initWithItemName:(NSString *)name;

@end
[/code]

[code]//
// BNRAppDelegate.h
// Homepwner
//
// Created by Nelson Capes on 8/25/15.
// Copyright © 2015 Nelson Capes. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BNRAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@end
[/code]

[code]//
// BNRAppDelegate.m
// Homepwner
//
// Created by Nelson Capes on 8/25/15.
// Copyright © 2015 Nelson Capes. All rights reserved.
//

#import “BNRAppDelegate.h”
#import “BNRItemsViewController.h”
@implementation BNRAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    // create a BNRItemsViewController
    BNRItemsViewController *itemsViewController = [[BNRItemsViewController alloc]init];

    // place BNRItemsViewControllr’s table view in the window hierarchy
    self.window.rootViewController = itemsViewController;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)applicationWillResignActive:(UIApplication *)application
    {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application
    {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application
    {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application
    {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application
    {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end
[/code]