-
Notifications
You must be signed in to change notification settings - Fork 0
/
BNRContainer.m
64 lines (49 loc) · 1.45 KB
/
BNRContainer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// BNRContainer.m
// RandomPossessions
//
// Created by Camron Schwoegler on 9/17/13.
// Copyright (c) 2013 Camron Schwoegler. All rights reserved.
//
#import "BNRContainer.h"
@implementation BNRContainer
- (id)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber
{
self = [super initWithItemName:name valueInDollars:value serialNumber:sNumber];
if (self) {
subitems = [[NSMutableArray alloc] init];
}
return self;
}
- (NSString *)description
{
NSString *desc = [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%ld\n", [self itemName], [self serialNumber], (long)([self totalValue] + [self valueInDollars])];
for (BNRItem *item in subitems) {
desc = [desc stringByAppendingFormat:@"%@\n", item];
}
return desc;
// return [[NSString alloc] initWithFormat:@"%@ %d %@", [self itemName], [self valueInDollars] + [self totalValue], [self subItems]];
}
- (NSInteger)totalValue
{
NSInteger totalValue = 0;
for (int i = 0; i < [subitems count]; i++) {
if ([[subitems objectAtIndex:i] class] == [BNRContainer class]) {
totalValue += [[subitems objectAtIndex:i] totalValue];
}
else
{
totalValue += [[subitems objectAtIndex:i] valueInDollars];
}
}
return totalValue;
}
- (void)addItem:(id)item
{
[subitems addObject:item];
}
- (id)subItems
{
return subitems;
}
@end