-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryScreen.java
More file actions
154 lines (142 loc) · 5.01 KB
/
EntryScreen.java
File metadata and controls
154 lines (142 loc) · 5.01 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
public class EntryScreen {
// The number of days we will skip if the user wants to and can
private static final int daysToSkip = 7;
private static boolean hasPrevious;
private static boolean hasNext;
private static boolean hasSkippablePrevious;
private static boolean hasSkippableNext;
private static final String previousKey = "<";
private static final String nextKey = ">";
private static final String skipPreviousKey = "<<";
private static final String skipNextKey = ">>";
private static final String editKey = "E";
private static final String removeKey = "R";
private static final String homeKey = "H";
public static void startEntries(LinkedList<Day>.Node nodeToPull) {
// 1. Clear the screen
Printer.clearScreen();
// 2. Display the message
Printer.displayEntryScreen(nodeToPull.getData());
// 3. Display the options
displayOptions(nodeToPull);
// 4. Get user's option selection
getUserSelection(nodeToPull);
}
private static void displayOptions(LinkedList<Day>.Node nodeToPull) {
Day dayToPull = nodeToPull.getData();
// If we have a previous and a next
hasPrevious = nodeToPull.getPrevious() != null;
hasNext = nodeToPull.getNext() != null;
// If there is a previous entry to skip to in large lists
hasSkippablePrevious = (hasPrevious)
&& (Main.listOfEntries.getSize() > daysToSkip)
&& (Main.listOfEntries.indexOf(dayToPull) > daysToSkip);
// If there is a next entry to skip to in large lists
hasSkippableNext = (hasNext)
&& (Main.listOfEntries.getSize() > daysToSkip)
&& (Main.listOfEntries.indexOf(dayToPull) <
(Main.listOfEntries.getSize() - daysToSkip));
// If there is a previous entry, display the option to go back to it
if(hasPrevious) {
Printer.displayPreviousArrow();
}
/* If there is a previous entry to skip to in large lists, display the
option to skip backward */
if(hasSkippablePrevious) {
Printer.displaySkipPreviousArrow();
}
// If there is a next entry, display the option to go to it
if(hasNext) {
Printer.displayNextArrow();
}
// If there is a next entry to skip to in large lists, display the option to skip forward
if(hasSkippableNext) {
Printer.displaySkipNextArrow();
}
// Display the other options
Printer.displayOtherOptions();
}
private static void getUserSelection(LinkedList<Day>.Node nodeToPull) {
Day dayToPull = nodeToPull.getData();
Sleep[] sleepsToPull = dayToPull.getSleeps();
label:
while(true) {
String userSelection = UserInput.getRegularInput();
// User chose to go to the previous entry
if(hasPrevious && userSelection.equals(previousKey)) {
startEntries(nodeToPull.getPrevious());
break;
}
// User chose to skip to previous
if(hasSkippablePrevious && userSelection.equals(skipPreviousKey)) {
startEntries(Main.listOfEntries.nodeOfIndex(
Main.listOfEntries.indexOf(dayToPull) - daysToSkip));
}
// User chose to go to the next entry
if(hasNext && userSelection.equals(nextKey)) {
startEntries(nodeToPull.getNext());
break;
}
// User chose to skip to next
if(hasSkippableNext && userSelection.equals(skipNextKey)) {
startEntries(Main.listOfEntries.nodeOfIndex(
Main.listOfEntries.indexOf(dayToPull) + daysToSkip));
}
switch (userSelection) {
// User chose to edit the entry
case editKey:
EditScreen.startEdit(nodeToPull);
break label;
// User chose to remove the entry
case removeKey:
LinkedList<Day>.Node previousNode = nodeToPull.getPrevious();
// If we only have one Sleep, just remove it
if (sleepsToPull.length == 1) {
dayToPull.removeSleep(sleepsToPull[0]);
// Else we need to ask the user which to delete
} else {
RemoveScreen.startRemove(nodeToPull);
}
// If there is a previous, go to that
if (hasPrevious) {
startEntries(previousNode);
// Else go to the next node if there is one
} else if (hasNext) {
startEntries(nodeToPull.getNext());
// Else go home because there are no entries to view
} else {
HomeScreen.startHome();
}
break label;
// User chose to go home
case homeKey:
HomeScreen.startHome();
break label;
// User had invalid input
default:
Printer.displayBadSelection();
}
}
}
public static String getPreviousKey() {
return previousKey;
}
public static String getNextKey() {
return nextKey;
}
public static String getSkipPreviousKey() {
return skipPreviousKey;
}
public static String getSkipNextKey() {
return skipNextKey;
}
public static String getEditKey() {
return editKey;
}
public static String getRemoveKey() {
return removeKey;
}
public static String getHomeKey() {
return homeKey;
}
}