-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployee.java
418 lines (319 loc) · 15.3 KB
/
Employee.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
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
import java.util.ArrayList;
import java.util.HashMap;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.*;
import java.util.Timer;
import java.util.TimerTask;
public abstract class Employee {
private String _firstName; //"humanizing the system"
private String _lastName; //"humanizing the system"
private String _fullName;
private String _passcode;
private int _workerId; //is set automatically to uniquely identify a worker
private String _title;
private String _currentProject;
private HashMap<LocalDate, ArrayList<Tuple<LocalTime>>> _attendance;//each tuple is pair of sign-in/sign-out
private HashMap<LocalDate, ArrayList<String>> _dailyHoursAtWork;
private HashMap<LocalDate, String> _totalDailyHoursAtWork;
private int totalDaysWorked = 0;
private long totalMonthlyTime = 0;
//constructors
public Employee() {
//Empty Constructor
}
public Employee(String firstName, String lastName, String passcode) {
_firstName = firstName;
_lastName = lastName;
setFullName(firstName + " " + lastName);
_passcode = passcode;
_workerId = ManagementSystem.addEmployee();
_attendance = new HashMap<LocalDate, ArrayList<Tuple<LocalTime>>>();
_dailyHoursAtWork = new HashMap<LocalDate, ArrayList<String>>();
_totalDailyHoursAtWork = new HashMap<LocalDate, String>();
}
public Employee(String firstName, String lastName, String title, String passcode) {
_firstName = firstName;
_lastName = lastName;
setFullName(firstName + " " + lastName);
_passcode = passcode;
_workerId = ManagementSystem.addEmployee();
_title = title;
_attendance = new HashMap<LocalDate, ArrayList<Tuple<LocalTime>>>();
_dailyHoursAtWork = new HashMap<LocalDate, ArrayList<String>>();
_totalDailyHoursAtWork = new HashMap<LocalDate, String>();
}
//ToString
public String ToString() {
return "Name: " + _firstName + " " +_lastName + "\nTitle: " + _title + "\nCurrent Project: " + _currentProject + "\n";
}
//getters and setters
public String getFirstName() {
return _firstName;
}
public void setFirstName(String firstName) {
//exceptions:
//name must be more than one character long
//name can only contain Latin(English) characters
_firstName = firstName;
}
public String getLastName() {
return _lastName;
}
public void setLastName(String lastName) {
//exceptions:
//name must be more than one character long
//name can only contain Latin(English) characters
_lastName = lastName;
}
public String getFullName() {
return _fullName;
}
public void setFullName(String _fullName) {
this._fullName = _fullName;
}
public int get_workerId() {
return _workerId;
}
public void set_workerId(int workerId) {
_workerId = workerId;
}
public String getPasscode() {
return _passcode;
}
public void setPasscode(String passcode) {
//exceptions:
//can only be an existing job title
//if new title: create the title first in the list of titles
_passcode = passcode;
}
public String getTitle() {
return _title;
}
public void setTitle(String title) {
//exceptions:
//can only be an existing job title
//if new title: create the title first in the list of titles
_title = title;
}
public String getCurrentProject() {
return _currentProject;
}
public void setCurrentProject(String currentProject) {
//exceptions:
_currentProject = currentProject;
}
//methods
public void startWorkDay(boolean goToUserWindow) {
//takes current time and puts in database
LocalDate thisDate = LocalDate.now();
LocalTime thisTime = LocalTime.now().withNano(0);
//checking if user already signed in and wants to go to his user window
if(goToUserWindow && !(hasSignedOut())) {
this.userWindow();
}
//if first sign in of the day--create a list for today
if(!this._attendance.containsKey(thisDate)){
//no date key: means first sign-in of the day
//if it's the first sign-in: we need to create the arraylist for today
ArrayList<Tuple<LocalTime>> arrayListOfTheDay = new ArrayList<Tuple<LocalTime>>();
this._attendance.put(thisDate, arrayListOfTheDay);
arrayListOfTheDay.add(new Tuple<LocalTime>(null));//creates a new empty tuple so user can log again today
//creating the duration list of the day
ArrayList<String> listOfTodaysDuration = new ArrayList<String>();
this._dailyHoursAtWork.put(thisDate, listOfTodaysDuration);
}else{
//IF ALREADY CHECKED-IN WITHOUT CHECKING OUT: DON'T ALLOW TO CHECK-IN AGAIN
ArrayList<Tuple<LocalTime>> arrList = this._attendance.get(thisDate);
int indexOfLastTuple = arrList.size() - 1;
Tuple<LocalTime> currentLastTupleInArrayList = arrList.get(indexOfLastTuple);
if(currentLastTupleInArrayList._open != null){
//check if the last (current) tuple in the list
//has an sign-in value
String msg = "- YOU ALREADY SIGNED INTO WORK TODAY AT " + currentLastTupleInArrayList._open + "!";
String longestLine = "- IF THERE WAS A MISTAKE WITH YOUR TIME: PLEASE INFORM YOUR MANAGER.";
ManagementSystem.printPageHeaderNOBAR("");
ManagementSystem.matchBarToString(longestLine);
System.out.println(msg + "\n\n" + longestLine);
ManagementSystem.matchBarToString(longestLine);
System.out.println("");
ManagementSystem.pressEnterToContinue();
ManagementSystem.startLoginSystem();
return;
}
}
//printing page title and what not
String pageTitle = "Start Your Work Day: " + this.getFullName();
String pageTitleInfo = "Current Date: " + thisDate +"\n" + "Current Time: " + thisTime.withSecond(0) +"\n";
ManagementSystem.printPageHeaderWithBar(pageTitle);
System.out.println(pageTitleInfo);
ManagementSystem.matchBarToString(pageTitle);
String msg ="Have a Nice Productive Day!";
ManagementSystem.printPageHeaderWithBar(pageTitle);
System.out.println(pageTitleInfo);
System.out.println(msg);
ManagementSystem.matchBarToString(pageTitle);
System.out.println();
//updating the the list in the hashmap that stores the time stamps
Tuple<LocalTime> open = new Tuple<LocalTime>(thisTime);
ArrayList<Tuple<LocalTime>> listOfTodaysAttendance = this._attendance.get(thisDate);
int indexOfLastTuple = listOfTodaysAttendance.size() - 1;
listOfTodaysAttendance.set(indexOfLastTuple, open);
this._attendance.put(thisDate, listOfTodaysAttendance);
//go back to login window after signing in
ManagementSystem.pressEnterToContinue();
//true: go to user window; else go back to main login window
if(goToUserWindow) {
this.userWindow();
}else {
ManagementSystem.startLoginSystem();
}
}//end method: startWorkDay
public void endWorkDay() {
//takes current time and puts in database
LocalDate thisDate = LocalDate.now();
LocalTime thisTime = LocalTime.now().withNano(0);
//you can't set an end time without a start time
if(!this._attendance.containsKey(thisDate)){
String pageTitle = "YOU HAVEN'T SINGED IN TODAY!";
String longestLine ="\n- If You Forgot To Sign In: Please Inform Your Manager.\n";
ManagementSystem.printPageHeaderNOBAR(pageTitle);
ManagementSystem.matchBarToString(longestLine);
System.out.println(longestLine);
ManagementSystem.matchBarToString(longestLine);
System.out.println();
ManagementSystem.pressEnterToContinue();
ManagementSystem.startLoginSystem();
return;
}
//IF ALREADY LEFT: DON'T ALLOW TO CHANGE LEAVE TIME
ArrayList<Tuple<LocalTime>> arLst = this._attendance.get(thisDate);
int indexOfLastTuple = arLst.size() - 1;
Tuple<LocalTime> currentLastTupleInArrayList = arLst.get(indexOfLastTuple);
if(currentLastTupleInArrayList._open == null){
String pageTitle = "YOU ALREADY SIGNED OUT TODAY!";
String longestLine ="\n- If There Was a Mistake With Your Time: Please Inform Your Manager.\n";
ManagementSystem.printPageHeaderNOBAR(pageTitle);
ManagementSystem.matchBarToString(longestLine);
System.out.println(longestLine);
ManagementSystem.matchBarToString(longestLine);
System.out.println();
ManagementSystem.pressEnterToContinue();
ManagementSystem.startLoginSystem();
return;
}
//this will execute if haven't left, and did sign in
//adding the end time to the employee's attendance
ArrayList<Tuple<LocalTime>> listOfTodaysAttendance = this._attendance.get(thisDate);
indexOfLastTuple = listOfTodaysAttendance.size() - 1;
LocalTime startTime = listOfTodaysAttendance.get(indexOfLastTuple)._open;//takes start time from start tuple
Tuple<LocalTime> fullDay = new Tuple<LocalTime>(startTime, thisTime);//creats new tuple with full time--start and end
listOfTodaysAttendance.set(indexOfLastTuple, fullDay);//updates the last tuple in the list
this._attendance.put(thisDate, listOfTodaysAttendance);
//preparing time string for saving and printing
Duration currentDuration = Duration.between(startTime, thisTime);
Long currentDurationInSeconds = currentDuration.getSeconds();
//the total duration is the sum of all the durations
Long totalDurationInSeconds = 0L;// = totalDailyDuration.getSeconds();
Tuple<LocalTime> tpl;
Duration tplDuration;
for(int i=0; i<listOfTodaysAttendance.size();i++){
tpl = listOfTodaysAttendance.get(i);
tplDuration = Duration.between(tpl._open, tpl._close);
totalDurationInSeconds += tplDuration.getSeconds();
}
//updating the total number of seconds the worker worked this month
this.totalMonthlyTime += totalDurationInSeconds;
listOfTodaysAttendance.add(new Tuple<LocalTime>(null));//creates a new empty tuple so user can log again today
//currentDuration shoes start time and end time and total time
String topString ="- Date: " + thisDate + " | Day: " + thisDate.getDayOfWeek();
String duration = (currentDurationInSeconds/3600)%24 + "h " + (currentDurationInSeconds/60)%60 + "m " + currentDurationInSeconds%60 + "s ";
String stringOfCurrentDuration = "- In: " + startTime + " | Out: " + thisTime + " | Duration: " + duration + "";
String totalDuration = (totalDurationInSeconds/3600)%24 + "h " + (totalDurationInSeconds/60)%60 + "m " + totalDurationInSeconds%60 + "s ";
String bottomString = "Total Time Today: " + totalDuration;
ArrayList<String> durations = this._dailyHoursAtWork.get(thisDate);
durations.add(stringOfCurrentDuration);
//actual printing
String pageTitle = "GoodBye: " + this.getFullName();
ManagementSystem.printPageHeaderNOBAR(pageTitle);
ManagementSystem.matchBarToString(stringOfCurrentDuration);
System.out.println();
System.out.println(topString);
ManagementSystem.matchBarToString(stringOfCurrentDuration);
for(int i=0; i<durations.size();i++){
System.out.println(durations.get(i));
ManagementSystem.matchBarToString(stringOfCurrentDuration);
}
//System.out.println(stringOfCurrentDuration);
System.out.println();
System.out.println(bottomString);
ManagementSystem.matchBarToString(stringOfCurrentDuration);
System.out.println();
//saving the amount of hours the employe worked today
//for making the paycheck in the end of the month
this._dailyHoursAtWork.put(thisDate, durations);
this._totalDailyHoursAtWork.put(thisDate, totalDuration);
this.totalDaysWorked++;
ManagementSystem.pressEnterToContinue();
ManagementSystem.startLoginSystem();
}//end method: endWorkDay
public boolean hasSignedOut(){
//returns true if the user is trying to exit his account during an active session
//an active session is a session with a start time, but no end time
LocalDate thisDate = LocalDate.now();
ArrayList<Tuple<LocalTime>> todaysList = this._attendance.get(thisDate);
Tuple<LocalTime> lastSession;
try{
//if the user hasn't signed in at all today--the list doesn't exist!
lastSession = todaysList.get(todaysList.size()-1);
}catch(NullPointerException ex){
return true;
}
if(lastSession._open != null && lastSession._close == null){
return false;
}
return true;
}
public void printMyMonthlyTime(){
//prints the hour and date to the console
if(this._totalDailyHoursAtWork.isEmpty()) {
ManagementSystem.printPageHeaderWithBar("No Data Yet");
ManagementSystem.pressEnterToContinue();
this.userWindow();
return;
}
ArrayList<String> datesAndTime = new ArrayList<String>();
String longestLine = "";
String totalMonthTime = (this.totalMonthlyTime/3600) + "h " + (this.totalMonthlyTime/60)%60 + "m " + this.totalMonthlyTime%60 + "s ";
//k is the key in the hashmap. It's a LocalTime object
//v is the value of k in the hashmap. It's a String object.
this._totalDailyHoursAtWork.forEach((k, v) -> datesAndTime.add("- Date: " + k + " | Day: " + k.getDayOfWeek()+ " | Total Time: " + v ));
//will find the longest string
//so all the bars match it
for(int i=0; i< datesAndTime.size();i++) {
if(datesAndTime.get(i).length() > longestLine.length()) {
longestLine = datesAndTime.get(i);
}
}
String pageTitle = this.getFullName() + "'s MONTHLY ATTENDANCE TABLE:";
ManagementSystem.printPageHeaderNOBAR(pageTitle);
ManagementSystem.matchBarToString(longestLine);
System.out.println();
for(int i=0; i< datesAndTime.size();i++) {
ManagementSystem.matchBarToString(longestLine);
System.out.println(datesAndTime.get(i));
}//end printing for loop
ManagementSystem.matchBarToString(longestLine);
System.out.println();
System.out.println("Number Of Days Worked: " + this.totalDaysWorked);
ManagementSystem.matchBarToString(longestLine);
System.out.println("Total Monthly Time: " + totalMonthTime);
ManagementSystem.matchBarToString(longestLine);
System.out.println();
ManagementSystem.pressEnterToContinue();
this.userWindow();
}//end method: printMyMonthlyTime
public abstract void userWindow();
public abstract String typeOfObject();
}