-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomerClient.cs
278 lines (251 loc) · 10.5 KB
/
CustomerClient.cs
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
using Phase3Databases.DatabaseModels;
namespace Phase3Databases;
public class CustomerClient : Client
{
//private Phase3Context Phase3DB;
private Customer user;
private Random rand = new Random();
public CustomerClient(string connstring) : base(connstring)
{
//Queries here? https://learn.microsoft.com/en-us/ef/core/querying/
try
{
using (Phase3Context db = new Phase3Context(this.connstring))
{
user = db.Customers.Single(e => (e.CustomerId == uid));
}
}
catch (Exception e)
{
//We either got more than one user for id (not possible) or got 0 results (id doesn't exist)
throw new UserNotFoundException("Couldn't find user with id " + this.uid);
}
Console.WriteLine(user.FirstName);
}
public override void MainLoop()
{
int option = -1;
while (option != 4)
{
Console.WriteLine("What would you like to do?\n");
option = Client.OptionsMenu("Search for an item", "Place an order", "Pick up available orders", "Log-out");
switch (option)
{
case 1:
//Search for an item
Console.WriteLine("What would you like to search for?");
List<Item> results = this.itemSearch(Console.ReadLine());
if (results.Count == 0)
{
Console.WriteLine("No results :(");
break;
}
else
{
foreach (Item i in results)
{
Console.WriteLine(i);
}
}
break;
case 2:
PlaceOrder();
break;
case 3:
PickupOrders();
break;
}
}
}
//if user has any outstanding orders, their status is updated and it will return true
private bool PickupOrders()
{
using (Phase3Context db = new Phase3Context(this.connstring))
{
List<Order> pickupOrders = db.Orders
.Where(order => ((order.CustomerId == uid) && (order.OrderStatus == 2))).ToList();
if (!pickupOrders.Any())
{
Console.WriteLine("Looks like you do not have any orders available for pickup right now!");
return false;
}
db.UpdateRange(pickupOrders);
foreach (Order order in pickupOrders)
{
order.OrderStatus = 4;
order.PickupTime = DateTime.Now;
Console.WriteLine("You picked up order #" + order.OrderId + " with " + db.ItemsOrdereds.Count(io => (io.OrderId == order.OrderId)) + " items!");
}
db.SaveChanges();
return true;
}
return false;
}
//Returns boolean showing whether order was successfully placed or not
private bool PlaceOrder()
{
List<ValueTuple<int, int>> shopList = new List<ValueTuple<int, int>>();
decimal orderTotal = 0;
do
{
ValueTuple<int, int> entry;
Console.WriteLine(
"Please enter the id and quantity of the item you would like to add to your order, separated by a comma: (enter a negative item id when you are done)");
try
{
string[] s = Console.ReadLine().Split(',');
if (s[0].Contains('-'))
{
break;
}
entry = (int.Parse(s[0]), int.Parse(s[1]));
}
catch (Exception e)
{
Console.WriteLine("Your input was formatted incorrectly. Please try again.");
continue;
}
if (entry.Item1 < 0)
{
break;
}
else if (entry.Item2 <= 0)
{
continue;
}
else
{
try
{
ValueTuple<int, int> currenttup = shopList.Single(tup => (tup.Item1 == entry.Item1));
shopList.Remove(currenttup);
currenttup.Item2 += entry.Item2;
shopList.Add(currenttup);
}
catch (InvalidOperationException e)
{
//Item not already in list
shopList.Add(entry);
}
}
} while (true);
if (shopList.Count == 0)
{
Console.WriteLine("Your cart is empty!");
return false;
}
else
{
using (Phase3Context db = new Phase3Context(this.connstring))
{
for (int i = 0; i < shopList.Count; i++)
{
try
{
Item item = db.Items.Where(j => j.ItemId == shopList[i].Item1).Single();
if (item.AgeRequirement != null && user.Age < item.AgeRequirement)
{
Console.WriteLine("Sorry, you do not meet the age requirements to order " + item.ItemName +
"! Would you still like to continue with your order?");
switch (OptionsMenu("Yes", "No"))
{
case 1:
shopList.Remove(shopList[i]);
continue;
case 2:
return false;
}
}
if (item.QuantityAvailable <= 0)
{
Console.WriteLine("Sorry, it looks like we are all out of " + item.ItemName +
"! Would you still like to continue with your order?");
switch (OptionsMenu("Yes", "No"))
{
case 1:
shopList.Remove(shopList[i]);
continue;
case 2:
return false;
}
}
else if (item.QuantityAvailable < shopList[i].Item2)
{
Console.WriteLine("You requested " + shopList[i].Item2 + " " + item.ItemName +
"(s), but it looks like we only have " + item.QuantityAvailable +
" left! What would you like to do?");
switch (OptionsMenu("Just give me the " + item.QuantityAvailable + " in stock",
"Remove item from order and continue", "Cancel entire order"))
{
case 1:
shopList[i] = (shopList[i].Item1, item.QuantityAvailable);
//Increment price proper amount
orderTotal += (item.Price * shopList[i].Item2);
continue;
case 2:
shopList.Remove(shopList[i]);
continue;
case 3:
return false;
}
}
//Increment price if we successfully validate the item for order
orderTotal += (item.Price * shopList[i].Item2);
}
catch (InvalidOperationException e)
{
Console.WriteLine("Could not find an item with id " + shopList[i].Item1 +
"! Would you like to continue with the rest of your order?");
switch (OptionsMenu("Yes", "No"))
{
case 1:
shopList.Remove(shopList[i]);
continue;
case 2:
return false;
}
}
}
Order newOrder = new Order();
newOrder.OrderId = rand.Next(100000000);
newOrder.OrderTotal = orderTotal;
newOrder.CustomerId = user.CustomerId;
newOrder.OrderTimestamp = DateTime.Now;
//Status of 1 = Placed and needing to be fulfilled
newOrder.OrderStatus = 1;
if (user.DeliveryLocation != null)
{
Console.WriteLine("Would you like this order delivered?");
switch (OptionsMenu("Yes", "No"))
{
case 1:
//if an order has an estimated delivery time, then we know it is a delivery order
newOrder.EstimatedDeliveryTime = DateTime.Now.AddDays(2);
break;
case 2:
break;
}
}
db.Orders.Add(newOrder);
foreach (ValueTuple<int, int> k in shopList)
{
db.ItemsOrdereds.Add(new ItemsOrdered(newOrder.OrderId, k.Item1, k.Item2));
Item item = db.Items.Where(j => j.ItemId == k.Item1).Single();
db.Update(item);
item.QuantityAvailable -= k.Item2;
db.PickLists.Add(new PickList(k.Item1, k.Item2, newOrder.OrderId));
}
db.SaveChanges();
}
}
//Here to keep the compiler quiet during testing
return true;
}
private List<Item> itemSearch(string query)
{
using (Phase3Context db = new Phase3Context(this.connstring))
{
return db.Items.Where(i => i.ItemName.Contains(query)).ToList();
}
}
}