-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLBasketStore.m
383 lines (321 loc) · 10 KB
/
PLBasketStore.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
// PLBasketStore.m
// Plate
//
// Created by emileleon on 12/23/13.
// Copyright (c) 2013 Plate SF. All rights reserved.
//
#import "PLBasketStore.h"
#import "PLALaCarteItem.h"
#import "PLPlate.h"
#import "PLAddOnItem.h"
#import "PLPlateSize.h"
@implementation PLBasketStore
-(int)addAddOnItem:(PLAddOnItem *)item
{
PLAddOnItem *addOnItem = nil;
for (PLAddOnItem *basketItem in [self addOnBuilder]) {
if ([[item plateId] isEqualToString:[basketItem plateId]]) {
addOnItem = basketItem;
break;
}
}
if (addOnItem == nil) {
addOnItem = [item copy];
addOnItem.quantity = 0;
[[[PLBasketStore sharedStore] addOnBuilder] addObject:addOnItem];
}
addOnItem.quantity++;
if (addOnItem.quantity > 5) {
addOnItem.quantity = 5;
}
return addOnItem.quantity;
}
-(int)removeAddOnItem:(PLAddOnItem *)item
{
PLAddOnItem *addOnItem = nil;
for (addOnItem in [self addOnBuilder]) {
if ([[item plateId] isEqualToString:[addOnItem plateId]]) {
break;
}
}
if (addOnItem == nil) {
return 0;
}
addOnItem.quantity--;
if (addOnItem.quantity <= 0) {
[[self addOnBuilder] removeObject:addOnItem];
return 0;
}
return addOnItem.quantity;
}
-(int)addALaCarteItem:(PLALaCarteItem *)item
{
PLALaCarteItem *acItem = nil;
for (PLALaCarteItem *basketItem in [self alaCarteBuilder]) {
if ([[item plateId] isEqualToString:[basketItem plateId]]) {
acItem = basketItem;
break;
}
}
if (acItem == nil) {
acItem = [item copy];
acItem.quantity = 0;
[[[PLBasketStore sharedStore] alaCarteBuilder] addObject:acItem];
}
acItem.quantity++;
if (acItem.quantity > 5) {
acItem.quantity = 5;
}
return acItem.quantity;
}
-(int)removeALaCarteItem:(PLALaCarteItem *)item
{
PLALaCarteItem *acItem = nil;
for (acItem in [self alaCarteBuilder]) {
if ([[item plateId] isEqualToString:[acItem plateId]]) {
break;
}
}
if (acItem == nil) {
return 0;
}
acItem.quantity--;
if (acItem.quantity <= 0) {
[[self alaCarteBuilder] removeObject:acItem];
return 0;
}
return acItem.quantity;
}
-(void)addPlateToBasket
{
[[self plates] addObject:[self plateBuilder]];
self.plateBuilder = [[PLPlate alloc]init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"BasketUpdated" object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"PlateBuilderEmptied" object:self];
}
-(void)addALaCarteItemsToBasket
{
// Iterate over the items in the builder. If they exist, only update the quantity
for (PLALaCarteItem *builderItem in [self alaCarteBuilder]) {
BOOL itemExists = false;
for (PLALaCarteItem *item in [self alaCarteItems]) {
if ([builderItem plateId] == [item plateId]) {
item.quantity = item.quantity + builderItem.quantity;
itemExists = true;
break;
}
}
if (!itemExists) {
[[self alaCarteItems] addObject:[builderItem copy]];
}
}
self.alaCarteBuilder = [[NSMutableArray alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"BasketUpdated" object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ALaCarteBuilderEmptied" object:self];
}
-(void)addAddOnItemsToBasket
{
// Iterate over the items in the builder. If they exist, only update the quantity
for (PLAddOnItem *builderItem in [self addOnBuilder]) {
BOOL itemExists = false;
for (PLAddOnItem *item in [self addOns]) {
if ([builderItem plateId] == [item plateId]) {
item.quantity = item.quantity + builderItem.quantity;
itemExists = true;
break;
}
}
if (!itemExists) {
[[self addOns] addObject:[builderItem copy]];
}
}
self.addOnBuilder = [[NSMutableArray alloc]init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"BasketUpdated" object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AddOnBuilderEmptied" object:self];
}
-(void)clearBasket
{
[self setAlaCarteBuilder:[[NSMutableArray alloc] init]];
[self setAddOnBuilder:[[NSMutableArray alloc]init]];
[self setPlateBuilder:[[PLPlate alloc]init]];
[self setAlaCarteItems:[[NSMutableArray alloc]init]];
[self setAddOns:[[NSMutableArray alloc]init]];
[self setPlates:[[NSMutableArray alloc]init]];
}
#pragma Plate Builder helper methods
-(void)setPlateType:(PlateType)plateType
{
[[self plateBuilder] setType:plateType];
}
-(void)setPlateSize:(PlateSize)plateSize
{
[[self plateBuilder] setSize:plateSize];
}
-(void)setPlateType2:(PlateType)plateType
{
[[self plateBuilder] setType:plateType];
}
-(void)setPlateSize2:(PlateSize)plateSize
{
[[self plateBuilder] setSize:plateSize];
}
-(void)setPlateMain:(PLMenuItem *)mainEntree
{
[[self plateBuilder] setMain:mainEntree];
}
-(void)addPlateSide:(PLMenuItem *)newSide
{
// Make sure we do not exceed the allowed number of sides
int numSides = 0;
NSString *slug = [[[[PLBasketStore sharedStore] plateBuilder] plateTypeSize] typeSlug];
if ([slug isEqualToString:OneMainTwoSides]) {
numSides = 2;
} else if ([slug isEqualToString:FourSides]) {
numSides = 4;
}
NSMutableArray *sides = [[self plateBuilder] sides];
if ([sides count] >= numSides) {
// remove a side; right now we just remove the earliest one added
[sides removeObjectAtIndex:0];
}
[sides addObject:newSide];
}
-(void)removePlateSide:(PLMenuItem *)sideToRemove
{
// Remove the first occurrence of this item in the 'sides' array
int currIndex = 0;
NSMutableArray *sides = [[self plateBuilder] sides];
for (int i =0; i < [sides count]; i++) {
PLMenuItem *side = [sides objectAtIndex:currIndex];
if (side == sideToRemove) {
[sides removeObjectAtIndex:currIndex];
break;
}
}
}
#pragma Helper methods
-(int)quantityOfItemInALaCarteBuilder:(PLALaCarteItem *)item
{
for (PLALaCarteItem *aLaCarteItem in [self alaCarteBuilder]) {
if ([[aLaCarteItem plateId] isEqualToString:[item plateId]]) {
return [aLaCarteItem quantity];
}
}
return 0;
}
-(int)quantityOfItemInAddOnBuilder:(PLAddOnItem *)item
{
for (PLAddOnItem *addOnItem in [self addOnBuilder]) {
if ([[addOnItem plateId] isEqualToString:[item plateId]]) {
return [addOnItem quantity];
}
}
return 0;
}
-(int)quantityOfSideInPlateBuilder:(PLMenuItem *)item
{
int count = 0;
for (PLMenuItem *side in [[self plateBuilder] sides]) {
if ([side plateId] == [item plateId]) {
count++;
}
}
return count;
}
-(int)quantityOfItemsInALaCarteBuilder{
int count = 0;
for (PLALaCarteItem *aLaCarteItem in [self alaCarteBuilder]) {
count += [aLaCarteItem quantity];
}
return count;
}
-(int)quantityOfItemsInAddOnBuilder{
int count = 0;
for (PLAddOnItem *addOnItem in [self addOnBuilder]) {
count += [addOnItem quantity];
}
return count;
}
-(NSString *)printContentsOfBasket
{
NSString *summary = @"";
// Plates
if ([[self plates] count] > 0) {
summary = [summary stringByAppendingString:@"PLATES:\n"];
for (PLPlate *plate in [self plates]) {
if ([plate main] != nil) {
summary = [summary stringByAppendingString:@"Main:\n"];
summary = [summary stringByAppendingString:[[plate main] name]];
summary = [summary stringByAppendingString:@"\n"];
}
summary = [summary stringByAppendingString:@"Sides:\n"];
for (PLMenuItem *side in [plate sides]) {
summary = [summary stringByAppendingString:[side name]];
summary = [summary stringByAppendingString:@"\n"];
}
}
}
// Add Ons
if ([[self addOns] count] > 0) {
summary = [summary stringByAppendingString:@"ADD ONS:\n"];
for (PLAddOnItem *item in [self addOns]) {
NSString *addOnSummary = [NSString stringWithFormat:@"%@: Qty %d\n", [item name], [item quantity]];
summary = [summary stringByAppendingString:addOnSummary];
}
}
// A La Carte
if ([[self alaCarteItems] count] > 0) {
summary = [summary stringByAppendingString:@"A LA CARTE:\n"];
for (PLALaCarteItem *item in [self alaCarteItems]) {
NSString *alacSummary = [NSString stringWithFormat:@"%@: Qty %d\n", [item name], [item quantity]];
summary = [summary stringByAppendingString:alacSummary];
}
}
if ([summary length] == 0) {
summary = [NSString stringWithFormat:@"Basket is empty"];
}
return summary;
}
-(float)totalCostOfItemsInBasket
{
float total = 0.0;
for (PLPlate *plate in [self plates]) {
total += [[plate plateSize] price];
}
for (PLALaCarteItem *item in [self alaCarteItems]) {
total += [item price] * [item quantity];
}
for (PLAddOnItem *item in [self addOns]) {
total += [item price] * [item quantity];
// total += (2 * [item quantity]);
}
return total;
}
-(int)quantityOfAllItemsInBasket
{
int count = 0;
count += [[self plates] count];
for (PLALaCarteItem *item in [self alaCarteItems]) {
count += [item quantity];
}
for (PLAddOnItem *item in [self addOns]) {
count += [item quantity];
}
return count;
}
#pragma Singleton setup
+ (id)allocWithZone:(struct _NSZone *)zone
{
return [self sharedStore];
}
+ (PLBasketStore *)sharedStore
{
static PLBasketStore *sharedStore = nil;
if (!sharedStore) {
sharedStore = [[super allocWithZone:nil] init];
[sharedStore clearBasket];
}
return sharedStore;
}
@end