-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeScreen.java
More file actions
102 lines (92 loc) · 2.8 KB
/
HomeScreen.java
File metadata and controls
102 lines (92 loc) · 2.8 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
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
public class HomeScreen {
private static final String readFromFileKey = "R";
private static final String viewEntriesKey = "E";
private static final String addEntryKey = "A";
private static final String viewStatsKey = "S";
private static final String saveKey = "F";
public static void startHome() {
// 1. Clear the screen
Printer.clearScreen();
// 2. Display the message and the options
Printer.displayHome();
// 3. Get the user's option selection
getUserSelection();
}
private static void getUserSelection() {
while(true) {
String userSelection = UserInput.getRegularInput();
userSelection = userSelection.toUpperCase();
// Allow us to import the entries if we have a file to import from
File objectFile = new File("Entries.ser");
if(userSelection.equals(readFromFileKey) && Main.listOfEntries.getSize() == 0 && objectFile.isFile()) {
try(FileInputStream fileInput = new FileInputStream("Entries.ser");
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
) {
Main.listOfEntries = (LinkedList<Day>)objectInput.readObject();
startHome();
break;
} catch(IOException e) {
e.printStackTrace();
break;
} catch(ClassNotFoundException e) {
Printer.displayError();
break;
}
}
// Allow view entries if we have any entries
if(userSelection.equals(viewEntriesKey)
&& Main.listOfEntries.getSize() > 0) {
EntryScreen.startEntries(Main.listOfEntries.getFirst());
break;
}
// Add entry
if(userSelection.equals(addEntryKey)) {
AddScreen.startAddEntry();
break;
}
// Allow view stats if we have two or more entries
if(userSelection.equals(viewStatsKey)
&& Main.listOfEntries.getSize() > 1) {
StatsScreen.startStats();
break;
}
// Allow save if we have any entries
if(userSelection.equals(saveKey) && Main.listOfEntries.getSize() >= 1) {
saveToFile();
break;
}
// User didn't enter a valid input
Printer.displayBadSelection();
}
}
private static void saveToFile() {
try(FileOutputStream fileOutput = new FileOutputStream("Entries.ser");
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
) {
objectOutput.writeObject(Main.listOfEntries);
} catch(IOException e) {
Printer.displayError();
}
}
public static String getReadFromFileKey() {
return readFromFileKey;
}
public static String getViewEntriesKey() {
return viewEntriesKey;
}
public static String getAddEntryKey() {
return addEntryKey;
}
public static String getViewStatsKey() {
return viewStatsKey;
}
public static String getSaveKey() {
return saveKey;
}
}