-
Notifications
You must be signed in to change notification settings - Fork 1
/
Store.java
287 lines (265 loc) · 8.21 KB
/
Store.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
import java.sql.*;
import java.util.*;
import java.sql.SQLException;
import java.text.ParseException;
public class Store {
/** 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;
/** Store table schema */
static int storeID = -1;
static int managerID = -1;
static String address = "";
static String phone = "";
/**
* Displays main menu for Store table and delegates user input to Store action methods.
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void storeMenu() throws ClassNotFoundException, SQLException, ParseException
{
do {
System.out.println("Store Menu");
System.out.println();
System.out.println("0. Return to Information Processing Menu");
System.out.println("1. View All Stores");
System.out.println("2. View Store by ID");
System.out.println("3. Add Store");
System.out.println("4. Edit Store");
System.out.println("5. Delete Store");
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:
viewAllStores();
break;
case 2:
viewStore();
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 Store tuples
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void viewAllStores() throws ClassNotFoundException, SQLException, ParseException
{
try {
ResultSet rs = StoreSQL.viewAllStores();
printStore(rs);
} catch(SQLException e) {
System.out.println("SQL Exception: " + e.getMessage());
}
}
/**
* Displays menu and calls SQL for viewing a single Store tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void viewStore() throws ClassNotFoundException, SQLException, ParseException {
do {
System.out.println("In order to go back to Store Menu, enter 0");
System.out.println("Please enter a Store ID to view a store");
System.out.println();
System.out.println("Store ID: ");
input = scan.nextInt();
scan.nextLine();
if (input > 0) {
ResultSet rs = StoreSQL.viewStore(input);
printStore(rs);
rs.close();
} else if (input < 0) {
System.out.println("Invalid input");
} else if (input == 0) {
System.out.println("Going back to Store Menu");
return;
}
} while (input != 0);
}
/**
* Displays menu and calls SQL for editing a Store tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void edit() throws ClassNotFoundException, SQLException, ParseException
{
input = 0;
do {
System.out.println("Enter a Store ID to edit a Store or enter 0 to return to Store Menu");
System.out.println();
System.out.println("Store ID: ");
input = scan.nextInt();
scan.nextLine();
System.out.println();
if (input > 0) {
rs = StoreSQL.viewStore(input);
if (!rs.next()) {
System.out.println("Store ID does not exist.");
} else {
addOrEdit("edit");
}
} else if (input == 0) {
System.out.println("Going back to Store Menu");
return;
} else {
System.out.println("Invalid Store ID");
}
} while (input != 0);
}
/**
* Displays menu and calls SQL for deleting a Store tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void delete() throws ClassNotFoundException, SQLException, ParseException
{
do{
System.out.println("Delete Store Menu:");
System.out.println("Enter Store ID to delete Store or enter 0 to return to Store Menu");
System.out.println();
System.out.println("Store ID: ");
input = scan.nextInt();
scan.nextLine();
System.out.println();
if (input > 0) {
try {
StoreSQL.deleteStore(input);
} catch (SQLException e) {
e.printStackTrace();
}
} else if (input < 0) {
System.out.println("Invalid input");
} else if (input == 0) {
System.out.println("Going back to Store Menu");
return;
}
} while (input != 0);
}
/**
* Prints to stdout all tuples held in the static ResultSet
* @param rs ResultSet containing the Store tuples to print to stdout
* @throws SQLException
*/
public static void printStore(ResultSet rs) throws SQLException
{
if (!rs.next()) {
System.out.println("There is no Store with this storeID.");
} else {
do {
storeID = rs.getInt("storeID");
managerID = rs.getInt("managerID");
address = rs.getString("address");
phone = rs.getString("phone");
System.out.println("Store ID: " + storeID + ", Manager ID: " + managerID + ", Address: " + address + ", Phone: " + phone);
} while (rs.next());
}
}
/**
* Parses user input to allow user to set desired attribute 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")) {
storeID = rs.getInt("storeID");
managerID = rs.getInt("managerID");
address = rs.getString("address");
phone = rs.getString("phone");
}
do {
System.out.println("Enter a number to " + mode + " store attributes or enter 0 to return to Store Menu");
System.out.println("0. Return to Store Menu");
System.out.println("1. Store ID: " + storeID);
System.out.println("2. Manager ID: " + managerID);
System.out.println("3. Address: " + address);
System.out.println("4. Phone: " + phone);
System.out.println("5. After all attributes have been entered, select 5 to " + mode + " Store");
input = scan.nextInt();
scan.nextLine();
System.out.println();
switch(input) {
case 0:
System.out.println("Going back to Store Menu");
System.out.println();
resetAttributes();
return;
case 1:
System.out.println("Enter Store ID: ");
storeID = scan.nextInt();
break;
case 2:
System.out.println("Enter Manager ID: ");
managerID = scan.nextInt();
break;
case 3:
System.out.println("Enter Address: ");
address = scan.nextLine();
break;
case 4:
System.out.println("Enter Phone Number: ");
phone = scan.nextLine();
break;
case 5:
try{
if(mode.equals("add")) {
StoreSQL.addStore(storeID, managerID, address, phone);
} else if (mode.equals("edit")) {
StoreSQL.editStore(storeID, managerID, address, phone);
}
} catch(SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
break;
}
ResultSet rs = StoreSQL.viewStore(storeID);
printStore(rs);
resetAttributes();
input = -1; // We're done, back to Store Menu
return;
default:
System.out.println("Invalid input");
break;
}
} while(input != 0);
}
/**
* Resets store attributes back to default.
*/
public static void resetAttributes()
{
storeID = -1;
managerID = -1;
address = "";
phone = "";
}
}