-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoisedProject.java
More file actions
235 lines (216 loc) · 10.4 KB
/
Copy pathPoisedProject.java
File metadata and controls
235 lines (216 loc) · 10.4 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
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* This java program manages construction projects by storing pending and
* complete project details, as well as maintaining updates about the project.
*
* @author Cherol Phoshoko
* @version 16.0.2, 2021-09-09
*/
public class PoisedProject {
/**
* Main method of the program used to access the existing projects, add updates,
* finalize them, or even add new projects at runtime. <br>
*
* @param args The command line arguments
* @throws IOException if error occurs
*/
public static void main(String[] args) throws IOException {
// Field variables used for project and persons' details.
String name, surname;
String userType;
String telephoneNum, email, physicalAdd;
String projectName, buildingType, erfNum, deadline;
double totalFee, totalPaid;
ProjectFile projectList = new ProjectFile();
// User prompted to add new project details when program runs
// System.out.println("1- ADD NEW PROJECT 2- Access existing file:");
while (true) {
System.out.println("""
\nProgram menu:
1 - Display all projects
2 - Add new project
3 - Edit existing project
4 - Display incomplete projects
5 - Display overdue projects
0 - exit
Enter option:""");
Scanner input = new Scanner(System.in);
int option = input.nextInt();
if (option == 1) {
projectList.printAll();
} else if (option == 2) {
Project newProject;
input.nextLine();
System.out.println("\nEnter Project name:");
projectName = input.nextLine();
System.out.println("Enter building type:");
buildingType = input.nextLine();
System.out.println("Enter building address:");
physicalAdd = input.nextLine();
System.out.println("Enter building ERF number:");
erfNum = input.nextLine();
System.out.println("Enter project deadline(yyyy-MM-dd):");
deadline = input.nextLine();
while (validDateFormat(deadline) == false) {
System.out.println("\nInvalid date format. Try again.");
System.out.println("Enter project deadline(yyyy-MM-dd):");
deadline = input.nextLine();
}
while (true) {
try {
System.out.println("Overall Project Fee:");
totalFee = input.nextDouble();
System.out.println("Total paid to date:");
totalPaid = input.nextDouble();
// new project Object
newProject = new Project(projectName, buildingType, physicalAdd, erfNum, deadline, totalFee,
totalPaid);
break;
} catch (InputMismatchException e) {
System.out.println("Invalid Input!\nPlease enter valid amount.");
input.nextLine();
}
}
// created person objects, they're accessible out of the loops
Person customer = null;
Person architect = null;
Person contractor;
// loop used to add details of project customer, architect and contractor
System.out.println("\nCustomer Details".toUpperCase());
input.nextLine();
int loop = 0;
while (true) {
loop++;
System.out.println("\nEnter name:");
name = input.nextLine();
System.out.println("Enter surname:");
surname = input.nextLine();
System.out.println("Enter telephone number:");
telephoneNum = input.nextLine();
System.out.println("Enter email address:");
email = input.nextLine();
System.out.println("Enter physical address:");
physicalAdd = input.nextLine();
if (loop == 1) {
userType = "Customer";
customer = new Person(name, surname, userType, telephoneNum, email, physicalAdd);
if (projectName.isBlank()) {
// if project name not provided Building type & client surname used in place of.
newProject.projectName = newProject.buildingType + " " + surname;
}
System.out.println("\nProject Architect Details".toUpperCase());
}
if (loop == 2) {
userType = "Architect";
architect = new Person(name, surname, userType, telephoneNum, email, physicalAdd);
System.out.println("\nProject Contractor Details".toUpperCase());
}
if (loop == 3) {
userType = "Contractor";
contractor = new Person(name, surname, userType, telephoneNum, email, physicalAdd);
break;
}
}
// assert used to declare null values as false.
System.out.println("\n\nInformation Successfully Saved!\n\n");
System.out.println(newProject.projectDetails() + "\n\n");
System.out.println(customer.personDetails() + "\n\n");
System.out.println(architect.personDetails() + "\n\n");
System.out.println(contractor.personDetails());
projectList.addNewProject(newProject, customer, architect, contractor);
//calling projectList methods with Project name as input specifies project to edit
} else if (option == 3) {
input.nextLine();
String project2Edit;
System.out.println("\nEnter project name:");
project2Edit = input.nextLine();
projectList.showProject(project2Edit);
while (true) {
System.out.println("""
\n1 - Edit contractor's details
2 - Edit project deadline
3 - Edit Amount paid to date
4 - Finalize project
0 - Return to main menu
""");
int choice = input.nextInt();
if (choice == 1) {
input.nextLine();
System.out.println("\nEnter contractor's new telephone number:");
telephoneNum = input.nextLine();
System.out.println("Enter contractor's new email Address: ");
email = input.nextLine();
projectList.changeContractorDetails(project2Edit, telephoneNum, email);
} else if (choice == 2) {
input.nextLine();
System.out.println("\nEnter New deadline(yyyy-MM-dd):");
deadline = input.nextLine();
while ((validDateFormat(deadline)) == false) {
System.out.println("\nInvalid date format. Try again.");
System.out.println(validDateFormat(deadline));
}
projectList.changeDeadline(project2Edit, deadline);
} else if (choice == 3) {
while (true) {
try {
System.out.println("\nEnter amount paid:");
Double paidAmount = input.nextDouble();
projectList.changeAmountPaid(project2Edit, paidAmount);
break;
} catch (InputMismatchException e) {
System.out.println("Invalid Input!\nPlease enter valid amount.");
input.nextLine();
}
}
} else if (choice == 4) {
projectList.finalizeProject(project2Edit);
} else if (choice == 0) {
break;
} else {
System.out.println("Invalid option!");
}
}
} else if (option == 4) {
projectList.incompleteProjects();
} else if (option == 5) {
try {
projectList.overdueProjects();
} catch (ParseException e) {
System.out.println("Parse Exception occurred!");
}
} else if (option == 0) {
System.out.println("\nLogout Successful!");
break;
}
}
//writes updated project object list, to projects file.
projectList.writeToFile();
}
/**
* This method validates if user inputs the correct date format for the project
* deadlines <br>
*
* @param deadline String will be formatted to a date
* @return boolean value for valid date.
*/
public static boolean validDateFormat(String deadline) {
boolean valid;
try {
String dateFormat = "yyyy-MM-dd";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(dateFormat)
.withResolverStyle(ResolverStyle.LENIENT);
LocalDate date = LocalDate.parse(deadline, dateFormatter);
valid = true;
} catch (DateTimeParseException e) {
valid = false;
}
return valid;
}
}