-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSMSImpl.java
810 lines (641 loc) · 22.9 KB
/
DSMSImpl.java
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
package a3;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import javax.jws.*;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;
@WebService(endpointInterface = "a3.WebInterface")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class DSMSImpl implements WebInterface{
HashMap<String,Item> inventory = new HashMap<>();
File log;
private static final java.lang.Object lock = new java.lang.Object();
static Integer sema = new Integer(0);
String storeName;
int port;
static ArrayList<String> itemsFound = new ArrayList<String>();
private static boolean exchange;
boolean returnStatus;
double purchaseStatus;
public DSMSImpl() {}
public DSMSImpl(String storeName){
inventory = new HashMap<String,Item>();
this.storeName = storeName;
exchange = false;
returnStatus = true;
switch(storeName) {
case "ON": port = 9000; break;
case "BC": port = 7000; break;
case "QC": port = 8000; break;
default: port = 8000; break;
}
Runnable UDPServer = () -> { receive(); };
Thread UDPthread = new Thread(UDPServer);
UDPthread.start();
// Create log file
try {
log = new File(storeName + "Serverlog.txt");
if(log.createNewFile()) {
System.out.println("File created for " + storeName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public DSMSImpl(String storeName, HashMap<String,Item> inventory){
this.inventory = inventory;
this.storeName = storeName;
exchange = false;
returnStatus = true;
switch(storeName) {
case "ON": port = 9000; break;
case "BC": port = 7000; break;
case "QC": port = 8000; break;
default: port = 8000; break;
}
Runnable UDPServer = () -> { receive(); };
Thread UDPthread = new Thread(UDPServer);
UDPthread.start();
// Create log file
try {
log = new File(storeName + "Serverlog.txt");
if(log.createNewFile()) {
System.out.println("File created for " + storeName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
////////////////////////
// Manager operations //
////////////////////////
@WebMethod()
public boolean addItem(String managerID, String itemID, String itemName, short quantity, double price) {
boolean status = true;
// A manager can only add items from their own store
if(!itemID.substring(0,2).equals(storeName)) {
status = false;
}
else {
if(inventory.containsKey(itemID)) { // If item already exists, change quantity
Item i = inventory.get(itemID);
i.quantity += quantity;
}
else { // Otherwise add to hash map
inventory.put(itemID, new Item(itemID, itemName, quantity, price));
}
}
// Write to file
synchronized(log) {
try {
FileWriter write = new FileWriter(log, true);
write.append("\nREQUEST:\n" + "\tDate: " + new Date()
+ "\n\tType: add" + "\n\tSubmitted by: " + managerID
+ "\n\tItem ID: " + itemID
+ "\n\tItem name: " + itemName
+ "\n\tItem quantity: " + quantity
+ "\n\tItem price: " + price);
write.append(status?"\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
write.close();
} catch (IOException e) {
e.printStackTrace();
}}
return status;
}
@WebMethod()
public boolean removeItem(String managerID, String itemID, short quantity) {
boolean status = true;
if(managerID.charAt(2)!='M') {
status = false;
System.out.println("Sorry, only managers can remove items!");
}
// Check if item exists
if(inventory.containsKey(itemID)) {
synchronized(inventory) {
if(quantity == -1) { // Remove the item entirely if -1 is entered
// Clear the list of people who have bought the item
if(!inventory.get(itemID).IDOfBuyer.isEmpty()) {
inventory.get(itemID).IDOfBuyer.clear();
inventory.get(itemID).dateOfPurchase = null;
}
inventory.remove(itemID); // Remove from inventory
}
else if(inventory.get(itemID).quantity > 0 && inventory.get(itemID).quantity >= quantity){ // Otherwise decrement by the specified quantity
Item i = inventory.get(itemID);
i.quantity -= quantity;
}
else {
status = false;
}
}
}
else{
status = false;
}
System.out.println(status?"Remove operation was successful":"Remove operation was not successful");
// Write to file
synchronized(log) {
try {
FileWriter write = new FileWriter(log, true);
write.append("\nREQUEST:\n" + "\tDate: " + new Date()
+ "\n\tType: remove" + "\n\tSubmitted by: " + managerID
+ "\n\tItem ID: " + itemID
+ "\n\tItem quantity: " + quantity);
write.append(status?"\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
write.close();
} catch (IOException e) {
e.printStackTrace();
}}
return status;
}
@WebMethod()
public String listItemAvailability(String managerID) {
if(managerID.charAt(2)!='M') {
return "Sorry, only managers can list items!";
}
String list = "";
// List items
for(Item i : inventory.values()) {
list += "ID: " + i.itemID + "\nName: " + i.itemName + "\nPrice: " + i.price + "\nQuantity: " + i.quantity + "\n\n";
}
// Write to file
synchronized(log){
try {
FileWriter write = new FileWriter(log, true);
write.append("\nREQUEST:\n" + "\tDate: " + new Date()
+ "\n\tType: list inventory" + "\n\tSubmitted by: " + managerID);
write.close();
} catch (IOException e) {
e.printStackTrace();
}}
return list;
}
////////////////////////
// User operations //
////////////////////////
@WebMethod()
public boolean checkPurchase(String customerID) {
for(Item item : inventory.values()) {
if(item.IDOfBuyer.contains(customerID)) return false;
}
return true;
}
@WebMethod()
public double purchaseItem(String customerID, String ID, a3.Date dateOfPurchase) {
String itemID = ID.substring(0,6);
String store = itemID.substring(0,2);
Runnable r = () -> {checkAvailability(itemID);};
Thread queueThread = new Thread(r);
boolean status = true;
Item i = null;
// Fail conditions
if(customerID.charAt(2)!='U') {
status = false;
System.out.println("Sorry, only customers can purchase items!");
}
// If the item is not from the store, send a request to the right server
if(!store.equals(storeName)) {
return Double.parseDouble(sendPurchaseRequest(store, customerID, ID, dateOfPurchase.getDay(), dateOfPurchase.getMonth(), dateOfPurchase.getYear()));
}
// Check if item exists in the store
if(inventory.containsKey(itemID)) {
i = inventory.get(itemID);
synchronized(i) {
// Buyers from other stores can only purchase one item
if(!customerID.substring(0,2).equals(storeName) && !checkPurchase(customerID.substring(0, 7))) {
System.out.println("This user has already purchased an item from this store.");
status = false;
}
else if(ID.length() > 6 && ID.substring(7).equals("wait")) { // Handle waiting list
synchronized(i.waitingList) {
i.waitingList.put(customerID); // Add user to queue
System.out.println("User " + customerID.substring(0,7) + " added to queue. Current users in queue:");
for(String id : i.waitingList) {
System.out.println(id);
}
}
queueThread.start();
}
else if(i.quantity > 0) { // Make purchase
if(customerID.length() > 7 && customerID.substring(7).matches("[0-9.]+")) { // Check budget
double budget = Double.parseDouble(customerID.substring(7));
if(i.price > budget) {
status = false;
}else {
synchronized(lock) {
i.quantity--;
i.dateOfPurchase = dateOfPurchase;
i.IDOfBuyer.add(customerID.substring(0,7));
}
}
}
}
else { status = false; return -1; }; // Ask the user whether they want to be added to the wait list
}
} else{
System.out.println("Item does not exist.");
status = false; // Cannot make the purchase because item does not exist
}
// Write to file
synchronized(log) {
try {
FileWriter write = new FileWriter(log, true);
write.append("\nREQUEST:\n" + "\tDate of purchase: " + dateOfPurchase
+ "\n\tType: purchase" + "\n\tSubmitted by: " + customerID
+ "\n\tItem ID: " + itemID);
write.append(status?"\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(status) return i.price; // Returns price if successful
else return 0.0; // Returns 0 if failed
}
@WebMethod()
// Once an item becomes available, the first customer in the queue will be able to purchase it
public void checkAvailability(String itemID){
if(inventory.containsKey(itemID)) {
Item item = inventory.get(itemID);
while(!item.waitingList.isEmpty()) {
synchronized(lock) {
if(item.quantity > 0) {
System.out.println("Item " + itemID + " is now in stock");
String customerID = item.waitingList.poll();
System.out.println(customerID.substring(0,7) + " is no longer in the waiting list for item " + itemID);
if(purchaseItem(customerID, itemID, a3.Date.getCurrentDate()) > 0) {
System.out.println(customerID.substring(0,7) + " has purchased " + itemID);
}
else {
System.out.println(customerID.substring(0,7) + " could not purchase " + itemID);
}
}
}
}
}
}
@WebMethod()
public String findItem(String customerID, String itemName) {
boolean status = true;
String items = "";
if(customerID.charAt(2)!='U') {
status = false;
return "Sorry, only customers can find items!";
}
// Search for item in own store
for(Item a : inventory.values()) {
if(a.itemName.equalsIgnoreCase(itemName)) {
itemsFound.add(a.itemID + " " + a.quantity + " " + a.price + "\n");
}
}
if(customerID.substring(0,2).equals(storeName)) { // Send request to other stores
for(int i = 7000; i <= 9000; i+=1000) {
if(i != this.port) {
sendFindItemRequest(i, customerID, itemName);
}
}
}
// Write to file
synchronized(log) {
try {
FileWriter write = new FileWriter(log, true);
write.append("\nREQUEST:\n" + "\tDate: " + new Date()
+ "\n\tType: search" + "\n\tSubmitted by: " + customerID
+ "\n\tItem name: " + itemName);
write.append(status?"\n\tFound items: " + items.toString() + "\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
write.close();
} catch (IOException e) {
e.printStackTrace();
}}
if(itemsFound.isEmpty()) return "";
String finalr = itemsFound.toString();
System.out.println("Find result: " + finalr);
itemsFound.clear();
return finalr;
}
@WebMethod()
public double returnItem(String customerID, String itemID, a3.Date dateOfReturn) {
boolean status = true;
Item i = null;
if(itemID.substring(0,2).equals(storeName) && customerID.charAt(2) == 'U') {
if(inventory.containsKey(itemID)) {
i = inventory.get(itemID);
synchronized(i) {
if(i.IDOfBuyer.contains(customerID)){
Calendar c = Calendar.getInstance();
c.set(i.dateOfPurchase.getDay(), i.dateOfPurchase.getMonth(), i.dateOfPurchase.getYear());
int dayOfYear = c.get(Calendar.DAY_OF_YEAR);
c.set(dateOfReturn.getDay(), dateOfReturn.getMonth(), dateOfReturn.getYear());
int dayOfYearReturn = c.get(Calendar.DAY_OF_YEAR);
// If the item was purchased by this customer within the past 30 days, they can return it
if((dayOfYearReturn - dayOfYear) < 30 && (dayOfYearReturn-dayOfYear) >= 0){
i.quantity++;
i.IDOfBuyer.remove(customerID);
//i.dateOfPurchase = null;
return i.price;
}
else{
status = false;
System.out.println("Sorry, returns are only valid for 30 days after the date of purchase.");
}
} else {
status = false;
System.out.println("Sorry, " + itemID + " has not been purchased by " + customerID + " yet.");
}
}
}
else {
status = false;
}
}
else if(!itemID.substring(0,2).equals(storeName)) {
String returnResult = sendReturnRequest(customerID, itemID).trim();
double res = Double.parseDouble(returnResult);
if(res == 0.0){
status=false;
} else return res;
}
else {
status = false;
System.out.println("Sorry, only customers can return items!");
}
// Write to file
synchronized(log) {
try {
FileWriter write = new FileWriter(log, true);
write.append("\nREQUEST:\n" + "\tDate of return: " + dateOfReturn
+ "\n\tType: return" + "\n\tSubmitted by: " + customerID
+ "\n\tItem ID: " + itemID);
write.append(status?"\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
write.close();
} catch (IOException e) {
e.printStackTrace();
}}
return 0.0; // Returns 0 if failed
}
@WebMethod()
public boolean exchangeItem(String customerID, String newItemID, String oldItemID) {
double budget = 0;
if(customerID.length() > 7) budget = Double.parseDouble(customerID.substring(7));
String customerIDactual = customerID.substring(0,7);
a3.Date currentDate = a3.Date.getCurrentDate();
sema = 2;
setExchange(true);
boolean status = false;
String pStatus = null, rStatus = null;
pStatus = sendExchangeRequest(customerID, newItemID, "purchase"); // Verify purchase
rStatus = sendExchangeRequest(customerIDactual, oldItemID, "return"); // Verify return
if(pStatus.trim().equals("true") && rStatus.trim().matches("[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}")) {
String[] d = rStatus.trim().split("-");
a3.Date returnDate = new a3.Date(Short.parseShort(d[0]),Short.parseShort(d[1]),Short.parseShort(d[2]));
returnItem(customerIDactual, oldItemID, returnDate);
purchaseItem(customerID, newItemID, currentDate);
return true;
} else return false;
}
private String sendExchangeRequest(String customerID, String itemID, String type) {
String store = itemID.substring(0,2);
int serverPort = 0;
double budget = 0;
if(customerID.length() > 7) budget = Double.parseDouble(customerID.substring(7));
switch(store) {
case "ON": serverPort = 9000; break;
case "BC": serverPort = 7000; break;
case "QC": serverPort = 8000; break;
default: serverPort = 8000;
}
// Create socket
DatagramSocket mySocket = null;
try {
mySocket = new DatagramSocket();
String dataToSend = customerID + "," + itemID + "," + type;
// Byte array for the item ID
byte[] item = dataToSend.getBytes();
InetAddress host = InetAddress.getLocalHost();
// Create packet
DatagramPacket request = new DatagramPacket(item, dataToSend.length(), host, serverPort);
// Send packet
mySocket.send(request);
System.out.println("Exchange request to " + store + " sent");
byte[] buffer = new byte[1000]; // Byte array to receive response
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
mySocket.receive(reply);
return new String(reply.getData()); // Receive status message from server
} catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mySocket != null) mySocket.close();
}
return "Operation was unsuccessful";
}
private String sendPurchaseRequest(String store, String customerID, String itemID, short day, short month, short year) {
int serverPort = 0;
switch(store) {
case "ON": serverPort = 9000; break;
case "BC": serverPort = 7000; break;
case "QC": serverPort = 8000; break;
default: serverPort = 8000;
}
// Create socket
DatagramSocket mySocket = null;
try {
mySocket = new DatagramSocket();
String dataToSend = customerID + "," + itemID + "," + day + "," + month + "," + year;
// Byte array for the item ID
byte[] item = dataToSend.getBytes();
InetAddress host = InetAddress.getLocalHost();
// Create packet
DatagramPacket request = new DatagramPacket(item, dataToSend.length(), host, serverPort);
// Send packet
mySocket.send(request);
System.out.println("Purchase request to " + store + " for " + itemID + " sent");
byte[] buffer = new byte[1000]; // Byte array to receive response
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
mySocket.receive(reply);
return new String(reply.getData()); // Receive status message from server
} catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mySocket != null) mySocket.close();
}
return "Operation was unsuccessful";
}
public static void setExchange(boolean b) {
exchange = b;
}
private String sendFindItemRequest(int serverPort, String customerID, String itemName) {
// Create socket
DatagramSocket mySocket = null;
try {
mySocket = new DatagramSocket();
String dataToSend = customerID + "," + itemName + ",f";
// Byte array for the item ID
byte[] item = dataToSend.getBytes();
InetAddress host = InetAddress.getLocalHost();
// Create packet
DatagramPacket request = new DatagramPacket(item, dataToSend.length(), host, serverPort);
// Send packet
mySocket.send(request);
System.out.println("Find request for " + itemName + " sent to " + serverPort);
byte[] buffer = new byte[1000]; // Byte array to receive response
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
mySocket.receive(reply);
return new String(reply.getData()); // Receive status message from server
} catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO: " + e.getMessage());
} finally {
if (mySocket != null) mySocket.close();
}
return "Operation was unsuccessful";
}
private String sendReturnRequest(String customerID, String itemID) {
int serverPort = 0;
// Get port for item's store
switch(itemID.substring(0,2)) {
case "ON": serverPort = 9000; break;
case "BC": serverPort = 7000; break;
case "QC": serverPort = 8000; break;
default: serverPort = 8000;
}
// Create socket
DatagramSocket mySocket = null;
try {
mySocket = new DatagramSocket();
String dataToSend = customerID + "," + itemID + ",r";
// Byte array for the item ID
byte[] item = dataToSend.getBytes();
InetAddress host = InetAddress.getLocalHost();
// Create packet
DatagramPacket request = new DatagramPacket(item, dataToSend.length(), host, serverPort);
// Send packet
mySocket.send(request);
System.out.println("Return request for " + itemID + " sent to " + itemID.substring(0,2));
byte[] buffer = new byte[1000]; // Byte array to receive response
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
mySocket.receive(reply);
return new String(reply.getData()); // Receive status message from server
} catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO: " + e.getMessage());
} finally {
if (mySocket != null) mySocket.close();
}
return "Operation was unsuccessful";
}
private void receive() {
DatagramSocket mySocket = null;
try {
mySocket = new DatagramSocket(port);
byte[] buffer = new byte[1000];
System.out.println("Server for port " + port + " ready");
while (true) {
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
mySocket.receive(request);
String rq = new String(request.getData()).trim();
System.out.println("Message received: " + rq);
byte[] serverResponse = new byte[1000];
if(rq.matches("(ON|BC|QC)U[0-9]{4}([0-9.]+)*,(ON|BC|QC)[0-9]{4}(\\swait)*,[0-9]{1,2},[0-9]{1,2},[0-9]{4}")){ // Purchase item request
String[] s = rq.split(",");
a3.Date d = new a3.Date(Short.parseShort(s[2]), Short.parseShort(s[3]), Short.parseShort(s[4]));
double a = purchaseItem(s[0], s[1], d);
serverResponse = new String("" + a).getBytes();
}
else if(rq.matches("(ON|BC|QC)U[0-9]{4},(\\w\\s*)+,f")) { // Find item request
String[] s = rq.split(",");
for(Item a : inventory.values()) {
if(a.itemName.equalsIgnoreCase(s[1])) {
itemsFound.add(a.itemID + " " + a.quantity + " " + a.price + "\n");
}
}
serverResponse = new String("").getBytes();
}
else if(rq.matches("(ON|BC|QC)U[0-9]{4}([0-9.]+)*,(ON|BC|QC)[0-9]{4},r")) { // Return item request
String[] s = rq.split(",");
double a = returnItem(s[0], s[1], a3.Date.getCurrentDate());
serverResponse = new String("" + a).getBytes();
}
else if(rq.matches("(ON|BC|QC)U[0-9]{4}([0-9.]+)*,(ON|BC|QC)[0-9]{4},purchase")) { // Exchange item request
// Perform checks for purchase
boolean success = false;
String[] s = rq.split(",");
double budget = Double.parseDouble(s[0].substring(7));
if(inventory.containsKey(s[1])) {
Item i = inventory.get(s[1]);
if(i.quantity > 0 && i.price < budget)
{
System.out.println("Purchase verification succeeded");
success = true;
} else System.out.println("Purchase verification failed");
}
serverResponse = (success)?new String("true").getBytes():new String("false").getBytes();
}
else if(rq.matches("(ON|BC|QC)U[0-9]{4},(ON|BC|QC)[0-9]{4},return")) { // Exchange item request
// Perform checks for returns
boolean success = false;
String dateString = "";
String[] s = rq.split(",");
DSMSApp.Date dateOfReturn = DSMSApp.Date.getCurrentDate();
if(inventory.containsKey(s[1])) {
Item i = inventory.get(s[1]);
if(i.IDOfBuyer.contains(s[0])){
Calendar c = Calendar.getInstance();
c.set(i.dateOfPurchase.getDay(), i.dateOfPurchase.getMonth(), i.dateOfPurchase.getYear());
int dayOfYear = c.get(Calendar.DAY_OF_YEAR);
c.set(dateOfReturn.Day, dateOfReturn.Month, dateOfReturn.Year);
int dayOfYearReturn = c.get(Calendar.DAY_OF_YEAR);
// If the item was purchased by this customer within the past 30 days, they can return it
if((dayOfYearReturn - dayOfYear < 30) && (dayOfYearReturn - dayOfYear >= 0)){
success = true;
dateString = i.dateOfPurchase.getDay() + "-" + i.dateOfPurchase.getMonth() + "-" + i.dateOfPurchase.getYear();
System.out.println("Return verification succeeded");
}
else System.out.println("Return verification failed");
}
}
serverResponse = (success)?new String(dateString).getBytes():new String("false").getBytes();
}
DatagramPacket reply = new DatagramPacket(serverResponse, serverResponse.length, request.getAddress(), request.getPort());
mySocket.send(reply);
System.out.println("Reply sent!");
buffer = new byte[1000];
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mySocket != null) mySocket.close();
}
}
}