-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmployeeAnalyzer.java
138 lines (125 loc) · 5.4 KB
/
EmployeeAnalyzer.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
class EmployeeData {
String positionID;
String positionStatus;
Date time;
Date timeOut;
String timecardHours;
Date payCycleStartDate;
Date payCycleEndDate;
String employeeName;
String fileNumber;
EmployeeData(String positionID, String positionStatus, Date time, Date timeOut, String timecardHours,
Date payCycleStartDate, Date payCycleEndDate, String employeeName, String fileNumber) {
this.positionID = positionID;
this.positionStatus = positionStatus;
this.time = time;
this.timeOut = timeOut;
this.timecardHours = timecardHours;
this.payCycleStartDate = payCycleStartDate;
this.payCycleEndDate = payCycleEndDate;
this.employeeName = employeeName;
this.fileNumber = fileNumber;
}
}
public class EmployeeAnalyzer {
public static void main(String[] args) {
String filename = "employee_records.csv"; // Replace with your input file path
List<EmployeeData> records = readEmployeeRecordsFromCSV(filename);
for (EmployeeData record : records) {
if (workedForSevenConsecutiveDays(records, record.employeeName)) {
System.out.println(record.employeeName + " has worked for 7 consecutive days.");
}
if (hasShortBreakBetweenShifts(records, record.employeeName)) {
System.out.println(record.employeeName + " has less than 10 hours between shifts but greater than 1 hour.");
}
if (workedForMoreThan14Hours(records, record.employeeName)) {
System.out.println(record.employeeName + " has worked for more than 14 hours in a single shift.");
}
System.out.println("------------------------");
}
}
private static List<EmployeeData> readEmployeeRecordsFromCSV(String filename) {
List<EmployeeData> records = new ArrayList<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
br.readLine(); // Skip the header line
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
String positionID = parts[0];
String positionStatus = parts[1];
Date time = parseDate(parts[2]);
Date timeOut = parseDate(parts[3]);
String timecardHours = parts[4];
Date payCycleStartDate = parseDate(parts[5]);
Date payCycleEndDate = parseDate(parts[6]);
String employeeName = parts[7];
String fileNumber = parts[8];
records.add(new EmployeeData(positionID, positionStatus, time, timeOut, timecardHours,
payCycleStartDate, payCycleEndDate, employeeName, fileNumber));
}
} catch (IOException e) {
e.printStackTrace();
}
return records;
}
private static Date parseDate(String dateString) {
if (dateString.isEmpty()) {
return null;
}
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
return dateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
private static boolean workedForSevenConsecutiveDays(List<EmployeeData> records, String name) {
int consecutiveDays = 0;
for (EmployeeData record : records) {
if (record.employeeName.equals(name)) {
consecutiveDays++;
if (consecutiveDays == 7) {
return true;
}
} else {
consecutiveDays = 0;
}
}
return false;
}
private static boolean hasShortBreakBetweenShifts(List<EmployeeData> records, String name) {
for (int i = 0; i < records.size() - 1; i++) {
EmployeeData currentRecord = records.get(i);
EmployeeData nextRecord = records.get(i + 1);
if (currentRecord.employeeName.equals(name) && nextRecord.employeeName.equals(name) &&
currentRecord.timeOut != null && nextRecord.time != null) { // Added null checks
long timeDifferenceHours = (nextRecord.time.getTime() - currentRecord.timeOut.getTime()) / 3600000;
if (timeDifferenceHours > 1 && timeDifferenceHours < 10) {
return true;
}
}
}
return false;
}
private static boolean workedForMoreThan14Hours(List<EmployeeData> records, String name) {
for (EmployeeData record : records) {
if (record.employeeName.equals(name) && record.time != null && record.timeOut != null) { // Added null checks
long shiftHours = (record.timeOut.getTime() - record.time.getTime()) / 3600000;
if (shiftHours > 14) {
return true;
}
}
}
return false;
}
}