-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phase3.java
308 lines (303 loc) · 10.5 KB
/
Phase3.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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Phase3 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Map<Integer, Employee> tempEmployee = new HashMap<>();
String eid = "";
System.out.println("Welcome to Employee Management System (EMS)");
tempEmployee = load(args[0]);
if(tempEmployee.isEmpty())
{
System.out.println("Database input file "+args[0]+" not found.");
System.out.println("Creating an empty database.");
}
int selection;
do {
menu();
System.out.println("Enter you selection (1..7):");
selection = input.nextInt();
while(selection <1 || selection > 7)
{
System.out.println("Invalid selection.");
System.out.println("Enter you selection (1..7):");
selection = input.nextInt();
}
if(selection == 1)
{
System.out.println("Enter an employee ID or QUIT to stop:");
eid = input.next();
input.nextLine();
while(!"QUIT".equalsIgnoreCase(eid) && Integer.parseInt(eid) <= 0)
{
System.out.println("Invalid ID. Employee ID should be a positive integer number.");
System.out.println("Enter an Employee ID or QUIT to stop:");
eid = input.next();
}
while(!"QUIT".equalsIgnoreCase(eid) && tempEmployee.containsKey(Integer.parseInt(eid)))
{
System.out.println("Employee ID: "+eid+" already exists in the database.");
System.out.println("Enter an Employee ID or QUIT to stop:");
eid = input.next();
while(!"QUIT".equalsIgnoreCase(eid) && Integer.parseInt(eid) <= 0)
{
System.out.println("Invalid ID. Employee ID should be a positive integer number.");
System.out.println("Enter an Employee ID or QUIT to stop:");
eid = input.next();
}
}
if(!"QUIT".equalsIgnoreCase(eid))
{
System.out.println("Enter employee name:");
String name = input.nextLine();
System.out.println("Enter employee department:");
String department = input.nextLine();
System.out.println("Enter employee title:");
String title = input.nextLine();
System.out.println("Enter employee salary:");
Double salary = input.nextDouble();
while(salary<=0)
{
System.out.println("Invalid salary. Salary should be a positive number.");
System.out.println("Enter employee salary:");
salary = input.nextDouble();
}
tempEmployee.put(Integer.parseInt(eid),new Employee(Integer.parseInt(eid),name,department,title, salary));
}
}
else if(selection == 2)
{
System.out.println("Enter an Employee ID or QUIT to stop:");
eid = input.next();
if("QUIT".equalsIgnoreCase(eid))
selection = 7;
else
findEmployeeByID(tempEmployee, Integer.parseInt(eid));
}
else if(selection == 3)
{
System.out.println("Enter an Employee name or QUIT to stop:");
String name = input.next();
if("QUIT".equalsIgnoreCase(name))
selection = 7;
else
findEmployeeByName(tempEmployee, name);
}
else if(selection == 4)
{
System.out.println("Enter an Employee ID to delete or QUIT to stop:");
eid = input.next();
if(!"QUIT".equalsIgnoreCase(eid))
{
while(Integer.parseInt(eid) <= 0)
{
System.out.println("Invalid ID. Employee ID should be a positive integer number.");
System.out.println("Enter an Employee ID to delete or QUIT to stop:");
eid = input.next();
}
if(tempEmployee.containsKey(eid))
{
tempEmployee.remove(Integer.parseInt(eid));
}
else
System.out.println("There is no employee with that ID number");
}
}
else if(selection == 5)
{
displayStatistics(tempEmployee);
}
else if(selection == 6)
{
displayAll(tempEmployee);
}
else if(selection == 7)
{
if(!tempEmployee.isEmpty())
{
File file = new File(args[0]);
save(tempEmployee, file);
}
}
}while(selection != 7);
System.out.println("Thank you for using Employee Management System (EMS)");
}
private static void displayStatistics(Map<Integer, Employee> tempEmployee) {
// TODO Auto-generated method stub
Map<String, Integer> departments = new HashMap<>();
if(!tempEmployee.isEmpty())
{
System.out.println("Department Statistics:");
for(int i : tempEmployee.keySet())
{
String department = tempEmployee.get(i).getDepartment();
if(!departments.containsKey(tempEmployee.get(i).getDepartment()))
{
departments.put(tempEmployee.get(i).getDepartment(),countDepartment(tempEmployee,tempEmployee.get(i).getDepartment()));
if(departments.get(department) == 1)
System.out.println("\tDepartment: "+department+" - "+departments.get(department) + " employee");
else
System.out.println("\tDepartment: "+department+" - "+departments.get(department) + " employees");
System.out.printf("\t\tMaximum Salary: $%10.2f\n",maxSal(tempEmployee, department));
System.out.printf("\t\tMinimum Salary: $%10.2f\n",minSal(tempEmployee, department));
System.out.printf("\t\tAverage Salary: $%10.2f\n",averageSal(tempEmployee, department));
}
}
if(departments.size() == 1)
System.out.println("There is "+departments.size()+" department in the database.");
else
System.out.println("There are "+departments.size()+" departments in the database.");
if(tempEmployee.size()==1)
System.out.println("There is "+tempEmployee.size()+" employee in the database.");
else
System.out.println("There are "+tempEmployee.size()+" employees in the database.");
}
else
{
System.out.println("There are no departments in the database.");
System.out.println("Employee database is empty.");
}
}
private static int countDepartment(Map<Integer, Employee> tempEmployee, String department)
{
int count = 0;
for(int i : tempEmployee.keySet())
{
if(tempEmployee.get(i).getDepartment().equalsIgnoreCase(department))
count ++;
}
return count;
}
private static double averageSal(Map<Integer, Employee> tempEmployee, String department)
{
double total = 0;
for(int i : tempEmployee.keySet())
{
if(tempEmployee.get(i).getDepartment().equalsIgnoreCase(department))
total += tempEmployee.get(i).getSalary();
}
return total/countDepartment(tempEmployee, department);
}
private static double maxSal(Map<Integer, Employee> tempEmployee, String department)
{
double max = 0;
for(int i : tempEmployee.keySet())
{
if(tempEmployee.get(i).getDepartment().equalsIgnoreCase(department))
if(tempEmployee.get(i).getSalary() > max)
max = tempEmployee.get(i).getSalary();
}
return max;
}
private static double minSal(Map<Integer, Employee> tempEmployee, String department)
{
double min = 0;
for(int i : tempEmployee.keySet())
{
if(tempEmployee.get(i).getDepartment().equalsIgnoreCase(department))
{
if(min == 0 || tempEmployee.get(i).getSalary() < min)
min = tempEmployee.get(i).getSalary();
}
}
return min;
}
private static void displayAll(Map<Integer, Employee> tempEmployee) {
// TODO Auto-generated method stub
if(tempEmployee.isEmpty())
System.out.println("Employee database is empty.");
else
{
for(int i : tempEmployee.keySet())
{
System.out.println("Employee ID: "+i);
System.out.println("\tName: "+ tempEmployee.get(i).getName());
System.out.println("\tDepartment: "+ tempEmployee.get(i).getDepartment());
System.out.println("\tTitle: "+tempEmployee.get(i).getTitle());
System.out.printf("\tSalary: %,.2f\n", tempEmployee.get(i).getSalary());
}
if(tempEmployee.size() == 1)
System.out.println("There is "+tempEmployee.size()+" employee in the database.");
else
System.out.println("There are "+tempEmployee.size()+" employees in the database.");
}
}
private static void findEmployeeByName(Map<Integer, Employee> tempEmployee, String name ) {
// TODO Auto-generated method stub
boolean found = false;
for(int keys : tempEmployee.keySet())
{
if(tempEmployee.get(keys).getName().equalsIgnoreCase(name))
{
found = true;
System.out.println("Employee ID: "+keys);
System.out.println("\tName: "+ tempEmployee.get(keys).getName());
System.out.println("\tDepartment: "+ tempEmployee.get(keys).getDepartment());
System.out.println("\tTitle: "+tempEmployee.get(keys).getTitle());
System.out.printf("\tSalary: %,.2f\n", tempEmployee.get(keys).getSalary());
}
}
if(!found)
System.out.println("No employee with "+name+" works here");
}
private static void findEmployeeByID(Map<Integer, Employee> tempEmployee, int idNum) {
// TODO Auto-generated method stub
boolean found = false;
for(int keys : tempEmployee.keySet())
{
if(keys == idNum)
{
found = true;
System.out.println("Employee ID: "+keys);
System.out.println("\tName: "+ tempEmployee.get(keys).getName());
System.out.println("\tDepartment: "+ tempEmployee.get(keys).getDepartment());
System.out.println("\tTitle: "+tempEmployee.get(keys).getTitle());
System.out.printf("\tSalary: %,.2f\n", tempEmployee.get(keys).getSalary());
}
}
if(!found)
System.out.println("No employee with "+idNum+" works here");
}
private static void menu()
{
System.out.println("Main Menu:");
System.out.println("1.\tAdd an Employee");
System.out.println("2.\tFind an Employee (By Employee ID)");
System.out.println("3.\tFind an Employee (By Name)");
System.out.println("4.\tDelete an Employee");
System.out.println("5.\tDisplay Statistics");
System.out.println("6.\tDisplay All Employees");
System.out.println("7.\tExit");
}
private static Map<Integer, Employee> load(String fileName) throws FileNotFoundException
{
Map<Integer, Employee> tempEmployee = new HashMap<>();
File file = new File(fileName);
if(file.exists())
{
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext()) {
String[] data = inputFile.nextLine().split(",");
tempEmployee.put(Integer.parseInt(data[0]),new Employee(Integer.parseInt(data[0]),data[1],data[2],data[3], Double.parseDouble(data[4])));
}
inputFile.close();
}
return tempEmployee;
}
private static void save(Map<Integer,Employee> tempEmployee, File file) throws IOException
{
PrintWriter outputFile = new PrintWriter(file);
for(int keys : tempEmployee.keySet())
{
outputFile.append(Integer.toString(keys)+","+tempEmployee.get(keys).getName()+","+tempEmployee.get(keys).getDepartment()+","+tempEmployee.get(keys).getTitle()+","+Double.toString(tempEmployee.get(keys).getSalary()));
outputFile.append('\n');
}
outputFile.close();
}
}