-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemGroup.m
260 lines (212 loc) · 7.26 KB
/
ItemGroup.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// ItemGroup.m
// AvocadoTest1
//
// Created by Jake on 12-01-31.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "ItemGroup.h"
#import "Menu.h"
#import "Item.h"
#import "Order.h"
#import "Combo.h"
#import "ItemGroupPricingStrategy.h"
NSString* ITEM_GROUP_MODIFIED = @"CroutonLabs/ItemGroupModified";
@implementation ItemGroup
@synthesize listOfItems;
@synthesize satisfyingItem;
@synthesize satisfyingMenus;
-(ItemGroup *)init
{
self = [super init];
listOfItems = [[NSMutableArray alloc] initWithCapacity:0];
satisfyingMenus = [[NSMutableArray alloc] initWithCapacity:0];
//This menu will contain all of the items that satisfy an item group that are not
//accounted for in the other explicitly added menus.
Menu *otherMenu = [[Menu alloc] init];
[otherMenu setName:@"Other"];
[satisfyingMenus addObject:otherMenu];
return self;
}
-(ItemGroup *)initWithDataAndParentMenu:(NSDictionary *)inputData :(Menu *)parentMenu
{
self = [super initFromData:inputData];
listOfItems = [[NSMutableArray alloc] initWithCapacity:0];
satisfyingMenus = [[NSMutableArray alloc] initWithCapacity:0];
Menu *otherMenu = [[Menu alloc] init];
[otherMenu setName:@"Other"];
[satisfyingMenus addObject:otherMenu];
strategy = [ItemGroupPricingStrategy pricingStrategyFromData:[inputData objectForKey:@"pricing_strategy"]];
if(!strategy)
{
CLLog(LOG_LEVEL_ERROR, @"An invalid ItemStrategy string was parsed from JSON data");
}
NSMutableArray *itemPKs = [inputData objectForKey:@"items"];
NSMutableArray *menuPKs = [inputData objectForKey:@"menus"] ;
for (NSString *itemPK in itemPKs)
{
NSInteger intItemPK = [itemPK intValue];
Item *itemToAdd = [parentMenu dereferenceItemPK:intItemPK];
if (itemToAdd) {
[self addItem:itemToAdd];
}
else
{
CLLog(LOG_LEVEL_ERROR, [NSString stringWithFormat: @"A dereference was attempted on an invalid item PK %i\n%@",intItemPK,parentMenu]);
}
}
for (NSString *menuPK in menuPKs)
{
Menu *menuForPK = [parentMenu dereferenceMenuPK:[menuPK intValue]];
if (menuForPK) {
[self addMenu:menuForPK];
}
else
{
CLLog(LOG_LEVEL_ERROR, [NSString stringWithFormat: @"A dereference was attempted on an invalid menu PK %@\n%@",menuPK,parentMenu]);
}
}
return self;
}
-(void) listOfItemsRecovery:(NSCoder *)decoder
{
switch (harddiskDataVersion) {
case VERSION_0_0_0:
//fall through to next
case VERSION_1_0_0:
//fall through to next
case VERSION_1_0_1:
listOfItems = [decoder decodeObjectForKey:@"list_of_items"];
case VERSION_1_1_0:
break;
default:
break;
}
}
-(void) satisfyingItemRecovery:(NSCoder *)decoder
{
switch (harddiskDataVersion) {
case VERSION_0_0_0:
//fall through to next
case VERSION_1_0_0:
//fall through to next
case VERSION_1_0_1:
satisfyingItem = [decoder decodeObjectForKey:@"satisfying_item"];
case VERSION_1_1_0:
break;
default:
break;
}
}
-(ItemGroup *)copy
{
ItemGroup *anItemGroup = [[ItemGroup alloc] init];
anItemGroup->primaryKey = primaryKey;
anItemGroup->name = name;
anItemGroup->desc = desc;
anItemGroup->listOfItems = [[NSMutableArray alloc] initWithArray:listOfItems];
anItemGroup->satisfyingItem = [satisfyingItem copy];
anItemGroup->strategy = strategy;
anItemGroup->satisfyingMenus = [[NSMutableArray alloc] initWithArray:satisfyingMenus];
return anItemGroup;
}
-(BOOL)containsItem:(Item *)anItem
{
return [[self itemIds] containsObject:[NSNumber numberWithUnsignedInteger:[anItem primaryKey]]];
}
-(BOOL)satisfied
{
return satisfyingItem && [self containsItem:satisfyingItem];
}
-(NSDecimalNumber *)price
{
if(!satisfyingItem) return [NSDecimalNumber decimalNumberWithString:@"0"];
return [strategy priceForItem:satisfyingItem];
}
-(NSDecimalNumber *)savings
{
if(!satisfyingItem) return nil;
return [[[self satisfyingItem] price] decimalNumberBySubtracting:[self price]];
}
-(void)setSatisfyingItem:(Item *)anItem
{
if(satisfyingItem)
[[NSNotificationCenter defaultCenter] removeObserver:self name:ITEM_MODIFIED object:satisfyingItem];
satisfyingItem = anItem;
[[NSNotificationCenter defaultCenter] postNotificationName:ITEM_GROUP_MODIFIED object:self];
}
-(void)addItem:(Item *)anItem
{
BOOL itemInMenus = NO;
for (Menu *eachMenu in satisfyingMenus) {
if ([[eachMenu flatItemList] containsObject:anItem]) {
itemInMenus = YES;
}
}
if (!itemInMenus) {
//Add the item to the "other" menu.
[[satisfyingMenus objectAtIndex:0] addItem:anItem];
}
[listOfItems addObject:anItem];
itemIds = nil;
}
-(void) addListOfItems:(NSArray *)items
{
for (Item *eachItem in items) {
[self addItem:eachItem];
}
}
-(void)addMenu:(Menu *)aMenu
{
[satisfyingMenus addObject:aMenu];
[self addListOfItems:[aMenu flatItemList]];
itemIds = nil;
}
-(NSSet*)itemIds{
if(!itemIds){
NSMutableSet* ids = [NSMutableSet setWithCapacity:[listOfItems count]];
for(Item* item in listOfItems){
[ids addObject:[NSNumber numberWithUnsignedInteger:[item primaryKey]]];
}
itemIds = ids;
}
return itemIds;
}
- (NSString *) descriptionWithIndent:(NSInteger) indentLevel
{
NSString *padString = [@"" stringByPaddingToLength:(indentLevel*4) withString:@" " startingAtIndex:0];
NSMutableString *output = [[NSMutableString alloc] initWithCapacity:0];
[output appendFormat:@"%@ItemGroup:%\n",padString];
[output appendString:[super descriptionWithIndent:indentLevel]];
[output appendFormat:@"%@Pricing strategy: %@\n",padString,strategy];
[output appendFormat:@"%@Satisfied: %i\n",padString,[self satisfied]];
[output appendFormat:@"%@Satisfying item: %@\n",padString,[satisfyingItem descriptionWithIndent:(indentLevel + 1)]];
[output appendFormat:@"%@Items:\n",padString];
for (Item *anItem in listOfItems) {
[output appendFormat:@"%@\n",[anItem descriptionWithIndent:(indentLevel + 1)]];
}
return output;
}
-(ItemGroup*)optimalPickFromItems:(NSArray *)items{
NSSet* ids = [self itemIds];
NSArray* applicable = [items filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(Item* item, NSDictionary *bindings) {
return [ids containsObject:[NSNumber numberWithUnsignedInteger:[item primaryKey]]];
}]];
if(![applicable count]){
return nil;
}
ItemGroup* new = [self copy];
[new setSatisfyingItem:[strategy optimalItem:applicable]];
return new;
}
-(NSDictionary*)orderRepresentation{
NSMutableDictionary* itemDictionary =
[NSMutableDictionary dictionaryWithDictionary:[satisfyingItem orderRepresentation]];
[itemDictionary setObject:[NSNumber numberWithUnsignedInteger:primaryKey] forKey:@"component"];
return itemDictionary;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end