-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSMSCustomerClient.java
178 lines (145 loc) · 4.98 KB
/
DSMSCustomerClient.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
package a3;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Date;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceRef;
public class DSMSCustomerClient {
String customerID;
double budget;
String store;
File log;
WebInterface service;
public DSMSCustomerClient(String[] args, String ID) {
if(ID.charAt(2) != 'U') {
System.out.println("User IDs must be in the format: [provinceAcronym]U[4-digit ID]");
return;
}
customerID = ID;
budget = 1000.00;
store = ID.substring(0,2);
try {
log = new File(ID + "log.txt");
if(log.createNewFile()) {
System.out.println("File created for customer #" + ID);
}
QName compQName = new QName("http://a3/", "DSMSImplService");
if(store.equals("QC")) {
URL compURL = new URL("http://localhost:8080/a3/WebInterfaceQC?wsdl");
Service compService = Service.create(compURL, compQName);
service = compService.getPort(WebInterface.class);
} else if(store.equals("ON")) {
URL compURL = new URL("http://localhost:8080/a3/WebInterfaceON?wsdl");
Service compService = Service.create(compURL, compQName);
service = compService.getPort(WebInterface.class);
}
else if(store.equals("BC")) {
URL compURL = new URL("http://localhost:8080/a3/WebInterfaceBC?wsdl");
Service compService = Service.create(compURL, compQName);
service = compService.getPort(WebInterface.class);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public int purchase(String itemID, a3.Date dateOfPurchase) throws RemoteException {
int status = 1;
String custAndBudget = this.customerID + budget;
double price = service.purchaseItem(custAndBudget, itemID, dateOfPurchase);
if(price > 0 && this.budget > price) { // If purchase was successful, subtract from budget
this.budget -= price;
}
else if(price == 0.0) {
status = 0;
System.out.println("User " + this.customerID + " could not purchase item " + itemID);
}
else if(price == -1){ // If the item is not in stock, ask the customer if they want to be added to the list
status = -1;
}
// Write to file
synchronized(log) {
try {
FileWriter writeUser = new FileWriter(log, true);
writeUser.append("\nREQUEST:\n" + "\tDate of purchase: " + dateOfPurchase
+ "\n\tType: purchase" + "\n\tSubmitted by: " + customerID
+ "\n\tItem ID: " + itemID);
writeUser.append(status==1?"\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
writeUser.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return status;
}
public boolean find(String itemName) throws RemoteException {
boolean nothingFound = true;
String found = service.findItem(this.customerID,itemName);
if(!found.equals("")) {
String[] founds = found.split(",");
for(String i : founds) {
i.trim();
//if(i.matches("[\\s\\[\\]]*[0-9A-Za-z\\s.]+")) {
System.out.println(i);
nothingFound = false;
//}
}
}
// Write to file
synchronized(log) {
try {
FileWriter writeUser = new FileWriter(log, true);
writeUser.append("\nREQUEST:\n" + "\tDate: " + new Date()
+ "\n\tType: search" + "\n\tSubmitted by: " + customerID
+ "\n\tItem name: " + itemName);
writeUser.append(nothingFound?"\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
writeUser.close();
} catch (IOException e) {
e.printStackTrace();
}}
return !nothingFound;
}
public boolean returnItem(String itemID, a3.Date dateOfReturn) throws RemoteException {
boolean status = true;
double price = service.returnItem(this.customerID, itemID, dateOfReturn);
if(price > 0) this.budget+=price;
else status = false;
// Write to file
synchronized(log) {
try {
FileWriter writeUser = new FileWriter(log, true);
writeUser.append("\nREQUEST:\n" + "\tDate of return: " + dateOfReturn
+ "\n\tType: return" + "\n\tSubmitted by: " + customerID
+ "\n\tItem ID: " + itemID);
writeUser.append(status?"\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
writeUser.close();
} catch (IOException e) {
e.printStackTrace();
}}
return status;
}
public boolean exchangeItem(String customerID, String newItemID, String oldItemID) throws RemoteException {
boolean status = true;
String custAndBudget = this.customerID + budget;
status = service.exchangeItem(custAndBudget, newItemID, oldItemID);
// Write to file
synchronized(log) {
try {
FileWriter writeUser = new FileWriter(log, true);
writeUser.append("\nREQUEST:\n" + "\tDate of exchange: " + a3.Date.getCurrentDate()
+ "\n\tType: return" + "\n\tSubmitted by: " + customerID
+ "\n\tOld Item ID: " + oldItemID
+ "\n\tNew Item ID: " + newItemID
);
writeUser.append(status?"\n\tStatus: successful":"\n\tStatus: unsuccessful\n");
writeUser.close();
} catch (IOException e) {
e.printStackTrace();
}}
return status;
}
}