-
Notifications
You must be signed in to change notification settings - Fork 0
/
Department.java
131 lines (114 loc) · 3.96 KB
/
Department.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
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Calendar;
import java.util.Date;
import java.io.Serializable;
public class Department implements Serializable {
String code;
DepartmentType type;
HashMap<String, Employee> employees;
HashMap<String, ArrayList<Ticket>> tickets;
public Department(String code, DepartmentType type, HashMap<String, Employee> employees, HashMap<String, ArrayList<Ticket>> tickets) {
this.code = code;
this.type = type;
this.employees = employees;
this.tickets = tickets;
}
public Department(String code, DepartmentType type) {
this(code, type, new HashMap<String, Employee>(), new HashMap<String, ArrayList<Ticket>>());
}
public Employee getEmployee(String id) {
return employees.get(id);
}
public Employee addEmployee(String id) {
Employee employee = new Employee(id);
employees.put(employee.id, employee);
return employee;
}
public Ticket punch(String employeeId, HourType hourType) {
return punch(employeeId, hourType, new Date());
}
public void punch(String id, HourType hourType, int day, int hours, int minutes) {
Calendar calStart = Calendar.getInstance();
calStart.set(Calendar.DAY_OF_WEEK, day);
calStart.set(Calendar.HOUR_OF_DAY, 1);
calStart.set(Calendar.MINUTE, 0);
calStart.set(Calendar.SECOND, 0);
calStart.set(Calendar.MILLISECOND, 0);
Calendar calEnd = Calendar.getInstance();
calEnd.set(Calendar.DAY_OF_WEEK, day);
calEnd.set(Calendar.HOUR_OF_DAY, hours+1);
calEnd.set(Calendar.MINUTE, minutes);
calEnd.set(Calendar.SECOND, 0);
calEnd.set(Calendar.MILLISECOND, 0);
Date start = calStart.getTime();
Date end = calEnd.getTime();
punch(id, hourType, start);
punch(id, hourType, end);
}
public Timesheet getTimesheet(String employeeId, Date date) {
ArrayList<Ticket> employeeTickets = getEmployeeTickets(employeeId, date);
Timesheet sheet = new Timesheet(employeeTickets);
return sheet;
}
public Timesheet getTimesheet(String id, Date[] dates) {
ArrayList<Ticket> tickets = new ArrayList<Ticket>();
for(Date date : dates) {
ArrayList<Ticket> ticketsForDate = getEmployeeTickets(id, date);
tickets.addAll(ticketsForDate);
}
return new Timesheet(tickets);
}
public ArrayList<Ticket> getEmployeeTickets(String id, Date date) {
ArrayList<Ticket> employeeTickets = new ArrayList<Ticket>();
ArrayList<Ticket> ticketsForDate = tickets.get(Ticket.keyFromDate(date));
if(ticketsForDate == null) {
return employeeTickets;
}
for(Ticket ticket : ticketsForDate) {
if(ticket.employeeId.equals(id)) {
employeeTickets.add(ticket);
}
}
return employeeTickets;
}
public Ticket punch(String employeeId, HourType hourType, Date date) {
Employee employee = getEmployee(employeeId);
if(employee == null) {
return null;
}
if(tickets.isEmpty()) {
return clockIn(employee.id, hourType, date);
}
ArrayList<Ticket> employeeTickets = getEmployeeTickets(employeeId, date);
if(employeeTickets.isEmpty()) {
return clockIn(employee.id, hourType, date);
}
int lastIndex = employeeTickets.size() - 1;
Ticket latestTicket = employeeTickets.get(lastIndex);
if(latestTicket.ticketType == TicketType.ClockIn) {
return clockOut(employee.id, hourType, date);
}
return clockIn(employee.id, hourType, date);
}
private Ticket clockIn(String employeeId, HourType hourType, Date date) {
Ticket ticket = new Ticket(date, hourType, TicketType.ClockIn, employeeId);
ArrayList<Ticket>ticketsForDay = tickets.get(ticket.dateKey());
if(ticketsForDay == null) {
ticketsForDay = new ArrayList<Ticket>();
tickets.put(ticket.dateKey(), ticketsForDay);
}
ticketsForDay.add(ticket);
return ticket;
}
private Ticket clockOut(String employeeId, HourType hourType, Date date) {
Ticket ticket = new Ticket(date, hourType, TicketType.ClockOut, employeeId);
ArrayList<Ticket>ticketsForDay = tickets.get(ticket.dateKey());
if(ticketsForDay == null) {
return null;
}
ticketsForDay.add(ticket);
return ticket;
}
}