-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddScreen.java
More file actions
117 lines (109 loc) · 3.91 KB
/
AddScreen.java
File metadata and controls
117 lines (109 loc) · 3.91 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
import java.time.format.DateTimeParseException;
import java.time.*;
public class AddScreen {
public static void startAddEntry() {
// 1. Clear the screen
Printer.clearScreen();
// 2. Display the message
Printer.displayAddEntry();
// 3. Try to get a start time from the user
LocalDateTime startTime = getUserDate();
// 4. Display further message
Printer.askForEndDate();
// 5. Try to get an end time from the user
LocalDateTime endTime = getUserDate();
// 8. Go back to the entries with the new node that was added
EntryScreen.startEntries(makeNewSleepNode(startTime, endTime));
}
public static LocalDateTime getUserDate() {
while(true) {
try {
return UserInput.getDateTimeFromUser();
/* If the user doesn't enter a date and time in the correct
format */
} catch(DateTimeParseException badInputException) {
Printer.displayBadFormat();
}
}
}
/* Add the sleep to either an existing node or a new node and return that
node */
public static LinkedList<Day>.Node addSleep(Sleep newSleep) {
// Get the data we need
LocalDateTime startTime = newSleep.getStartTime();
LocalDate sleepDate = startTime.toLocalDate();
String sleepTime = Formatter.formatData(startTime);
// Check if the start time is AM or PM
String amPM = sleepTime.substring(sleepTime.length() - 2);
boolean isAM = amPM.equals("AM");
/* If the start time is AM, ask which date we want to add to: current
or day before */
if(isAM) {
sleepDate = checkWhichDayToAddTo(sleepDate);
}
// If this is the first node, simply add to front
if(Main.listOfEntries.getSize() == 0) {
Day newDay = new Day(sleepDate, newSleep);
Main.listOfEntries.addToFront(newDay);
return Main.listOfEntries.getFirst();
}
// Traverse through the linked list if this isn't the first node
LinkedList<Day>.Node potentialNode = addToLinkedList(sleepDate,
newSleep);
if(potentialNode != null) {
return potentialNode;
}
// If we get here, no node was after so we add a new node to the back
Day newDay = new Day(sleepDate, newSleep);
Main.listOfEntries.addToBack(newDay);
return Main.listOfEntries.getLast();
}
// Check if we want to add to the current date or the previous date
private static LocalDate checkWhichDayToAddTo(LocalDate sleepDate) {
Printer.askWhichDayToAddTo(sleepDate);
while(true) {
String userSelection = UserInput.getRegularInput();
// User selected current date, do nothing
if(userSelection.equals("1")) {
break;
}
// User selected previous date, change our date to day before
if(userSelection.equals("2")) {
sleepDate = sleepDate.minusDays(1);
break;
}
// User didn't enter an appropriate response
Printer.displayBadSelection();
}
return sleepDate;
}
// Traverse through the linked list to find where to add to
private static LinkedList<Day>.Node addToLinkedList(LocalDate sleepDate,
Sleep newSleep) {
LinkedList<Day>.Node currentNode = Main.listOfEntries.getFirst();
for(int index = 0; index < Main.listOfEntries.getSize(); index++) {
Day currentDay = currentNode.getData();
LocalDate currentDate = currentDay.getLabel();
// If we already have this day as a node, add the sleep to it
if(currentDate.equals(sleepDate)) {
currentDay.addSleep(newSleep);
return currentNode;
}
// Else, place the new node in the correct spot
if(currentDate.isAfter(sleepDate)) {
Day newDay = new Day(sleepDate, newSleep);
Main.listOfEntries.add(newDay, index);
return Main.listOfEntries.nodeOfObject(newDay);
}
currentNode = currentNode.getNext();
}
return null;
}
// Add a new sleep and return the node it was added to
private static LinkedList<Day>.Node makeNewSleepNode(LocalDateTime startTime, LocalDateTime endTime) {
// Make a new sleep out of the inputs
Sleep newSleep = new Sleep(startTime, endTime);
// Add the sleep and return the associated node
return addSleep(newSleep);
}
}