-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay.java
More file actions
59 lines (50 loc) · 1.47 KB
/
Day.java
File metadata and controls
59 lines (50 loc) · 1.47 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
import java.time.*;
import java.util.ArrayList;
import java.io.Serializable;
public class Day implements Serializable {
private ArrayList<Sleep> listOfSleeps = new ArrayList<Sleep>();
private final LocalDate label;
private double totalDuration;
public Day(LocalDate label, Sleep firstSleep) {
this.label = label;
listOfSleeps.add(firstSleep);
totalDuration = firstSleep.getDuration();
}
public void addSleep(Sleep sleepToAdd) {
/* Iterate through the sleeps list to find where to add to to maintain
sorted list */
for(int i = 0; i < listOfSleeps.size(); i++) {
Sleep currentSleep = listOfSleeps.get(i);
if(currentSleep.getStartTime().isAfter(sleepToAdd.getStartTime())) {
listOfSleeps.add(i, sleepToAdd);
return;
}
}
// Add to the end if we didn't find a suitable spot
listOfSleeps.add(sleepToAdd);
totalDuration += sleepToAdd.getDuration();
}
public void removeSleep(Sleep sleepToRemove) {
listOfSleeps.remove(sleepToRemove);
// Delete this day if there are no more sleeps
if(listOfSleeps.isEmpty()) {
Main.listOfEntries.remove(this);
}
totalDuration -= sleepToRemove.getDuration();
}
public void resetTotalDuration() {
totalDuration = 0;
for(Sleep sleep : listOfSleeps) {
totalDuration += sleep.getDuration();
}
}
public Sleep[] getSleeps() {
return listOfSleeps.toArray(new Sleep[listOfSleeps.size()]);
}
public LocalDate getLabel() {
return label;
}
public double getTotalDuration() {
return totalDuration;
}
}