-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTechnician.java
348 lines (294 loc) · 10.4 KB
/
Technician.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
338
339
340
341
342
343
344
345
346
347
348
import java.sql.*;
class Technician {
//attributes
private int technicianID;
private String firstName;
private String lastName;
private String phoneNumber;
private String emailAddress;
private int officeLocation;
private boolean currentlyHired;
private boolean isAdmin;
private String specialty;
private int experienceLevel;
private String password;
//no arg constructor
public Technician() {
this.technicianID = -1;
this.firstName = "not_loaded";
this.lastName = "not_loaded";
this.phoneNumber = "not_loaded";
this.emailAddress = "not_loaded";
this.officeLocation = -1;
this.currentlyHired = false;
this.isAdmin = false;
this.specialty = "not_loaded";
this.experienceLevel = -1;
this.password = "not_loaded";
}
//constructor overloading
//full arg constructor
public Technician(int technicianID, String firstName, String lastName, String phoneNumber,
String emailAddress, int officeLocation, boolean currentlyHired, boolean isAdmin, String specialty,
int experienceLevel, String password) {
this.technicianID = technicianID;
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.emailAddress = emailAddress;
this.officeLocation = officeLocation;
this.currentlyHired = currentlyHired;
this.isAdmin = isAdmin;
this.specialty = specialty;
this.experienceLevel = experienceLevel;
this.password = password;
}
//getters
public int getTechnicianID() {
return this.technicianID;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public String getEmailAddress() {
return this.emailAddress;
}
public int getOfficeLocation() {
return this.officeLocation;
}
public boolean getCurrentlyHired() {
return this.currentlyHired;
}
//booleans are ints in mysql
public int getCurrentlyHiredAsInt() {
if (this.currentlyHired) {
return 1;
} else {
return 0;
}
}
public boolean getIsAdmin() {
return this.isAdmin;
}
public int getIsAdminAsInt() {
if (this.isAdmin) {
return 1;
} else {
return 0;
}
}
public String getSpecialty() {
return this.specialty;
}
public int getExperienceLevel() {
return this.experienceLevel;
}
public String getPassword() {
return this.password;
}
//setters
public void setTechnicianID(int technicianID) {
this.technicianID = technicianID;
}
public void setFirstName (String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public void setOfficeLocation(int officeLocation) {
this.officeLocation = officeLocation;
}
public void setCurrentlyHired(boolean currentlyHired) {
this.currentlyHired = currentlyHired;
}
//booleans are stored as tinyint(1) in mysql
public void setCurrentlyHiredFromInt(int currentlyHired) {
if (currentlyHired == 1) {
this.currentlyHired = true;
} else {
this.currentlyHired = false;
}
}
public void setIsAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
public void setIsAdminFromInt(int isAdmin) {
if (isAdmin == 1) {
this.isAdmin = true;
} else {
this.isAdmin = false;
}
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public void setExperienceLevel(int experienceLevel) {
this.experienceLevel = experienceLevel;
}
public void setPassword(String password) {
this.password = password;
}
//update one attribute from the database to the object
//so it's synced
public void updateOne(String columnToUpdate, Statement stmt) {
//updates FROM the database TO the java client
String updateColumnQuery = "SELECT " + columnToUpdate + " FROM Technician WHERE technicianID = " + getTechnicianID();
try {
ResultSet newColumnRS = stmt.executeQuery(updateColumnQuery);
if (newColumnRS.next()) {
String newValue = newColumnRS.getString(columnToUpdate);
switch(columnToUpdate) {
case "firstName":
setFirstName(newValue);
break;
case "lastName":
setLastName(newValue);
break;
case "phoneNumber":
setPhoneNumber(newValue);
break;
case "emailAddress":
setEmailAddress(newValue);
break;
case "officeLocation":
setOfficeLocation(Integer.parseInt(newValue));
break;
case "specialty":
setSpecialty(newValue);
break;
case "experienceLevel":
setExperienceLevel(Integer.parseInt(newValue));
break;
case "password":
setPassword(newValue);
break;
default:
System.out.println("invalid arg");
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//edge-case, as booleans are ints in mysql but bools in java
//only needed for isAdmin and currentlyHired boolean attributes
public void updateOneIntToBool(String columnToUpdate, Statement stmt) {
//updates FROM the database TO the java client
String updateColumnQuery = "SELECT " + columnToUpdate + " FROM Technician WHERE technicianID = " + getTechnicianID();
try {
ResultSet newColumnRS = stmt.executeQuery(updateColumnQuery);
if (newColumnRS.next()) {
int newValue = newColumnRS.getInt(columnToUpdate);
switch(columnToUpdate) {
case "currentlyHired":
if (newValue == 1) {
setCurrentlyHired(true);
} else {
setCurrentlyHired(false);
}
break;
case "isAdmin":
if (newValue == 1) {
setIsAdmin(true);
} else {
setIsAdmin(false);
}
break;
default:
System.out.println("invalid arg");
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//returns out all attributes, useful for testing/debugging
public String toString() {
String str = "";
str += "TechID: " + getTechnicianID() + ". ";
str += "First name: " + getFirstName() + ". ";
str += "Last name: " + getLastName() + ". ";
str += "Phone #: " + getPhoneNumber() + ". ";
str += "Email: " + getEmailAddress() + ". ";
str += "Room #: " + getOfficeLocation() + ". ";
str += "Hired: " + getCurrentlyHired() + ". ";
str += "Admin: " + getIsAdmin() + ". ";
str += "Specialty: " + getSpecialty() + ". ";
str += "Experience: " + getExperienceLevel() + ". ";
str += "Password: " + getPassword() + ". ";
return str;
}
//update all object attributes from the SQL columns
//important for consistency in case the technician was changed in the database
//while the program is still running
public void updateAll(Statement stmt) {
//updates FROM the database TO the java client
try {
String checkIfMyAccountStillExists = "SELECT COUNT(technicianID) AS techExists FROM Technician WHERE technicianID = " + getTechnicianID();
ResultSet accountStillExistsRS = stmt.executeQuery(checkIfMyAccountStillExists);
if (accountStillExistsRS.next()) {
int stillExists = accountStillExistsRS.getInt("techExists");
//if an admin deletes your account while you're logged in, you get logged out
if (stillExists == 0) {
System.out.println("Your account has been deleted and now you will be logged out.");
System.exit(1);
}
} else {
//query response issue
System.out.println("checkIfMyAccountStillExists error");
System.exit(1);
}
//getting here means your account still exists
//proceeding to update the Technician object with the latest values from the database
//get most up-to-date firstName value from database and set it as this object's firstName attribute
updateOne("firstName", stmt);
//update last name from database
updateOne("lastName", stmt);
//update phone number from database
updateOne("phoneNumber", stmt);
//update email address
updateOne("emailAddress", stmt);
//update office location
updateOne("officeLocation", stmt);
//if you get changed from admin to regular technician or vice versa, you get
//logged out but can log back in, and will have a different menu
boolean oldAdminStatus = getIsAdmin();
updateOneIntToBool("isAdmin", stmt);
if (oldAdminStatus != getIsAdmin()) {
System.out.println("Your account type has changed. You will need to log back in");
System.exit(1);
}
//update currentlyHired
updateOneIntToBool("currentlyHired", stmt);
//if you get fired by an admin while you're logged in, you get logged out
//and can't log back in
if (!getCurrentlyHired()) {
System.out.println("Your technician account has been disabled.");
System.exit(1);
}
//update specialty
updateOne("specialty", stmt);
//update experience leve
updateOne("experienceLevel", stmt);
//update password
updateOne("password", stmt);
} catch (SQLException s) {
s.printStackTrace();
}
}
}