-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarketRun.java
More file actions
585 lines (515 loc) · 31.6 KB
/
MarketRun.java
File metadata and controls
585 lines (515 loc) · 31.6 KB
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
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.Runnable;
import java.text.*;
public class MarketRun extends Thread implements Runnable {
private DataInputStream socketReader;
private DataOutputStream socketWriter;
private Socket newSocket;
private MarketServer server;
private volatile boolean running = true;
public static HashMap <String, Sellers> sellers = new HashMap<String, Sellers>();
public static HashMap <String, Customers> customers = new HashMap<String, Customers>();
public MarketRun(Socket newSocket, DataInputStream socketReader, DataOutputStream socketWriter, MarketServer server) throws IOException {
this.newSocket = newSocket;
this.socketReader = socketReader;
this.socketWriter = socketWriter;
this.server = server;
}
public void run() {
try {
populateHashMaps();
} catch (IOException e) {
e.printStackTrace();
}
while (running) {
try {
String user = "";
final boolean[] validUser = {false};
do {
int userNum = 0;
if (socketReader.available() > 0) {
user = socketReader.readUTF();
userNum = 0;
if (user.equals("Customer")) {
userNum = 1;
} else if (user.equals("Seller")) {
userNum = 2;
} else if (user.equals("Create Account")) {
userNum = 3;
}
}
if (userNum == 1) {
boolean loggedIn = false;
do {
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
String username = socketReader.readUTF();
String password = socketReader.readUTF();
//User authentication (goes through users.txt file, validates password) PROGRAM QUITS AFTER AUTHENTICATION
String line;
try {
line = br.readLine();
while (line != null) {
String[] lineSplit = line.split(";");
if (lineSplit[1].equals(username)) {
if (lineSplit[2].equals(password)) {
loggedIn = true;
validUser[0] = true;
socketWriter.writeUTF("loggedIn");
}
}
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
//Customer homepage
if (loggedIn) {
int selectionNum = 0;
do {
String selectionInfo = socketReader.readUTF();
String[] selectionSplit = selectionInfo.split(". ");
selectionNum = Integer.parseInt(selectionSplit[0]);
switch (selectionNum) {
//View marketplace QUITS WHEN VIEW MARKETPLACE
case 1:
ArrayList<String> market = new ArrayList<String>();
market = printMarket(); //problem, why?
String marketplace = "";
for (int i = 0; i < market.size(); i++) {
marketplace += market.get(i) + "\n";
}
if (marketplace.equals("")) {
marketplace = "There is nothing currently in the marketplace.";
}
System.out.println(marketplace);
socketWriter.writeUTF(marketplace);
int sort = Integer.parseInt(socketReader.readUTF());
if (sort == 0) {
int sortingNum = Integer.parseInt(socketReader.readUTF());
if (sortingNum == 1) {
ArrayList<String> sortedMarket = (customers.get(username)).sortByPrice(market);
marketplace = null;
for (int i = 0; i < sortedMarket.size(); i++) {
marketplace += sortedMarket.get(i) + "\n";
}
if (marketplace == null) {
marketplace = "There is nothing currently in the marketplace.";
}
socketWriter.writeUTF(marketplace);
} else if (sortingNum == 2) {
ArrayList<String> sortedMarket = (customers.get(username)).sortByQuantity(market);
marketplace = null;
for (int i = 0; i < sortedMarket.size(); i++) {
marketplace += sortedMarket.get(i) + "\n";
}
if (marketplace == null) {
marketplace = "There is nothing currently in the marketplace.";
}
socketWriter.writeUTF(marketplace);
}
} else {
}
if (marketplace != null) {
String selectedProduct = socketReader.readUTF();
String viewProduct = viewProduct(selectedProduct);
socketWriter.writeUTF(viewProduct);
int purchasing = 0;
if (!viewProduct.isEmpty()) {
purchasing = Integer.parseInt(socketReader.readUTF());
if (purchasing == 0) {
int availableQuantity = checkQuantity(selectedProduct);
int quantity = Integer.parseInt(socketReader.readUTF());
String available = "no";
if (availableQuantity - quantity >= 0) {
productPurchased(selectedProduct, username, quantity);
double price = checkPrice(selectedProduct);
String store = checkStore(selectedProduct);
String description = checkDescription(selectedProduct);
(customers.get(username)).updatePurchaseHistory(selectedProduct, store, description, quantity, price);
available = "yes";
socketWriter.writeUTF(available);
} else {
socketWriter.writeUTF(available);
}
}
}
}
break;
//Search for product
case 2:
ArrayList<String> newMarket = new ArrayList<String>();
newMarket = printMarket();
int choiceNum = Integer.parseInt(socketReader.readUTF());
if (choiceNum == 1) {
String product = socketReader.readUTF();
(customers.get(username)).searchProductName(newMarket, product);
//writeUTF it back to client
} else if (choiceNum == 2) {
String store = socketReader.readUTF();
(customers.get(username)).searchProductStore(newMarket, store);
} else if (choiceNum == 3) {
String description = socketReader.readUTF();
(customers.get(username)).searchProductDescription(newMarket, description);
}
break;
case 3:
String purchaseHistory = (customers.get(username)).getPurchaseHistory();
socketWriter.writeUTF(purchaseHistory);
break;
//Logout
case 4:
break;
default:
}
} while (selectionNum != 4);
} else {
}
} while (!loggedIn);
//Seller section LOGIN SECTION NEEDS TO BE I/O
} else if (userNum == 2) {
boolean loggedIn = false;
do {
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
String username = socketReader.readUTF();
String password = socketReader.readUTF();
//User authentication (goes through users.txt file, validates password) PROGRAM QUITS AFTER AUTHENTICATION
String line;
try {
line = br.readLine();
while (line != null) {
String[] lineSplit = line.split(";");
if (lineSplit[1].equals(username)) {
if (lineSplit[2].equals(password)) {
loggedIn = true;
validUser[0] = true;
socketWriter.writeUTF("loggedIn");
}
}
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
if (loggedIn) {
String selection;
int selectionNum = 0;
//Seller homepage
do {
selection = socketReader.readUTF();
selectionNum = Integer.parseInt(selection);
switch (selectionNum) {
//Create product
case 1:
String product = socketReader.readUTF();
String[] storesArray = (sellers.get(username).getStores()).split(", ");
String allStores = "";
for (String store : storesArray) {
allStores = allStores + store + ",";
}
socketWriter.writeUTF(allStores);
String productStore = socketReader.readUTF();
boolean storeExists = storeExists(productStore);
if (storeExists) {
socketWriter.writeUTF("yes");
} else {
socketWriter.writeUTF("no");
}
if (storeExists) {
//Get all info for product, add product
String description = socketReader.readUTF();
int quantity = Integer.parseInt(socketReader.readUTF());
double price = Double.parseDouble(socketReader.readUTF());
sellers.get(username).addProduct(product, productStore, description, quantity, price);
}
break;
//Edit/Delete product
case 2:
String choice = socketReader.readUTF();
String[] choiceSplit = choice.split(". ");
int choiceNum = Integer.parseInt(choiceSplit[0]);
// Edit product (deletes, then adds with changes)
if (choiceNum == 1) {
String[] productsArray = (sellers.get(username).getProducts()).split(", ");
String allProducts = "";
for (String products : productsArray) {
allProducts = allProducts + products + ", ";
}
socketWriter.writeUTF(allProducts);
String editProduct = socketReader.readUTF();
String newProduct = socketReader.readUTF();
String[] storesArrays = (sellers.get(username).getStores()).split(", ");
String newAllStores = "";
for (String store : storesArrays) {
newAllStores = newAllStores + store + ", ";
}
socketWriter.writeUTF(newAllStores);
String newProductStore = socketReader.readUTF();
boolean newStoreExists = storeExists(newProductStore);
if (newStoreExists) {
socketWriter.writeUTF("yes");
} else {
socketWriter.writeUTF("no");
}
if (newStoreExists) {
// Get all info for product, add product
String newDescription = socketReader.readUTF();
int newQuantity = Integer.parseInt(socketReader.readUTF());
double newPrice = Double.parseDouble(socketReader.readUTF());
sellers.get(username).deleteProduct(editProduct);
sellers.get(username).addProduct(newProduct, newProductStore, newDescription, newQuantity, newPrice);
} else {
socketWriter.writeUTF("That store doesn't exist!");
}
// Deletes product
} else if (choiceNum == 2) {
String[] productsArray = (sellers.get(username).getProducts()).split(", ");
String allProducts = "";
for (String products : productsArray) {
allProducts = allProducts + products + ", ";
}
socketWriter.writeUTF(allProducts);
String unwantedProduct = socketReader.readUTF();
sellers.get(username).deleteProduct(unwantedProduct);
}
break;
//Create store
case 3:
String storeName = socketReader.readUTF();
(sellers.get(username)).addStore(storeName);
break;
//View Stores
case 4:
String everyStore = (sellers.get(username)).getStores();
System.out.println(everyStore);
socketWriter.writeUTF(everyStore);
break;
//View Seller Dashboard NEEDS TO BE CONVERTED TO I/O
case 5:
String sellerStores = (sellers.get(username)).getStores();
String[] stores = sellerStores.split(", ");
SellersDashboard sellersDashboard = new SellersDashboard(stores);
sellersDashboard.viewDashboard();
break;
//Logout
case 6:
break;
default:
}
} while (selectionNum != 6);
} else {
JOptionPane.showMessageDialog(null, "Login failed!", "Error",
JOptionPane.ERROR_MESSAGE);
}
} while (!loggedIn);
//User creation system
//Add new user authentication?
} else if (userNum == 3) {
boolean userTaken;
String username;
do {
username = JOptionPane.showInputDialog(null, "Please enter a username:",
"Create Account", JOptionPane.QUESTION_MESSAGE);
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
String line = br.readLine();
userTaken = false;
//Makes sure username is not taken
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[1].equals(username)) {
userTaken = true;
JOptionPane.showMessageDialog(null, "This username is taken!", "Error",
JOptionPane.ERROR_MESSAGE);
}
line = br.readLine();
}
} while (userTaken);
String password = JOptionPane.showInputDialog(null, "Please enter a password:",
"Create Account", JOptionPane.QUESTION_MESSAGE);
String[] userTypeArray = {"1. Customer", "2. Seller"};
String userType = (String) JOptionPane.showInputDialog(null, "\"Are you a...",
"Create Account", JOptionPane.QUESTION_MESSAGE, null, userTypeArray, userTypeArray[0]);
int userTypeNum = Integer.parseInt(String.valueOf(userType.charAt(0)));
if (userTypeNum == 1) {
try {
addUser("Customer", username, password);
} catch (IOException e) {
e.printStackTrace();
}
} else if (userTypeNum == 2) {
try {
addUser("Seller", username, password);
} catch (IOException e) {
e.printStackTrace();
}
} else {
}
} else {
validUser[0] = false;
}
} while (!validUser[0]);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if (socketReader != null) {
socketReader.close();
}
if (socketWriter != null) {
socketWriter.close();
}
if (newSocket != null) {
newSocket.close();
}
server.stopThread(Thread.currentThread());
} catch (IOException e) {
e.printStackTrace();
}
}
public void stopThread() {
running = false;
}
//This will eventually print the marketplace for customers
public static ArrayList<String> printMarket() throws IOException {
ArrayList<String> market = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
while (line != null) {
String[] splitLine = line.split(";");
if (Integer.parseInt(splitLine[4]) > 0) {
market.add(splitLine[2] + " | $" + splitLine[5] + " | " + splitLine[1]);
}
line = br.readLine();
}
return market;
}
public static String viewProduct(String product) throws IOException {
String productView = "";
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[2].equals(product)) {
productView = splitLine[2] + "\nPrice: $" + splitLine[5] + "\nQuantity Available: " + splitLine[4] + "\nDescription: " + splitLine[3] + "\nStore: " + splitLine[1];
}
line = br.readLine();
}
return productView;
}
public static int checkQuantity(String product) throws IOException {
int quantity = 0;
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[2].equals(product)) {
quantity = Integer.parseInt(splitLine[4]);
}
line = br.readLine();
}
return quantity;
}
public static void productPurchased(String product, String username, int quantity) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
int newQuantity = 0;
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[2].equals(product)) {
newQuantity = Integer.parseInt(splitLine[4]) - quantity;
}
if (newQuantity > 0 && !product.isEmpty()) {
(sellers.get(splitLine[0])).deleteProduct(product);
(sellers.get(splitLine[0])).addProduct(splitLine[2], splitLine[1], splitLine[3], newQuantity, Double.parseDouble(splitLine[5]));
} else if (newQuantity == 0) {
(sellers.get(splitLine[0])).deleteProduct(product);
}
line = br.readLine();
}
}
public static String checkDescription(String product) throws IOException{
String description = "";
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[2].equals(product)) {
description = splitLine[3];
}
line = br.readLine();
}
return description;
}
public static double checkPrice(String product) throws IOException{
double price = 0;
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[2].equals(product)) {
price = Double.parseDouble(splitLine[5]);
}
line = br.readLine();
}
return price;
}
public static String checkStore(String product) throws IOException{
String store = "";
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[2].equals(product)) {
store = splitLine[1];
}
line = br.readLine();
}
return store;
}
public static boolean storeExists(String store) throws IOException{
boolean storeExists = false;
BufferedReader br = new BufferedReader(new FileReader("stores.txt"));
String line = br.readLine();
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[1].equals(store)) {
storeExists = true;
}
line = br.readLine();
}
return storeExists;
}
//Creates new users and adds them to hashmaps
//EVENTUALLY get rid of file system, authenticate with hashmaps instead
public static void addUser(String userType, String username, String password) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("users.txt", true), true);
pw.write(userType + ";" + username + ";" + password);
pw.println();
pw.close();
//"put" adds them to the hashmaps, with the specified username,
//and the specified user profile
if (userType.equals("Seller")) {
Sellers newSeller = new Sellers(username, password);
sellers.put(username, newSeller);
} else {
Customers newCustomer = new Customers(username, password);
customers.put(username, newCustomer);
}
}
public static void populateHashMaps() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
String line = br.readLine();
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[0].equals("Seller")) {
sellers.put(splitLine[1], new Sellers(splitLine[1], splitLine[2]));
} else {
customers.put(splitLine[1], new Customers(splitLine[1], splitLine[2]));
}
line = br.readLine();
}
}
}