-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
204 lines (154 loc) · 6.62 KB
/
Main.java
File metadata and controls
204 lines (154 loc) · 6.62 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.time.LocalDate;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<StudySession> sessions = FileManager.loadSessions();
int nextId = sessions.size() + 1;
Thread autoSaveThread = new Thread(() -> {
while (true) {
try {
Thread.sleep(10000);
FileManager.saveSessions(sessions);
} catch (InterruptedException e) {
break;
}
}
});
autoSaveThread.setDaemon(true);
autoSaveThread.start();
while (true) {
System.out.println("\n--- Study Tracker ---");
System.out.println("1. Add Session");
System.out.println("2. View Sessions");
System.out.println("3. Search by Subject");
System.out.println("4. Delete Session");
System.out.println("5. Show Stats");
System.out.println("6. Start Live Session");
System.out.println("7. Save & Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
sc.nextLine();
System.out.println("Choose subject:");
System.out.println("1. Math");
System.out.println("2. Physics");
System.out.println("3. Coding");
int subChoice = sc.nextInt();
sc.nextLine();
String subject = switch (subChoice) {
case 1 -> "Math";
case 2 -> "Physics";
case 3 -> "Coding";
default -> "Other";
};
System.out.print("Enter hours: ");
int h = sc.nextInt();
System.out.print("Enter minutes: ");
int m = sc.nextInt();
int totalSeconds = h * 3600 + m * 60; // no seconds input
sessions.add(new StudySession(
nextId++,
subject,
totalSeconds,
LocalDate.now()
));
System.out.println("Session added!");
break;
case 2:
if (sessions.isEmpty()) {
System.out.println("No sessions found.");
} else {
sessions.forEach(System.out::println);
}
break;
case 3:
sc.nextLine();
System.out.print("Enter subject to search: ");
String search = sc.nextLine();
boolean found = false;
for (StudySession s1 : sessions) {
if (s1.getSubject().equalsIgnoreCase(search)) {
System.out.println(s1);
found = true;
}
}
if (!found) {
System.out.println("No sessions found.");
}
break;
case 4:
System.out.print("Enter ID to delete: ");
int id = sc.nextInt();
boolean removed = sessions.removeIf(s1 -> s1.getId() == id);
System.out.println(removed ? "Deleted." : "ID not found.");
break;
case 5:
if (sessions.isEmpty()) {
System.out.println("No data available.");
break;
}
Map<String, Integer> stats = new HashMap<>();
for (StudySession s1 : sessions) {
stats.put(
s1.getSubject(),
stats.getOrDefault(s1.getSubject(), 0) + s1.getDurationSeconds()
);
}
System.out.println("\n--- Study Stats ---");
String topSubject = "";
int maxTime = 0;
for (String key : stats.keySet()) {
int sec = stats.get(key);
int hr = sec / 3600;
int min = (sec % 3600) / 60;
int secRem = sec % 60;
System.out.println(key + ": " + hr + "h " + min + "m " + secRem + "s");
if (sec > maxTime) {
maxTime = sec;
topSubject = key;
}
}
System.out.println("Most Studied Subject: " + topSubject);
break;
case 6:
sc.nextLine();
System.out.println("Choose subject:");
System.out.println("1. Math");
System.out.println("2. Physics");
System.out.println("3. Coding");
int subChoiceLive = sc.nextInt();
sc.nextLine();
String subjectLive = switch (subChoiceLive) {
case 1 -> "Math";
case 2 -> "Physics";
case 3 -> "Coding";
default -> "Other";
};
System.out.println("Session started... Press ENTER to stop");
long startTime = System.currentTimeMillis();
sc.nextLine();
long endTime = System.currentTimeMillis();
int seconds = (int)((endTime - startTime) / 1000);
sessions.add(new StudySession(
nextId++,
subjectLive,
seconds,
LocalDate.now()
));
int hr = seconds / 3600;
int min = (seconds % 3600) / 60;
int secRem = seconds % 60;
System.out.println("Session recorded: " + hr + "h " + min + "m " + secRem + "s");
break;
case 7:
FileManager.saveSessions(sessions);
System.out.println("Saved. Goodbye!");
return;
default:
System.out.println("Invalid choice.");
}
}
}
}