-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiscount.java
306 lines (284 loc) · 9.34 KB
/
Discount.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
import java.sql.*;
import java.util.*;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
public class Discount {
/** 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;
/** Discount table schema */
static int discountID = -1;
static int productID = -1;
static double priceReduction = -1.00;
static Date startDate = Date.valueOf("0001-01-01");
static Date endDate = Date.valueOf("0001-01-01");
/**
* Displays main menu for Discount table and delegates user input to Discount action methods
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void discountMenu() throws ClassNotFoundException, SQLException, ParseException
{
do {
System.out.println("Discount Menu");
System.out.println();
System.out.println("0. Return to Information Processing Menu");
System.out.println("1. View All Discounts");
System.out.println("2. View Discount by ID");
System.out.println("3. Add Discount");
System.out.println("4. Edit Discount");
System.out.println("5. Delete Discount");
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:
viewAllDiscounts();
break;
case 2:
viewDiscount();
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 Discount tuples
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void viewAllDiscounts() throws ClassNotFoundException, SQLException, ParseException
{
try {
ResultSet rs = DiscountSQL.viewAllDiscounts();
printDiscount(rs);
} catch(SQLException e) {
System.out.println("SQL Exception: " + e.getMessage());
}
}
/**
* Displays menu and calls SQL for viewing a single Discount tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void viewDiscount() throws ClassNotFoundException, SQLException, ParseException
{
do {
System.out.println("In order to go back to Discount Menu, enter 0");
System.out.println("Please enter a Discount ID to view a Discount");
System.out.println();
System.out.println("Discount ID: ");
input = scan.nextInt();
scan.nextLine();
if (input > 0) {
ResultSet rs = DiscountSQL.viewDiscount(input);
printDiscount(rs);
rs.close();
} else if (input < 0) {
System.out.println("Invalid input");
} else if (input == 0) {
System.out.println("Going back to Discount Menu");
return;
}
} while (input != 0);
}
/**
* Displays menu and calls SQL for editing a Discount tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void edit() throws ClassNotFoundException, SQLException, ParseException
{
input = 0;
do {
System.out.println("Enter a Discount ID to edit a Discount Member or enter 0 to return to Discount Menu");
System.out.println();
System.out.println("Discount ID: ");
input = scan.nextInt();
scan.nextLine();
System.out.println();
if (input > 0) {
rs = DiscountSQL.viewDiscount(input);
if (!rs.next()) {
System.out.println("Discount ID does not exist.");
} else {
addOrEdit("edit");
}
} else if (input == 0) {
System.out.println("Going back to Discount Menu");
return;
} else {
System.out.println("Invalid Discount ID");
}
} while (input != 0);
}
/**
* Displays menu and calls SQL for deleting a Discount tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void delete() throws ClassNotFoundException, SQLException, ParseException
{
do{
System.out.println("Delete Discount Menu:");
System.out.println("Enter Discount ID to delete a Discount or enter 0 to return to Discount Menu");
System.out.println();
System.out.println("Discount ID: ");
input = scan.nextInt();
scan.nextLine();
System.out.println();
if (input > 0) {
try {
DiscountSQL.deleteDiscount(input);
} catch (SQLException e) {
e.printStackTrace();
}
} else if (input < 0) {
System.out.println("Invalid input");
} else if (input == 0) {
System.out.println("Returning to Discount Menu");
return;
}
} while (input != 0);
}
/**
* Prints to stdout all tuples held in the static ResultSet
* @param rs
* @throws SQLException
*/
public static void printDiscount(ResultSet rs) throws SQLException
{
if (!rs.next()) {
System.out.println("There is no Discount with this Discount ID.");
} else {
do {
discountID = rs.getInt("discountID");
productID = rs.getInt("productID");
priceReduction = rs.getDouble("priceReduction");
startDate = rs.getDate("startDate");
endDate = rs.getDate("endDate");
System.out.println("Discount ID: " + discountID + ", Product ID: " + productID + ", Price Reduction: " + priceReduction + ", Start Data: " + startDate + ", End Date: " + endDate);
} 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 = -1;
if (mode.equals("edit")) {
discountID = rs.getInt("discountID");
productID = rs.getInt("productID");
priceReduction = rs.getDouble("priceReduction");
startDate = rs.getDate("startDate");
endDate = rs.getDate("endDate");
}
do {
System.out.println("Enter a number to " + mode + " discount attributes or enter 0 to return to Discount Menu");
System.out.println("0. Return to Discount Menu");
System.out.println("1. Discount ID: " + discountID);
System.out.println("2. Product ID: " + productID);
System.out.println("3. Price Reduction: " + priceReduction);
System.out.println("4. Start Date: " + startDate);
System.out.println("5. End Date: " + endDate);
System.out.println("6. After all attributes have been entered, select 6 to " + mode + " Discount");
input = scan.nextInt();
scan.nextLine();
System.out.println();
switch(input) {
case 0:
System.out.println("Going back to Discount Menu");
System.out.println();
resetAttributes();
return;
case 1:
System.out.println("Enter Discount ID: ");
discountID = scan.nextInt();
break;
case 2:
System.out.println("Enter Product ID: ");
productID = scan.nextInt();
break;
case 3:
System.out.println("Enter Price Reduction: ");
priceReduction = scan.nextDouble();
break;
case 4:
System.out.println("Enter Start Date in format YYYY-MM-DD: ");
try{
startDate = Date.valueOf(scan.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("Date format incorrect, please try again.");
}
break;
case 5:
System.out.println("Enter End Date in format YYYY-MM-DD: ");
try{
endDate = Date.valueOf(scan.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("Date format incorrect, please try again.");
}
break;
case 6:
try{
if (mode.equals("add")) {
DiscountSQL.addDiscount(discountID, productID, priceReduction, startDate, endDate);
} else if (mode.equals("edit")) {
DiscountSQL.editDiscount(discountID, productID, priceReduction, startDate, endDate);
}
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
break;
}
rs = DiscountSQL.viewDiscount(discountID);
printDiscount(rs);
resetAttributes();
input = -1; // We're done, back to Discount Menu
return;
default:
System.out.println("Invalid input");
break;
}
} while(input != 0);
}
/**
* Resets Discount attributes back to default.
*/
public static void resetAttributes()
{
discountID = -1;
productID = -1;
priceReduction = -1.00;
startDate = Date.valueOf("0001-01-01");
endDate = Date.valueOf("0001-01-01");
}
}