Skip to content

Instantly share code, notes, and snippets.

@woodnathan
Last active August 29, 2015 14:02
Show Gist options
  • Save woodnathan/6e41c8800ac0fb4e5596 to your computer and use it in GitHub Desktop.
Save woodnathan/6e41c8800ac0fb4e5596 to your computer and use it in GitHub Desktop.
//
// BSAuthor.h
// BookstoreApp
//
// Created by Nathan Wood on 17/06/2014.
// Copyright (c) 2014 Nathan Wood. All rights reserved.
//
#import <Foundation/Foundation.h>
@class BSBook;
@interface BSAuthor : NSObject
- (instancetype)initWithName:(NSString *)name;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy, readonly) NSArray *books;
- (void)addBook:(BSBook *)book;
@end
//
// BSAuthor.m
// BookstoreApp
//
// Created by Nathan Wood on 17/06/2014.
// Copyright (c) 2014 Nathan Wood. All rights reserved.
//
#import "BSAuthor.h"
@interface BSAuthor ()
@property (nonatomic, copy, readwrite) NSArray *books;
@end
@implementation BSAuthor
- (instancetype)initWithName:(NSString *)name
{
self = [super init];
if (self)
{
self.name = name;
self.books = [[NSArray alloc] init];
}
return self;
}
- (void)addBook:(BSBook *)book
{
self.books = [self.books arrayByAddingObject:book];
}
@end
//
// BSBook.h
// BookstoreApp
//
// Created by Nathan Wood on 17/06/2014.
// Copyright (c) 2014 Nathan Wood. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BSBook : NSObject
+ (instancetype)bookWithTitle:(NSString *)title price:(float)price;
- (instancetype)initWithTitle:(NSString *)title price:(float)price;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) float price;
@property (nonatomic, strong) UIImage *image;
@end
//
// BSBook.m
// BookstoreApp
//
// Created by Nathan Wood on 17/06/2014.
// Copyright (c) 2014 Nathan Wood. All rights reserved.
//
#import "BSBook.h"
@implementation BSBook
+ (instancetype)bookWithTitle:(NSString *)title price:(float)price
{
return [[self alloc] initWithTitle:title price:price];
}
- (instancetype)initWithTitle:(NSString *)title price:(float)price
{
self = [super init];
if (self)
{
self.title = title;
self.price = price;
}
return self;
}
@end
//
// BSBookStore.h
// BookstoreApp
//
// Created by Nathan Wood on 17/06/2014.
// Copyright (c) 2014 Nathan Wood. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "BSAuthor.h"
#import "BSBook.h"
@class BSAuthor;
@interface BSBookStore : NSObject
- (instancetype)initWithTitle:(NSString *)title;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy, readonly) NSArray *authors;
- (void)addAuthor:(BSAuthor *)author;
- (void)populate;
@end
//
// BSBookStore.m
// BookstoreApp
//
// Created by Nathan Wood on 17/06/2014.
// Copyright (c) 2014 Nathan Wood. All rights reserved.
//
#import "BSBookStore.h"
@interface BSBookStore ()
@property (nonatomic, copy, readwrite) NSArray *authors;
@end
@implementation BSBookStore
- (instancetype)init
{
return [self initWithTitle:@"My Book Store"];
}
- (instancetype)initWithTitle:(NSString *)title
{
self = [super init];
if (self)
{
self.title = title;
self.authors = [[NSArray alloc] init];
}
return self;
}
- (void)addAuthor:(BSAuthor *)author
{
self.authors = [self.authors arrayByAddingObject:author];
}
- (void)populate
{
BSAuthor *rowling = [[BSAuthor alloc] initWithName:@"J.K. Rowling"];
BSAuthor *martin = [[BSAuthor alloc] initWithName:@"George R.R. Martin"];
BSAuthor *collins = [[BSAuthor alloc] initWithName:@"Suzanne Collins"];
[rowling addBook:[BSBook bookWithTitle:@"The Philosopher's Stone" price:6.59]];
[rowling addBook:[BSBook bookWithTitle:@"The Chamber of Secrets" price:6.64]];
[rowling addBook:[BSBook bookWithTitle:@"The Prisoner of Azkaban" price:6.88]];
[martin addBook:[BSBook bookWithTitle:@"A Game of Thrones" price:6.02]];
[martin addBook:[BSBook bookWithTitle:@"A Dance with Dragons" price:6.02]];
[martin addBook:[BSBook bookWithTitle:@"A Feast for Crows" price:6.02]];
BSBook *hungerGames = [BSBook bookWithTitle:@"The Hunger Games" price:6.22];
hungerGames.image = [UIImage imageNamed:@"hunger.jpeg"];
[collins addBook:hungerGames];
[collins addBook:[BSBook bookWithTitle:@"Catching Fire" price:7.32]];
[collins addBook:[BSBook bookWithTitle:@"Mockingjay" price:8.06]];
// Add the authors to our store
[self addAuthor:rowling];
[self addAuthor:martin];
[self addAuthor:collins];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment