-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.java
More file actions
490 lines (452 loc) · 16.6 KB
/
Copy pathTransaction.java
File metadata and controls
490 lines (452 loc) · 16.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
import java.sql.*;
import java.util.*;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
public class Transaction {
/** Reads user input from stdin */
static Scanner scan = new Scanner(System.in);
/** Current menu choice */
static int input = 0;
/** Return value of SQL queries */
static ResultSet rs = null;
/** Transaction table schema */
static int transactionID = -1;
static int storeID = -1;
static int memberID = -1;
static int cashierID = -1;
static Date date = Date.valueOf("0001-01-01");
static int productID = -1;
static double price = -1.00;
static int quantity = -1;
static double total = -1.00;
/**
* Displays main menu for Transaction table and delegates user input to Transaction action methods
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void transactionMenu() throws ClassNotFoundException, SQLException, ParseException
{
do {
System.out.println("Transaction Menu");
System.out.println();
System.out.println("0. Return to Information Processing Menu");
System.out.println("1. View All Transactions");
System.out.println("2. View Transaction by ID");
System.out.println("3. Add Transaction");
System.out.println("4. Edit Transaction");
System.out.println("5. Delete Transaction");
System.out.println();
System.out.println("Please choose an option from the menu: ");
input = scan.nextInt();
switch(input) {
case 0:
System.out.println("Going back to Information Processing Menu");
return;
case 1:
viewAllTransactions();
break;
case 2:
viewTransaction();
break;
case 3:
addOrEdit("add");
break;
case 4:
edit();
break;
case 5:
delete();
break;
default:
System.out.println("Invalid input");
break;
}
input = -1;
} while(input != 0);
}
/**
* Displays menu and calls SQL for viewing all Transaction tuples
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void viewAllTransactions() throws ClassNotFoundException, SQLException, ParseException
{
try {
ResultSet rs = TransactionSQL.viewAllTransactions();
printTransaction(rs);
} catch(SQLException e) {
System.out.println("SQL Exception: " + e.getMessage());
}
}
/**
* Displays menu and calls SQL for viewing a single Transaction tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void viewTransaction() throws ClassNotFoundException, SQLException, ParseException
{
do {
System.out.println("In order to go back to Transaction Menu, enter 0");
System.out.println("Please enter a Transaction ID to view a Transaction");
System.out.println();
System.out.println("Transaction ID: ");
input = scan.nextInt();
scan.nextLine();
if (input > 0) {
ResultSet rs = TransactionSQL.viewTransaction(input);
printTransaction(rs);
rs.close();
} else if (input < 0) {
System.out.println("Invalid input");
} else if (input == 0) {
System.out.println("Going back to Transaction Menu");
return;
}
} while (input != 0);
}
/**
* Displays menu and calls SQL for editing a Transaction tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void edit() throws ClassNotFoundException, SQLException, ParseException
{
input = 0;
do {
System.out.println("Enter a Transaction ID to edit a Transaction or enter 0 to return to Transaction Menu");
System.out.println();
System.out.println("Transaction ID: ");
input = scan.nextInt();
scan.nextLine();
System.out.println();
if (input > 0) {
rs = TransactionSQL.viewTransaction(input);
if (!rs.next()) {
System.out.println("Transaction ID does not exist.");
} else {
addOrEdit("edit");
}
} else if (input == 0) {
System.out.println("Going back to Transaction Menu");
return;
} else {
System.out.println("Invalid Transaction ID");
}
} while (input != 0);
}
/**
* Displays menu and calls SQL for deleting a Transaction tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void delete() throws ClassNotFoundException, SQLException, ParseException
{
do{
System.out.println("Delete Transaction Menu:");
System.out.println("Enter Transaction ID to delete a Transaction or enter 0 to return to Transaction Menu");
System.out.println();
System.out.println("Transaction ID: ");
input = scan.nextInt();
scan.nextLine();
System.out.println();
if (input > 0) {
try {
TransactionSQL.deleteTransaction(input);
} catch (SQLException e) {
e.printStackTrace();
}
} else if (input < 0) {
System.out.println("Invalid input");
} else if (input == 0) {
System.out.println("Returning to Transaction Menu");
return;
}
} while (input != 0);
}
/**
* Prints to stdout all tuples held in the static ResultSet
* @param rs
* @throws SQLException
*/
public static void printTransaction(ResultSet rs) throws SQLException
{
if (!rs.next()) {
System.out.println("There is no Transaction with this transactionID.");
} else {
do {
transactionID = rs.getInt("transactionID");
storeID = rs.getInt("storeID");
memberID = rs.getInt("memberID");
cashierID = rs.getInt("cashierID");
date = rs.getDate("date");
productID = rs.getInt("productID");
price = rs.getDouble("price");
quantity = rs.getInt("quantity");
total = rs.getDouble("total");
System.out.println("Transaction ID: " + transactionID + ", Store ID: " + storeID + ", MemberID: " + memberID + ", Cashier ID: " + cashierID + ", Date: " + date + ", Product ID: " + productID + ", Price: " + price + ", Quantity: " + quantity + ", Total: " + total);
} while (rs.next());
}
}
/**
* Parses input to allow user to set desired attributes for either INSERT or UPDATE operation.
* @param mode Determines whether to "add" new tuple or "edit" existing tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void addOrEdit(String mode) throws ClassNotFoundException, SQLException, ParseException
{
int input = 0;
if (mode.equals("edit")) {
transactionID = rs.getInt("transactionID");
storeID = rs.getInt("storeID");
memberID = rs.getInt("memberID");
cashierID = rs.getInt("cashierID");
date = rs.getDate("date");
productID = rs.getInt("productID");
price = rs.getDouble("price");
quantity = rs.getInt("quantity");
total = rs.getDouble("total");
}
do {
System.out.println("Enter a number to " + mode + " transaction attributes or enter 0 to return to Transaction Menu");
System.out.println("0. Return to Transaction Menu");
System.out.println("1. Transaction ID: " + transactionID);
System.out.println("2. Store ID: " + storeID);
System.out.println("3. Member ID: " + memberID);
System.out.println("4. Cashier ID: " + cashierID);
System.out.println("5. Date: " + date);
System.out.println("6. Product ID: " + productID);
System.out.println("7. Price: " + price);
System.out.println("8: Quantity: " + quantity);
System.out.println("9. Total: " + total);
System.out.println("10. After all attributes have been entered, select 10 to " + mode + " Transaction");
input = scan.nextInt();
scan.nextLine();
System.out.println();
switch(input) {
case 0:
System.out.println("Going back to Transaction Menu");
System.out.println();
resetAttributes();
return;
case 1:
System.out.println("Enter Transaction ID: ");
transactionID = scan.nextInt();
break;
case 2:
System.out.println("Enter Store ID: ");
storeID = scan.nextInt();
break;
case 3:
System.out.println("Enter Member ID: ");
memberID = scan.nextInt();
break;
case 4:
System.out.println("Enter Cashier ID: ");
cashierID = scan.nextInt();
break;
case 5:
System.out.println("Enter Date in format YYYY-MM-DD: ");
try{
date = Date.valueOf(scan.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("Date format incorrect, please try again.");
}
break;
case 6:
System.out.println("Enter Product ID: ");
productID = scan.nextInt();
break;
case 7:
System.out.println("Enter Price: ");
price = scan.nextDouble();
break;
case 8:
System.out.println("Enter Quantity: ");
quantity = scan.nextInt();
break;
case 9:
System.out.println("Enter Total: ");
total = scan.nextDouble();
break;
case 10:
try{
if (mode.equals("add")) {
TransactionSQL.addTransaction(transactionID, storeID, memberID, cashierID, date, productID, price, quantity, total);
} else if (mode.equals("edit")) {
TransactionSQL.editTransaction(transactionID, storeID, memberID, cashierID, date, productID, price, quantity, total);
}
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
break;
}
rs = TransactionSQL.viewTransaction(transactionID);
printTransaction(rs);
resetAttributes();
input = -1; // We're done, back to Transaction Menu
return;
default:
System.out.println("Invalid input");
break;
}
} while(input != 0);
}
/**
* Menu for Total Sales report. Prompts user for date range then calls SQL method to get sum.
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void totalSales() throws ClassNotFoundException, SQLException, ParseException
{
do {
String rangeStart = "0001-01-01";
String rangeEnd = "0001-01-01";
int id = -1;
boolean isStore = false;
boolean isMember = false;
System.out.println("Report: Sales and Purchase Reports");
System.out.println();
System.out.println("0. Return to Reports Menu");
System.out.println("1. View total sales by day");
System.out.println("2. View total sales by month");
System.out.println("3. View total sales by year");
System.out.println();
System.out.print("Please choose an option from the menu: ");
input = scan.nextInt();
if (input != 0) {
System.out.println("Is this for a specific Store? (y/n)");
isStore = scan.next().toLowerCase().equals("y");
if (!isStore) {
System.out.println("Do you want a Member purchase report?(y/n)");
isMember = scan.next().toLowerCase().equals("y");
}
}
if (isStore) {
System.out.println("Please enter the ID of the store: ");
id = scan.nextInt();
} else if (isMember) {
System.out.println("Please enter the ID of the member: ");
id = scan.nextInt();
}
switch(input) {
case 0:
System.out.println("Going back to Reports Menu");
return;
case 1:
System.out.println("Please input Start Date in the format YYYY-MM-DD:");
rangeStart = scan.next();
System.out.println("Please input End Date in the format YYYY-MM-DD:");
rangeEnd = scan.next();
break;
case 2:
System.out.println("Please input Start year in the format YYYY");
rangeStart = String.valueOf(scan.nextInt()) + rangeStart.substring(4);
System.out.println("Please input Start month in the format MM");
rangeStart = rangeStart.toString().substring(0, 5) + String.valueOf(scan.nextInt()) + "-01";
System.out.println("Please input End year in the format YYYY");
rangeEnd = String.valueOf(scan.nextInt()) + rangeEnd.substring(4);
System.out.println("Please input End month in the format MM");
rangeEnd = rangeEnd.substring(0, 5) + String.valueOf(scan.nextInt()) + "-01";
break;
case 3:
System.out.println("Please input Start year in the format YYYY");
rangeStart = String.valueOf(scan.nextInt()) + "-01-01";
System.out.println("Please input End year in the format YYYY");
rangeEnd = String.valueOf(scan.nextInt()) + "-01-01";
break;
}
rs = TransactionSQL.totalSalesReport(isStore, isMember, id, rangeStart, rangeEnd);
rs.next();
if (isStore) {
System.out.println("Total Sales for Store " + id + " for period between " + rangeStart + " and " + rangeEnd + ": $" + rs.getDouble("SUM(total)"));
} else if (isMember) {
System.out.println("Total Sales for Member " + id + " for period between " + rangeStart + " and " + rangeEnd + ": $" + rs.getDouble("SUM(total)"));
} else {
System.out.println("Total Sales for period between " + rangeStart + " and " + rangeEnd + ": $" + rs.getDouble("SUM(total)"));
}
System.out.println();
} while (input != 0);
}
/**
* Method is called in the MainMenu file under maintainBillingTransactions. This method will prompt user
* for a transactionID to be passed to transactionTotal in TransactionSQL to generate the query.
*/
public static void transactionTotal() throws ParseException, ClassNotFoundException, SQLException{
do {
System.out.println("In order to go back to Billing Menu, enter 0");
System.out.println("Generate total price for each transaction, enter 1");
input = scan.nextInt();
if(input == 0){
System.out.println("Going back to Billing Menu");
return;
} else if (input == 1){
System.out.println("In order to generate the total price of a transaction, please enter a transactionID");
transactionID = scan.nextInt();
rs = TransactionSQL.transactionTotal(transactionID);
if(!rs.next() || rs.getDouble("SUM(total)") == 0){
System.out.println("There is no transaction with this transactionID");
} else{
System.out.println("Total Price for Transaction: $" + rs.getDouble("SUM(total)"));
}
rs.close();
} else{
System.out.println("Invalid input");
}
} while(input != 0);
}
/**
* Prompts user for Transaction ID and then checks all products for applicable discount. Discounts are automatically applied when transaction is created, this is just listing them.
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void checkDiscounts() throws ClassNotFoundException, SQLException, ParseException
{
do{
System.out.println("0. Return to Billing Menu");
System.out.println("1. Check Transaction for applicable Discounts");
input = scan.nextInt();
if (input == 0) {
System.out.println("Going back to Billing Menu");
return;
} else if (input == 1) {
System.out.println("Please input the Transaction ID to check for discounts: ");
int transactionID = scan.nextInt();
ResultSet rs = TransactionSQL.checkDiscounts(transactionID);
if (rs.next()) {
System.out.println("Discounts applied:");
do {
System.out.println("Transaction: " + rs.getInt("transactionID") + ", Product: " + rs.getInt("productID") + ", Discount: " + rs.getDouble("priceReduction") + "%");
} while (rs.next());
} else {
System.out.println("No applicable Discounts");
}
} else {
System.out.println("Invalid input");
}
} while (input != 0);
}
/**
* Resets Transaction attributes back to default.
*/
public static void resetAttributes()
{
transactionID = -1;
storeID = -1;
memberID = -1;
cashierID = -1;
date = Date.valueOf("0001-01-01");
productID = -1;
price = -1.00;
quantity = -1;
total = -1.00;
}
}