-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditFinaliseMenu.java
More file actions
304 lines (274 loc) · 10.1 KB
/
EditFinaliseMenu.java
File metadata and controls
304 lines (274 loc) · 10.1 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
package poised;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.Scanner;
/**
* Abstract class that prints the edit and finalisation sub menu options and
* uses a <code>switch</code> block to perform the user specified action(s).
* Calls functions that use getters and setters as defined in the
* <code>Project</code> and
* <code>Person</code> classes, to edit the attributes.
* Calls methods from the <code>FinaliseProject</code> class that mark projects
* as complete, set date of completion, generate an invoice if there is an
* outstanding amount, and removes the
* project from the current objects List.
*
* @author Nadia
* @version 2.0
* @since 1 June 2022
* @see Project.addPaidAmount
* @see Project.addProjCost
* @see Project.addDeadline
* @see Person.editPersonPhone
* @see Person.editPersonEmail
* @see Project.getCustomer
* @see Project.getContractor
* @see FinaliseProject.finaliseDate
* @see FinaliseProject.generateInvoice
* @see FinaliseProject.writeCompletedProjToFile
*
*/
public abstract class EditFinaliseMenu {
/**
* Constructor made explicit and private so the class can't be instantiated
* by the implicit public one.
*/
private EditFinaliseMenu() {
}
// new scanner object to read user input
static Scanner userEntry = new Scanner(System.in);
/**
* A string of sub menu options used exclusively
* for passing into the <code>edits</code> method of the same class.
* @value #EDITING_MENU}
*/
private static final String EDITING_MENU = """
=== Editing Options ===
(1) - Edit project cost
(2) - Edit project fee paid
(3) - Edit project deadline
(4) - Edit customer phone number
(5) - Edit customer email address
(6) - Edit contractor phone number
(7) - Edit contractor email address
(8) - Edit architect phone number
(9) - Edit architect email address
(10) - Edit engineer phone number
(11) - Edit engineer email address
(12) - Edit manager phone number
(13) - Edit manager email address
(f) - Finalise Project
(mm) - Return to the main menu
Please enter your selection: """;
/**
* Displays a sub menu with options to alter selected details of a Project
* or to mark it as complete. Updates both Project and Person objects along
* with relevant database value.
* <p>
* Call methods to valid user's input where necessary, set that
* input to the relevant object attribute, and change that
* value in the database.
* <p>
* Calls methods from the <code>FinaliseProject</code> class. That is,
* the <code>finaliseDate</code> method to
* record the Project's date of completion, and the <code>generateInvoice</code>
* method to display an invoice to the user if there is still an amount
* owing.
*
* @param project returned object from the
* <code>searchProjects</code> method
* @throws SQLException JDBC API error
*/
public static void edits(Project project) throws SQLException {
String menuChoice;
do {
// print out menu and user's input
System.out.println(EDITING_MENU);
menuChoice = userEntry.nextLine().toLowerCase();
System.out.printf("Menu selection: \'%s\'%n", menuChoice);
// connect to DB
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/PoisePMS?useSSL=false",
"otheruser",
"swordfish");
Statement statement = connection.createStatement();
switch (menuChoice) {
/**
* Call methods to valid user's input where necessary, set that
* input to the relevant object attribute, and change that
* value in the database.
*/
// change project deadline (project table in DB)
case "1" -> {
// change object's attribute
project.addProjCost(project);
// get changed value
double p_cost = project.getFeeTotal();
// update DB with the changed value
statement.executeUpdate(
"UPDATE project SET proj_cost=" + p_cost + " WHERE proj_num="
+ project.getProjNumber());
}
// change project deadline (project table in DB)
case "2" -> {
// change object's attribute
project.addPaidAmount(project);
// get changed value
double p_paid = project.getFeePaid();
// update DB with the changed value
statement.executeUpdate(
"UPDATE project SET proj_paid=" + p_paid + " WHERE proj_num="
+ project.getProjNumber());
}
// change project deadline (project table in DB)
case "3" -> {
// change object's attribute
project.addDeadline(project);
// get changed value
LocalDate p_deadline = project.getProjDeadline();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE project SET proj_due_on='" + p_deadline + "' WHERE proj_num="
+ project.getProjNumber());
}
// change customer phone number (customer table in DB)
case "4" -> {
// change object's attribute
project.getCustomer()
.editPersonPhone(project.getCustomer(), "Customer");
// get changed value
String cu_phone = project.getCustomer().getPhoneNumber();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE customer SET cust_phone='" + cu_phone + "' WHERE proj_num="
+ project.getProjNumber());
}
// change customer email (customer table in DB)
case "5" -> {
// change object's attribute
project.getCustomer()
.editPersonEmail(project.getCustomer(), "Customer");
// get changed value
String cu_email = project.getCustomer().getEmail();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE customer SET cust_email='" + cu_email + "' WHERE proj_num="
+ project.getProjNumber());
}
// change contractor phone number (contractor table in DB)
case "6" -> {
// change object's attribute
project.getContractor()
.editPersonPhone(project.getContractor(), "Contractor");
// get changed value
String co_phone = project.getContractor().getPhoneNumber();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE contractor SET cont_phone='" + co_phone + "' WHERE proj_num="
+ project.getProjNumber());
}
// change contractor email (contractor table in DB)
case "7" -> {
// change object's attribute
project.getContractor()
.editPersonEmail(project.getContractor(), "Contractor");
// get changed value
String co_email = project.getContractor().getEmail();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE contractor SET cont_email='" + co_email + "' WHERE proj_num="
+ project.getProjNumber());
}
// change architect phone number (architect table in DB)
case "8" -> {
// change object's attribute
project.getArchitect()
.editPersonPhone(project.getArchitect(), "Architect");
// get changed value
String ar_phone = project.getArchitect().getPhoneNumber();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE architect SET arch_phone='" + ar_phone + "' WHERE proj_num="
+ project.getProjNumber());
}
// change architect email (architect table in DB)
case "9" -> {
// change object's attribute
project.getArchitect()
.editPersonEmail(project.getArchitect(), "Architect");
// get changed value
String ar_email = project.getArchitect().getEmail();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE architect SET arch_email='" + ar_email + "' WHERE proj_num="
+ project.getProjNumber());
}
// change engineer phone number (engineer table in DB)
case "10" -> {
// change object's attribute
project.getEngineer()
.editPersonPhone(project.getEngineer(), "Engineer");
// get changed value
String en_phone = project.getEngineer().getPhoneNumber();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE engineer SET engr_phone='" + en_phone + "' WHERE proj_num="
+ project.getProjNumber());
}
// change engineer email (engineer table in DB)
case "11" -> {
// change object's attribute
project.getEngineer()
.editPersonEmail(project.getEngineer(), "Engineer");
// get changed value from object
String en_email = project.getEngineer().getEmail();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE engineer SET engr_email='" + en_email + "' WHERE proj_num="
+ project.getProjNumber());
}
// change manager phone number (manager table in DB)
case "12" -> {
// change object's attribute
project.getManager()
.editPersonPhone(project.getManager(), "Manager");
// get changed value
String ma_phone = project.getManager().getPhoneNumber();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE manager SET mang_phone='" + ma_phone + "' WHERE proj_num="
+ project.getProjNumber());
}
// change manager email (manager table in DB)
case "13" -> {
// change object's attribute
project.getManager()
.editPersonEmail(project.getManager(), "Manager");
// get changed value from object
String ma_email = project.getManager().getEmail();
// update DB cell in table with the changed value
statement.executeUpdate(
"UPDATE manager SET mang_email='" + ma_email + "' WHERE proj_num="
+ project.getProjNumber());
}
// #### Finalising project ###
case "f" -> {
/* call method to add date completed and mark project as complete
both on the project object and in the DB */
FinaliseProject.finaliseDate(project);
// check if the customer has an outstanding payment and generate invoice
FinaliseProject.generateInvoice(project);
}
case "mm" -> System.out.println();
default -> System.out.println("Invalid selection. Please try again");
}
// close connections
statement.close();
connection.close();
// exit to main menu
} while (!menuChoice.equals("mm"));
}
}