-
Notifications
You must be signed in to change notification settings - Fork 1
/
Staff.java
337 lines (315 loc) · 10.1 KB
/
Staff.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
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
import java.sql.*;
import java.util.*;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
public class Staff {
/** 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;
/** Staff table schema */
static int staffID = -1;
static int storeID = -1;
static String name = "";
static int age = -1;
static String address = "";
static String title = "";
static String phone = "";
static String email = "";
static Date employmentTime = Date.valueOf("0001-01-01");
/**
* Displays main menu for Staff table and delegates user input to Staff action methods.
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void staffMenu() throws ClassNotFoundException, SQLException, ParseException
{
do {
System.out.println("Staff Menu");
System.out.println();
System.out.println("0. Return to Information Processing Menu");
System.out.println("1. View All Staff");
System.out.println("2. View Staff by ID");
System.out.println("3. Add Staff");
System.out.println("4. Edit Staff");
System.out.println("5. Delete Staff");
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:
viewAllStaff();
break;
case 2:
viewStaff();
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 Staff tuples
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void viewAllStaff() throws ClassNotFoundException, SQLException, ParseException
{
try {
ResultSet rs = StaffSQL.viewAllStaff();
printStaff(rs);
} catch(SQLException e) {
System.out.println("SQL Exception: " + e.getMessage());
}
}
/**
* Displays menu and calls SQL for viewing a single Staff tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void viewStaff() throws ClassNotFoundException, SQLException, ParseException {
do {
System.out.println("In order to go back to Staff Menu, enter 0");
System.out.println("Please enter a Staff ID to view a staff member");
System.out.println();
System.out.println("Staff ID: ");
input = scan.nextInt();
scan.nextLine();
if (input > 0) {
ResultSet rs = StaffSQL.viewStaff(input);
printStaff(rs);
rs.close();
} else if (input < 0) {
System.out.println("Invalid input");
} else if (input == 0) {
System.out.println("Going back to Staff Menu");
return;
}
} while (input != 0);
}
/**
* Displays menu and calls SQL for editing a Staff tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void edit() throws ClassNotFoundException, SQLException, ParseException
{
input = 0;
do {
System.out.println("Enter a Staff ID to edit a Staff Member or enter 0 to return to Staff Menu");
System.out.println();
System.out.println("Staff ID: ");
input = scan.nextInt();
scan.nextLine();
System.out.println();
if (input > 0) {
rs = StaffSQL.viewStaff(input);
if (!rs.next()) {
System.out.println("Staff ID does not exist.");
} else {
addOrEdit("edit");
}
} else if (input == 0) {
System.out.println("Going back to Staff Menu");
return;
} else {
System.out.println("Invalid Staff ID");
}
} while (input != 0);
}
/**
* Displays menu and calls SQL for deleting a Staff tuple
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void delete() throws ClassNotFoundException, SQLException, ParseException
{
do{
System.out.println("Delete Staff Menu:");
System.out.println("Enter Staff ID to delete Staff or enter 0 to return to Staff Menu");
System.out.println();
System.out.println("Staff ID: ");
input = scan.nextInt();
scan.nextLine();
System.out.println();
if (input > 0) {
try {
StaffSQL.deleteStaff(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 Staff Menu");
return;
}
} while (input != 0);
}
/**
* Prints to stdout all tuples held in the static ResultSet
* @param rs ResultSet containing the Staff tuples to print to stdout
* @throws SQLException
*/
public static void printStaff(ResultSet rs) throws SQLException
{
if (!rs.next()) {
System.out.println("There is no Staff with this staffID.");
} else {
do {
staffID = rs.getInt("staffID");
storeID = rs.getInt("storeID");
name = rs.getString("name");
age = rs.getInt("age");
address = rs.getString("address");
title = rs.getString("title");
phone = rs.getString("phone");
email = rs.getString("email");
employmentTime = rs.getDate("employmentTime");
System.out.println("Staff ID: " + staffID + ", Store ID: " + storeID + ", Name: " + name + ", Age: " + age + ", Address: " + address + ", Title: " + title + ", Phone: " + phone + ", Email: " + email + ", Employment Time: " + employmentTime);
} 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")) {
staffID = rs.getInt("staffID");
storeID = rs.getInt("storeID");
name = rs.getString("name");
age = rs.getInt("age");
address = rs.getString("address");
title = rs.getString("title");
phone = rs.getString("phone");
email = rs.getString("email");
employmentTime = rs.getDate("employmentTime");
}
do {
System.out.println("Enter a number to " + mode + " staff attributes or enter 0 to return to Staff Menu");
System.out.println("0. Return to Staff Menu");
System.out.println("1. Staff ID: " + staffID);
System.out.println("2. Store ID: " + storeID);
System.out.println("3. Name " + name);
System.out.println("4. Age: " + age);
System.out.println("5. Address: " + address);
System.out.println("6. Title: " + title);
System.out.println("7. Phone: " + phone);
System.out.println("8. Email: " + email);
System.out.println("9. Employment Time: " + employmentTime);
System.out.println("10. After all attributes have been entered, select 10 to " + mode + " Staff Member");
input = scan.nextInt();
scan.nextLine();
System.out.println();
switch(input) {
case 0:
System.out.println("Going back to Staff Menu");
System.out.println();
resetAttributes();
return;
case 1:
System.out.println("Enter Staff ID: ");
staffID = scan.nextInt();
break;
case 2:
System.out.println("Enter Store ID: ");
storeID = scan.nextInt();
break;
case 3:
System.out.println("Enter Name: ");
name = scan.nextLine();
break;
case 4:
System.out.println("Enter Age: ");
age = scan.nextInt();
break;
case 5:
System.out.println("Enter Address: ");
address = scan.nextLine();
break;
case 6:
System.out.println("Enter Title: ");
title = scan.nextLine();
break;
case 7:
System.out.println("Enter Phone Number: ");
phone = scan.nextLine();
break;
case 8:
System.out.println("Enter Email Address: ");
email = scan.nextLine();
break;
case 9:
System.out.println("Enter Employment Time in format YYYY-MM-DD: ");
try {
employmentTime = Date.valueOf(scan.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("Date format incorrect, please try again.");
}
break;
case 10:
try{
if(mode.equals("add")) {
StaffSQL.addStaff(staffID, storeID, name, age, address, title, phone, email, employmentTime);
} else if (mode.equals("edit")) {
StaffSQL.editStaff(staffID, storeID, name, age, address, title, phone, email, employmentTime);
}
} catch(SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
break;
}
ResultSet rs = StaffSQL.viewStaff(staffID);
printStaff(rs);
resetAttributes();
input = -1; // We're done, back to Staff Menu
return;
default:
System.out.println("Invalid input");
break;
}
} while(input != 0);
}
/**
* Resets staff attributes back to default.
*/
public static void resetAttributes()
{
staffID = -1;
storeID = -1;
name = "";
age = -1;
address = "";
title = "";
phone = "";
email = "";
employmentTime = Date.valueOf("0001-01-01");
}
}