forked from exercism/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitSecondStopwatch.java
More file actions
125 lines (102 loc) · 3.69 KB
/
Copy pathSplitSecondStopwatch.java
File metadata and controls
125 lines (102 loc) · 3.69 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
import java.util.ArrayList;
import java.util.List;
public class SplitSecondStopwatch {
/**
* A split-second stopwatch that tracks elapsed time with lap functionality.
* Supports start, stop, reset, and lap operations with precise time tracking.
* Times are formatted in HH:MM:SS format with two-digit precision.
*
* @see <a href="https://github.com/exercism/problem-specifications/tree/main/exercises/split-second-stopwatch">
* Problem Specifications
* </a>
*/
private enum State { READY, RUNNING, STOPPED }
private State state;
private long totalCompletedLaps; // Total time from completed laps
private long currentLapStart; // When current lap started
private long accumulated; // Accumulated time for current lap when stopped
private List<String> previousLaps;
private long mockTime;
public SplitSecondStopwatch() {
this.state = State.READY;
this.totalCompletedLaps = 0;
this.currentLapStart = 0;
this.accumulated = 0;
this.previousLaps = new ArrayList<>();
this.mockTime = 0;
}
public void start() {
if (state == State.RUNNING) {
throw new IllegalStateException("cannot start an already running stopwatch");
}
currentLapStart = mockTime;
state = State.RUNNING;
}
public void stop() {
if (state != State.RUNNING) {
throw new IllegalStateException("cannot stop a stopwatch that is not running");
}
accumulated += mockTime - currentLapStart;
state = State.STOPPED;
}
public void reset() {
if (state != State.STOPPED) {
throw new IllegalStateException("cannot reset a stopwatch that is not stopped");
}
state = State.READY;
totalCompletedLaps = 0;
currentLapStart = 0;
accumulated = 0;
previousLaps.clear();
}
public void lap() {
if (state != State.RUNNING) {
throw new IllegalStateException("cannot lap a stopwatch that is not running");
}
long currentLapTime = getCurrentLapTime();
totalCompletedLaps += currentLapTime;
previousLaps.add(formatTime(currentLapTime));
// Reset current lap and restart
accumulated = 0;
currentLapStart = mockTime;
}
public String state() {
return state.name().toLowerCase();
}
public String currentLap() {
return formatTime(getCurrentLapTime());
}
public String total() {
return formatTime(totalCompletedLaps + getCurrentLapTime());
}
public List<String> previousLaps() {
return new ArrayList<>(previousLaps);
}
public void advanceTime(String timeString) {
String[] parts = timeString.split(":");
long hours = Long.parseLong(parts[0]);
long minutes = Long.parseLong(parts[1]);
long seconds = Long.parseLong(parts[2]);
long milliseconds = (hours * 3600 + minutes * 60 + seconds) * 1000;
mockTime += milliseconds;
}
private long getCurrentLapTime() {
switch (state) {
case READY:
return 0;
case RUNNING:
return accumulated + (mockTime - currentLapStart);
case STOPPED:
return accumulated;
default:
throw new IllegalStateException("Invalid state");
}
}
private String formatTime(long milliseconds) {
long totalSeconds = milliseconds / 1000;
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
}