-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExistingProjects.java
More file actions
445 lines (355 loc) · 19.1 KB
/
ExistingProjects.java
File metadata and controls
445 lines (355 loc) · 19.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//Software Engineering Task 24 - Capstone Project III.
// Java classes imported for use in the program.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Formatter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
// ExistingProjects is a subclass that inherits methods from the InputChecks superclass.
// This class contains methods to find, view, update, finalise and do date checks on projects in the Poised
// Management System.
public class ExistingProjects extends InputChecks {
// The find project method runs through project info in the CurrentProjects.txt file to locate a particular
// project object needed.
// @param projectInfo either a project number or project name to identify the project object
// @return returns an integer lineCount to indicate the line number of the project in the text file
public int findProject(String projectInfo) {
// String array declared to stored project info from each line in the external text file.
// A line count set to run through the text file to find the project.
String[] info = new String[10];
int lineCount = 1;
// A new file object is created and a while loop used to run through lines in the test file.
// Line info is then split and stored in the array.
// While loop is exited if the first or second index in the array is equal to the projectInfo parameter,
// i.e. the project number or project name must match that of the text file line.
try {
File projects = new File("../CurrentProjects.txt");
Scanner projectFile = new Scanner(projects);
while (projectFile.hasNextLine()) {
info = projectFile.nextLine().split(", ");
if ((info[0].equalsIgnoreCase(projectInfo)) || (info[1].equalsIgnoreCase(projectInfo))) {
break;
} else {
lineCount++; // Line count incremented whilst match not made.
}
}
} catch (FileNotFoundException e) { // Try-catch block used to handle errors.
System.out.println("Error.");
}
return lineCount; // Integer line count returned.
}
// The viewProjects method runs through the CurrentProjects.txt file to display all project objects listed.
// The projects are each displayed in an easy-to-read format.
public void viewProjects() {
String[] info = new String[10]; // String array set to store project info from lines in text file.
// A file object is created and a while loop used to run through each line in the CurrentProjects.txt file.
// Each line in the file is split, with the info stored in the set string array.
// Each index in the array is assigned to a variable pertaining to project details, e.g. info[0] = projectNum.
// Each project is then displayed in a clear format using headings and the split project variables.
try {
File searchFile = new File("../CurrentProjects.txt");
Scanner projectFile = new Scanner(searchFile);
while (projectFile.hasNextLine()) {
info = projectFile.nextLine().split(", ");
String projectNum = info[0];
String projectName = info[1];
String buildingType = info[2];
String address = info[3];
String erfNum = info[4];
String totalFee = info[5];
String amountPaid = info[6];
String deadline = info[7];
String Completion = info[8];
String Status = info[9];
// Project info displayed for each line in the file.
System.out.println("\nProject Number: " + projectNum + "\nProjectName: " + projectName
+ "\nBuilding Type: " + buildingType + "\nPhysical Address: " + address + "\nERF Number: " + erfNum + "\nTotal Fee: R"
+ totalFee + "\nAmount Paid: R" + amountPaid + "\nDeadline: " + deadline + "\nCompletion Date: " + Completion + "\nProject Status: " + Status);
}
} catch (FileNotFoundException e) { // Try-catch block used to handle errors.
System.out.println("Error.");
}
}
// The updateProject method is used to edit project information on a particular project object located in the
// CurrentProjects.txt file.
// @param editChoice integer choice made by the user in the main menu (either 1 or 2)
// @param lineCount integer lineCount returned by the findProject function to locate the project
public void updateProject(int editChoice, int lineCount) {
// An ArrayList is set to store each line (i.e. each project) from the text file with current projects.
// A string array is used to store elements from a split line in the array.
// A findLine integer variable is set to match the lineCount for the specified file.
ArrayList<String> infoArray = new ArrayList<String>();
String[] info = new String[10];
int findLine = 1;
// A file object is created and a while loop used to run through each line of the text file.
// Each line of project information is added to the ArrayList.
// This is useful because when one project is updated, that line of info can be replaced in the array.
// The updated array can then be re-written to the CurrentProjects.txt file.
try {
File projects = new File("../CurrentProjects.txt");
Scanner projectFile = new Scanner(projects);
while (projectFile.hasNextLine()) {
infoArray.add(projectFile.nextLine()); // Each line added to the infoArray.
}
} catch (FileNotFoundException e) { // try-catch block used to handle errors.
System.out.println("Error.");
}
// Another try-catch block is now used to create another file object.
// Each line of the text file is run through and when the indicated file is found,
// i.e. the lineCount matches the findLine variable, then the line info is split.
// That specific split line info is then stored in the info string array.
try {
File projects = new File("../CurrentProjects.txt");
Scanner projectFile = new Scanner(projects);
while (projectFile.hasNextLine()) {
if (findLine == lineCount) {
info = projectFile.nextLine().split(", ");
} else if (findLine != lineCount) { // Line count incremented if match is not made.
findLine++;
}
}
} catch (FileNotFoundException e) {
System.out.println("Error.");
}
// If the user chose edit choice 1, they are prompted for a new due date.
// The new due date info is then inserted into index number 7 in the info array,
// to update the old information.
// The info array is then converted into a string, with extra characters removed (i.e. '[' trailing characters).
// The finalised string 'newLine' is then inserted into the correct index of the infoArray ArrayList (i.e. lineCount -1).
// The same process is followed for edit choice '2', except they are prompted for a new total amount, which is replaced at index 6 in the info array.
if (editChoice == 1) {
System.out.println("Please enter a new project due date: ");
String newDeadline = stringCheck("due date");
info[7] = newDeadline;
String newInfo = Arrays.toString(info);
String changeLine = newInfo.replace("[", "");
String newLine = changeLine.replace("]", "");
infoArray.set(lineCount-1, newLine);
} else if (editChoice == 2) {
System.out.println("Please enter a new total amount of the fee paid to date:");
double newAmount = doubleCheck("new total amount");
info[6] = Double.toString(newAmount);
String newInfo = Arrays.toString(info);
String changeLine = newInfo.replace("[", "");
String newLine = changeLine.replace("]", "");
infoArray.set(lineCount-1, newLine);
}
// Each element, or line of project info, of the updated array
// is then re-written to the CurrentProjects.txt file.
try {
Formatter F = new Formatter("../CurrentProjects.txt");
for (String element : infoArray) {
F.format("%s", element + "\r\n");
}
System.out.println("Project successfully updated."); // Successful message displayed.
F.close();
} catch (Exception e) {
System.out.println("Error."); // try-catch block used to handle errors.
}
}
// The finaliseProject method finalises a project object by generating an invoice, marking the project as finalised
// and adding a completion date to the project info.
// It selects a project object from the CurrentProjects.txt file, determines whether an invoice must be generated
// and then marks the project as finalised, and adds a completion date. The finalised project is then stored in a
// new text file called CompletedProjects.txt.
// @param lineCount integer lineCount determined by the findProject function
public void finaliseProject(int lineCount) {
// An ArrayList is set to store each line (i.e. each project) from the text file with current projects.
// A string array is used to store elements from a split line in the array.
// A findLine integer variable is set to match the lineCount for the specified file.
ArrayList<String> infoArray = new ArrayList<String>();
String[] info = new String[10];
int findLine = 1;
// A file object is created and a while loop used to run through each line of the text file.
// Each line of project information is added to the ArrayList.
// This is useful because when one project is updated, that line of info can be replaced in the array.
// The updated array can then be re-written to the CurrentProjects.txt file.
try {
File projects = new File("../CurrentProjects.txt");
Scanner projectFile = new Scanner(projects);
while (projectFile.hasNextLine()) {
infoArray.add(projectFile.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("Error.");
}
// Another try-catch block is now used to create another file object.
// Each line of the text file is run through and when the indicated file is found,
// i.e. the lineCount matches the findLine variable, then the line info is split.
// That specific split line info is then stored in the info string array.
try {
File projects = new File("../CurrentProjects.txt");
Scanner projectFile = new Scanner(projects);
while (projectFile.hasNextLine()) {
if (findLine == lineCount) {
info = projectFile.nextLine().split(", ");
} else {
findLine++; // Line count incremented if match is not made.
}
}
} catch (FileNotFoundException e) {
System.out.println("Error.");
}
// Each index of the split line is then assigned to a new variable.
// These variables describe project info.
// The totalFee and amountPaid indexed variables are parsed for comparison.
String projectNum = info[0];
String projectName = info[1];
String buildingType = info[2];
String address = info[3];
String erfNum = info[4];
String deadline = info[7];
String Completion;
String Status = "Finalised"; // Status of project set to 'finalised'.
double totalFee = Double.parseDouble(info[5]);
double amountPaid = Double.parseDouble(info[6]);
// If the project has been paid in full, no invoice is generated (i.e. totalFee = amountPaid).
// The user is prompted for a completion date for the project.
if (totalFee == amountPaid ) {
System.out.println("The project has been paid in full. No invoice generated.");
System.out.println("Please add a completion date for this project (e.g. day, month, year, 3 December 2021): ");
Completion = stringCheck("completion date");
// If there is still an amount outstanding, the user is prompted for customer details to generate an invoice.
// They are also prompted for a completion date.
} else {
System.out.println("Your invoice will be generated with the following details: ");
System.out.println("\nPlease enter the customer's full name: ");
String customer = stringCheck("customer's full name");
String personType2 = "Customer";
System.out.println("\nPlease enter the customer's contact number: ");
int number2 = intCheck("customer's contact number");
System.out.println("\nPlease enter the customer's email address: ");
String email2 = stringCheck("customer's email address");
System.out.println("\nPlease enter the customer's physical address: ");
String address2 = stringCheck("customer's physical address");
System.out.println("Please add a completion date for this project (e.g. day, month, year, 3 December 2021): ");
Completion = stringCheck("completion date");
// A new person object is created with the gathered info.
// This object is then displayed to the user with the DisplayPerson() method.
Person customer1 = new Person(personType2, customer, number2, email2, address2);
System.out.println("\nPlease view your invoice details below: ");
System.out.println(customer1.DisplayPerson());
System.out.println("\nAmount still owed: R" + (totalFee - amountPaid) + "\n");
}
// Finalised project details also displayed to the user in a clear format.
System.out.println("Finalised Project Details: \n" + "Project Number: " + projectNum + "\nProjectName: " + projectName
+ "\nBuilding Type: " + buildingType + "\nPhysical Address: " + address + "\nERF Number: " + erfNum + "\nTotal Fee: R"
+ totalFee + "\nAmount Paid: R" + amountPaid + "\nDeadline: " + deadline + "\nCompletion Date: " + Completion + "\nProject Status: " + Status);
infoArray.remove(lineCount-1); // infoArray updated by removing the project from the current projects list.
// The updated infoArray is then written to the CurrentProjects.txt file.
try {
Formatter F = new Formatter("../CurrentProjects.txt");
for (String element : infoArray) {
F.format("%s", element + "\r\n");
}
System.out.println("Project successfully finalised."); // Successful message displayed.
F.close();
} catch (Exception e) {
System.out.println("Error."); // try-catch block used to handle errors.
}
String projectComplete = projectNum + ", " + projectName + ", " + buildingType + ", " + address + ", "
+ erfNum + ", " + totalFee + ", " + amountPaid + ", " + deadline + ", " + Completion + ", " + Status;
// A file is then created to store the completed/ finalised projects, if it does not already exist.
try {
File newFile = new File("../CompletedProjects.txt");
if (newFile.createNewFile()) {
System.out.println("File created: " + newFile.getName());
}
else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
// Thereafter, the string of project completed info is written to the CompletedProjects.txt file in append mode.
try {
BufferedWriter out = new BufferedWriter(new FileWriter("../CompletedProjects.txt", true));
out.write(projectComplete + " \r\n");
out.close();
System.out.println("Project successfully finalised.");
} catch (IOException e) {
System.out.println("exception occurred" + e);
}
}
// The dateCheck method determines whether a project is incomplete or overdue and displays those listed projects.
// It takes in either an 'overdue' or 'incomplete' projectType string, searches through the project objects in the
// CurrentProjects.txt file, and displays a list of overdue or complete project objects.
// @param projectType string type to specify projects listed (either 'overdue' or incomplete')
// @throws ParseException occurs if a date string is in the wrong format to be parsed
public void dateCheck(String projectType) throws ParseException {
// A string array is set to store project related info from each split line in the CurrentProjects.txt file.
// An integer array is used to store numbers 1 through to 12, set to relate to the number of months in the year.
// Another string array is set to store abbreviated months of the year to get date information on a project.
// An integer for an indexed month number is also set to 0.
String[] info = new String[10];
int[] months = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
String[] monthsofYear = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int monthNum = 0;
// A try-catch block is then used to create a file object to scan through with a while loop.
// Each line of project info is split, with the info stored in the info string array.
// Index 7 of the info array ('deadline') is then split further to get specific date information (e.g '3 December 2021').
// The first index (dateInfo[0]) is stored into a 'day' integer variable.
// A string 'month' is set using the first three letters of dateInfo[1], which would be the full month name e.g. 'December'.
// The year integer variable is set from index dateInfo[2].
try {
File projects = new File("../CurrentProjects.txt");
Scanner projectFile = new Scanner(projects);
while (projectFile.hasNextLine()) {
info = projectFile.nextLine().split(", ");
String projectNum = info[0];
String projectName = info[1];
String buildingType = info[2];
String address = info[3];
String erfNum = info[4];
String totalFee = info[5];
String amountPaid = info[6];
String Completion = info[8];
String finalise = info[9];
String deadline = info[7];
String[] dateInfo = deadline.split(" ");
int day = Integer.parseInt(dateInfo[0]);
String monthInfo = dateInfo[1];
String month = (monthInfo.substring(0,2));
int year = Integer.parseInt(dateInfo[2]);
// A for loop is then used to compare the substring 'month' with the monthsofYear string array.
// Once matched with an abbreviated month of the year, the corresponding number from the integer array 'months'
// is stored in the 'monthNum' variable, for use as date info.
for (int index = 0; index < monthsofYear.length ; index++) {
if (month.equalsIgnoreCase(monthsofYear[index])) {
monthNum = months[index];
}
}
String current = "" + java.time.LocalDate.now(); // Getting the current date and storing it as a string.
// Creating a new simple date format object.
SimpleDateFormat dateObj = new SimpleDateFormat("yyyy-MM-dd");
// Dates d1 and d2 are then created by parsing string info from 'current' date
// and date info gathered from the file above.
Date d1 = dateObj.parse(current);
Date d2 = dateObj.parse(day + "-" + monthNum + "-" + year);
// Setting a string to display project details.
String projectDetails = "\nProject Number: " + projectNum + "\nProjectName: " + projectName
+ "\nBuilding Type: " + buildingType + "\nPhysical Address: " + address + "\nERF Number: " + erfNum + "\nTotal Fee: R"
+ totalFee + "\nAmount Paid: R" + amountPaid + "\nDeadline: " + deadline + "\nCompletion Date: " + Completion + "\nProject Status: " + finalise;
// If the current date is before the deadline date and the user has chosen to view 'incomplete' projects,
// Those specific projects from each line of the text file are displayed.
if ((d1.compareTo(d2) > 0) && (projectType.equalsIgnoreCase("incomplete"))) {
System.out.println(projectDetails);
// Otherwise, if the current date is past the deadline and the user has chosen to view 'overdue' projects,
// Those specific projects from each line of the text file are displayed.
} else if ((d1.compareTo(d2) < 0) && (projectType.equalsIgnoreCase("overdue"))) {
System.out.println(projectDetails);
}
}
} catch (FileNotFoundException e) { // try-catch block used to handle errors.
System.out.println("Error.");
}
}
}