-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkingHours.java
More file actions
166 lines (150 loc) · 4.79 KB
/
WorkingHours.java
File metadata and controls
166 lines (150 loc) · 4.79 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
import java.io.*;
import java.time.*;
import java.util.*;
import java.util.regex.Pattern;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.time.LocalTime.now;
import static java.time.temporal.ChronoUnit.MINUTES;
import static java.util.Comparator.comparing;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.groupingBy;
public class WorkingHours {
public static void main(
String... args
) throws Exception {
readJournalByDate()
.forEach(WorkingHours::printWorkingHours);
}
private static Map<String, List<JournalEntry>> readJournalByDate(
) throws IOException {
try (
var reader = new InputStreamReader(System.in, UTF_8);
) {
return new BufferedReader(reader)
.lines()
.filter(WorkingHours::isTopicLine)
.map(JournalEntry::parse)
.collect(groupingBy(JournalEntry::date));
}
}
private static boolean isTopicLine(
String line
) {
return Pattern.matches(
"\\[(\\d{4}-\\d{2}-\\d{2}) (\\d{2}:\\d{2})\\] (.*)",
line
);
}
private static void printWorkingHours(
String date,
List<JournalEntry> journalEntries
) {
var dailyAccounting = sumJournalEntries(date, journalEntries);
printWorkingHours(dailyAccounting);
}
private static DailyAccounting sumJournalEntries(
String date,
List<JournalEntry> journalEntries
) {
var dailyAccounting = new DailyAccounting(date);
journalEntries.stream()
.sorted(comparing(JournalEntry::time))
.forEach(dailyAccounting::add);
return dailyAccounting;
}
private static void printWorkingHours(
DailyAccounting dailyAccounting
) {
var duration = dailyAccounting.getWorkingTime();
System.out.printf(
"%s %02d:%02d %s%n",
dailyAccounting.date,
duration.toHours(),
duration.toMinutesPart(),
dailyAccounting.pauseRegistered ? "" : "no pause"
);
}
static record JournalEntry (
String date,
LocalTime time,
String activity
) {
static JournalEntry parse(
String line
) {
var dateAndRest = splitOnFirstWhitespace(line);
var date = dateAndRest[0].substring(1);
var timeAndActivity = splitOnFirstWhitespace(dateAndRest[1]);
var time = LocalTime.parse(
removeLastChar(timeAndActivity[0]));
var activity = timeAndActivity[1];
return new JournalEntry(date, time, activity);
}
private static String[] splitOnFirstWhitespace(
String text
) {
var indexWhitespace = text.indexOf(' ');
return new String[] {
text.substring(0, indexWhitespace),
text.substring(indexWhitespace + 1)
};
}
private static String removeLastChar(String text) {
return text.substring(
0,
text.length() - 1);
}
}
static class DailyAccounting {
final String date;
boolean active;
boolean endOfDayRegistered;
long numMinutesWorked;
boolean pauseRegistered;
LocalTime startOfLastActivePhase;
DailyAccounting(
String date
) {
this.date = requireNonNull(date, "The date is missing.");
}
void add(
JournalEntry journalEntry
) {
var activity = journalEntry.activity().trim().toLowerCase();
switch (activity) {
case "feierabend":
setInactive(journalEntry.time);
endOfDayRegistered = true;
break;
case "lunch":
case "pause":
setInactive(journalEntry.time);
pauseRegistered = true;
break;
default:
setActive(journalEntry.time);
}
}
Duration getWorkingTime() {
if (active)
return Duration.ofMinutes(
numMinutesWorked + startOfLastActivePhase.until(now(), MINUTES));
else
return Duration.ofMinutes(numMinutesWorked);
}
private void setActive(
LocalTime time
) {
if (!active)
startOfLastActivePhase = time;
active = true;
}
private void setInactive(
LocalTime time
) {
if (active)
numMinutesWorked += startOfLastActivePhase.until(time, MINUTES);
active = false;
}
}
}