-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudyMentor.java
More file actions
395 lines (283 loc) · 12.7 KB
/
StudyMentor.java
File metadata and controls
395 lines (283 loc) · 12.7 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class StudyMentor {
private static DataHandler dataHandler;
private static ProgressTracker progressTracker;
private static AIProvider aiProvider;
private static Scanner scanner;
private static Profile profile; // FIXED: added missing profile variable
private static String currentAIProvider = "OpenAI";
private static List<String> conversationHistory;
public static void main(String[] args) {
scanner = new Scanner(System.in);
dataHandler = new DataHandler();
conversationHistory = dataHandler.loadHistory();
System.out.println("\n" + Colors.BLUE + "═══════════════════════════════════════════════════════════════");
System.out.println(Colors.CYAN + "🎓 Welcome to StudyMentor - Your AI-Powered Study Assistant!");
System.out.println(Colors.BLUE + "═══════════════════════════════════════════════════════════════" + Colors.RESET);
profile = dataHandler.loadProfile();
if (profile == null) {
createProfile();
} else {
System.out.println(Colors.GREEN + "\n✅ Profile loaded: " + profile.getName() + Colors.RESET);
currentAIProvider = profile.getPreferredAI();
}
progressTracker = new ProgressTracker();
progressTracker.loadStats(dataHandler);
initializeAIProvider();
boolean running = true;
while (running) {
showMainMenu();
int choice = getChoice(13);
switch (choice) {
case 1 -> askQuestion();
case 2 -> createStudyPlan();
case 3 -> explainConcept();
case 4 -> getMotivation();
case 5 -> viewProgress();
case 6 -> manageProfile();
case 7 -> exportHistory();
case 8 -> changeAIProvider();
case 9 -> viewStatistics();
case 0 -> {
System.out.println(Colors.YELLOW + "\n👋 Thanks for using StudyMentor! Keep studying!" + Colors.RESET);
running = false;
}
default -> System.out.println(Colors.RED + "❌ Invalid choice. Try again." + Colors.RESET);
}
}
dataHandler.saveProfile(profile);
progressTracker.saveStats(dataHandler);
scanner.close();
}
private static void initializeAIProvider() {
aiProvider = new AIProvider(currentAIProvider);
if (!aiProvider.isAvailable()) {
System.out.println(Colors.RED + "\n⚠️ AI key not found for " + currentAIProvider + Colors.RESET);
System.out.println(Colors.YELLOW + """
Set your environment variable:
OpenAI → set OPENAI_API_KEY=your_key
Gemini → set GOOGLE_API_KEY=your_key
""" + Colors.RESET);
} else {
System.out.println(Colors.GREEN + "✅ " + currentAIProvider + " initialized!" + Colors.RESET);
}
}
private static void createProfile() {
System.out.println(Colors.CYAN + "\n📝 Create Profile" + Colors.RESET);
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Grade/Level: ");
String grade = scanner.nextLine();
System.out.print("Email (optional): ");
String email = scanner.nextLine();
System.out.print("Subjects (comma-separated): ");
List<String> subjects = Arrays.stream(scanner.nextLine().split(","))
.map(String::trim).toList();
System.out.println("\nChoose AI Provider:");
System.out.println("1. OpenAI");
System.out.println("2. Gemini");
int choice = getChoice(2);
currentAIProvider = (choice == 1) ? "OpenAI" : "Gemini";
profile = new Profile( name, grade, email, subjects, currentAIProvider);
dataHandler.saveProfile(profile);
System.out.println(Colors.GREEN + "\n✅ Profile created!" + Colors.RESET);
}
private static void showMainMenu() {
System.out.println("\n" + Colors.BLUE + "═══════════════════════════════════════════════════════════════");
System.out.println(Colors.CYAN + "🏠 MAIN MENU | AI: " + currentAIProvider + " | Student: " + profile.getName());
System.out.println(Colors.BLUE + "═══════════════════════════════════════════════════════════════" + Colors.RESET);
System.out.println("""
1. 💬 Ask Question
2. 📋 Create Study Plan
3. 🧠 Explain Concept
4. 💪 Motivation
5. 📈 View Progress
6. 👤 Manage Profile
7. 💾 Export History
8. 🔄 Change AI Provider
9. 📊 View Statistics
0. 🚪 Exit
""");
System.out.print(Colors.CYAN + "Enter choice: " + Colors.RESET);
}
private static int getChoice(int max) {
try {
int c = Integer.parseInt(scanner.nextLine().trim());
if (c >= 0 && c <= max) return c;
} catch (Exception ignored) {}
return -1;
}
private static void askQuestion() {
System.out.println(Colors.CYAN + "\n💬 Ask your question" + Colors.RESET);
System.out.print("Your question: ");
String q = scanner.nextLine();
if (!aiProvider.isAvailable()) {
System.out.println(Colors.RED + "❌ AI Provider not available." + Colors.RESET);
return;
}
System.out.println(Colors.YELLOW + "\n🤖 Thinking..." + Colors.RESET);
try {
String ans = aiProvider.askQuestion(q);
System.out.println(Colors.GREEN + "\n📝 Answer:\n" + Colors.RESET + ans);
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
conversationHistory.add("[" + time + "] Q: " + q);
conversationHistory.add("[" + time + "] A: " + ans);
dataHandler.saveHistory(conversationHistory);
progressTracker.recordQuestion();
} catch (Exception e) {
System.out.println(Colors.RED + "❌ Error: " + e.getMessage() + Colors.RESET);
}
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
private static void createStudyPlan() {
System.out.print("\nSubject: ");
String subject = scanner.nextLine();
System.out.print("Duration (days): ");
String days = scanner.nextLine();
System.out.print("Daily hours: ");
String hours = scanner.nextLine();
System.out.print("Level (Beginner/Intermediate/Advanced): ");
String level = scanner.nextLine();
String prompt = """
Create a %s-day study plan for %s.
Level: %s
Daily time: %s hours
Include daily goals, topics, and weekly review.
""".formatted(days, subject, level, hours);
try {
String ans = aiProvider.askQuestion(prompt);
System.out.println(Colors.GREEN + "\n📋 Study Plan:\n" + Colors.RESET + ans);
progressTracker.recordStudyPlan();
} catch (Exception e) {
System.out.println("❌ " + e.getMessage());
}
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
private static void explainConcept() {
System.out.print("\nConcept: ");
String concept = scanner.nextLine();
System.out.println("""
1. ELI5
2. Technical
3. Visual
4. Analogy
""");
int c = getChoice(4);
String[] styles = {"ELI5", "Technical", "Visual", "Analogy"};
String style = styles[c - 1];
String prompt = "Explain " + concept + " in a " + style + " style.";
try {
String ans = aiProvider.askQuestion(prompt);
System.out.println(Colors.GREEN + "\n🧠 Explanation:\n" + Colors.RESET + ans);
} catch (Exception e) {
System.out.println("❌ " + e.getMessage());
}
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
private static void getMotivation() {
try {
String ans = aiProvider.askQuestion("Give motivational study tips.");
System.out.println(Colors.GREEN + "\n💪 Motivation:\n" + Colors.RESET + ans);
progressTracker.recordMotivation();
} catch (Exception e) {
System.out.println("❌ " + e.getMessage());
}
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
private static void viewProgress() {
System.out.println(Colors.CYAN + "\n📈 Progress" + Colors.RESET);
System.out.println("Name: " + profile.getName());
System.out.println("Grade: " + profile.getGrade());
System.out.println("Subjects: " + String.join(", ", profile.getSubjects()));
System.out.println("Sessions: " + progressTracker.getTotalSessions());
System.out.println("Questions: " + progressTracker.getTotalQuestions());
System.out.println("\nPress Enter...");
scanner.nextLine();
}
private static void manageProfile() {
System.out.println("""
1. View profile
2. Change name
3. Change grade
4. Change subjects
""");
int c = getChoice(4);
switch (c) {
case 1 -> {
System.out.println("Name: " + profile.getName());
System.out.println("Grade: " + profile.getGrade());
System.out.println("Email: " + profile.getEmail());
System.out.println("Subjects: " + String.join(", ", profile.getSubjects()));
}
case 2 -> {
System.out.print("New name: ");
profile.setName(scanner.nextLine());
}
case 3 -> {
System.out.print("New grade: ");
profile.setGrade(scanner.nextLine());
}
case 4 -> {
System.out.print("New subjects: ");
List<String> subs = Arrays.stream(scanner.nextLine().split(","))
.map(String::trim).toList();
profile.setSubjects(subs);
}
}
dataHandler.saveProfile(profile);
System.out.println("Updated!");
System.out.println("\nPress Enter...");
scanner.nextLine();
}
private static void exportHistory() {
if (conversationHistory.isEmpty()) {
System.out.println("⚠️ No history.");
return;
}
try {
String file = "study_history_" +
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))
+ ".txt";
FileWriter fw = new FileWriter(file);
for (String s : conversationHistory) fw.write(s + "\n");
fw.close();
System.out.println("Saved → " + file);
} catch (Exception e) {
System.out.println("❌ " + e.getMessage());
}
System.out.println("\nPress Enter...");
scanner.nextLine();
}
private static void changeAIProvider() {
System.out.println("""
1. OpenAI
2. Gemini
""");
int c = getChoice(2);
String newProvider = (c == 1) ? "OpenAI" : "Gemini";
if (!newProvider.equals(currentAIProvider)) {
currentAIProvider = newProvider;
profile.setPreferredAI(newProvider);
dataHandler.saveProfile(profile);
initializeAIProvider();
System.out.println("Changed!");
} else {
System.out.println("Already selected.");
}
System.out.println("\nPress Enter...");
scanner.nextLine();
scanner.nextLine();
}
private static void viewStatistics() {
progressTracker.displayStatistics();
System.out.println("\nPress Enter...");
scanner.nextLine();
}
}