M

Data stored

public
mdjunayet May 22, 2024 Never 26
Clone
C Data Stored 579 lines (526 loc) | 19.19 KB
1
#include <windows.h>
2
#include <string.h>
3
#include <time.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
7
#define MAX_USERS 100
8
#define MAX_SESSIONS 100
9
#define MAX_TODO 100
10
#define MAX_POMODOROS 100
11
12
13
/// Define structures for user accounts, study sessions, and to-do items
14
typedef struct {
15
char description[100];
16
int completed;
17
} ToDoItem;
18
19
typedef struct {
20
char category[50];
21
struct tm startTime;
22
struct tm endTime;
23
int duration; // in seconds
24
int sessionActive; // Indicates if a session is currently active
25
int countdownActive; // Indicates if a countdown is currently active
26
int countdownDuration; // in seconds
27
char sessionType[20]; // Type of the session (Timer, Countdown, Pomodoro)
28
} StudySession;
29
30
typedef struct {
31
int studyDuration; // in seconds
32
int breakDuration; // in seconds
33
} PomodoroSession;
34
35
typedef struct {
36
char username[50];
37
int sessionCount;
38
StudySession sessions[MAX_SESSIONS];
39
ToDoItem todos[MAX_TODO];
40
int todoCount;
41
PomodoroSession pomodoros[MAX_POMODOROS];
42
int pomodoroCount;
43
} User;
44
45
// Global array to store users
46
User users[MAX_USERS];
47
int userCount = 0;
48
int loggedIn = -1; // Index of logged-in user, -1 if no user is logged in
49
50
51
// Function prototypes
52
void clearScreen();
53
void displayHeader(const char* header);
54
void displayMainMenu();
55
void displayUserMenu();
56
void createUser();
57
void loginUser();
58
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
59
60
void startStudyOption();
61
void endSession();
62
void showStatistics();
63
void manageToDoList();
64
int findUserIndex(const char* username);
65
void pressEnterToContinue();
66
void startTimer();
67
void startCountdown();
68
int updateCountdown(int totalSeconds);
69
void pomodoro(int userIndex);
70
71
// Function to clear the screen
72
void clearScreen() {
73
#ifdef _WIN32
74
system("cls");
75
#else
76
system("clear");
77
#endif
78
}
79
80
// Function to display a header
81
void displayHeader(const char* header) {
82
clearScreen();
83
printf("== %s ==\n\n", header);
84
}
85
86
// Function to display the main menu
87
void displayMainMenu() {
88
system("color 3F");
89
displayHeader("Main Menu");
90
printf("1. Create new user\n");
91
printf("2. Login\n");
92
printf("3. Exit\n");
93
printf("Enter your choice: ");
94
}
95
96
// Function to display the user menu
97
void displayUserMenu() {
98
displayHeader("User Menu");
99
printf("1. Start a study session\n");
100
printf("2. Show statistics\n");
101
printf("3. Manage to-do list\n");
102
printf("4. Logout\n");
103
printf("Enter your choice: ");
104
}
105
106
// Function to create a new user
107
void createUser() {
108
if (userCount >= MAX_USERS) {
109
MessageBox(NULL, "Error: Maximum number of users reached.", "Error", MB_OK | MB_ICONERROR);
110
return;
111
}
112
char username[50];
113
printf("Enter username: ");
114
scanf("%49s", username);
115
strcpy(users[userCount].username, username);
116
users[userCount].sessionCount = 0;
117
users[userCount].todoCount = 0;
118
userCount++;
119
MessageBox(NULL, "User created successfully.", "Success", MB_OK | MB_ICONINFORMATION);
120
}
121
122
// Function to login a user
123
void loginUser() {
124
char username[50];
125
printf("Enter username: ");
126
scanf("%49s", username);
127
for (int i = 0; i < userCount; i++) {
128
if (strcmp(users[i].username, username) == 0) {
129
loggedIn = i;
130
MessageBox(NULL, "Login successful.", "Success", MB_OK | MB_ICONINFORMATION);
131
return;
132
}
133
}
134
MessageBox(NULL, "Error: User not found.", "Error", MB_OK | MB_ICONERROR);
135
}
136
137
// Entry point
138
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
139
const char CLASS_NAME[] = "Sample Window Class";
140
141
WNDCLASS wc = { };
142
wc.lpfnWndProc = WindowProc;
143
wc.hInstance = hInstance;
144
wc.lpszClassName = CLASS_NAME;
145
146
RegisterClass(&wc);
147
148
HWND hwnd = CreateWindowEx(
149
0,
150
CLASS_NAME,
151
"Study Tracker",
152
WS_OVERLAPPEDWINDOW,
153
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
154
NULL,
155
NULL,
156
hInstance,
157
NULL
158
);
159
160
if (hwnd == NULL) {
161
return 0;
162
}
163
164
ShowWindow(hwnd, nCmdShow);
165
166
MSG msg = { };
167
while (GetMessage(&msg, NULL, 0, 0)) {
168
TranslateMessage(&msg);
169
DispatchMessage(&msg);
170
}
171
172
return 0;
173
}
174
175
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
176
switch (uMsg) {
177
case WM_DESTROY:
178
PostQuitMessage(0);
179
return 0;
180
181
case WM_COMMAND:
182
switch (LOWORD(wParam)) {
183
case 1: // Create User
184
createUser();
185
break;
186
case 2: // Login
187
loginUser();
188
break;
189
}
190
break;
191
192
case WM_CREATE: {
193
HMENU hMenu = CreateMenu();
194
AppendMenu(hMenu, MF_STRING, 1, "Create User");
195
AppendMenu(hMenu, MF_STRING, 2, "Login");
196
SetMenu(hwnd, hMenu);
197
}
198
return 0;
199
}
200
201
return DefWindowProc(hwnd, uMsg, wParam, lParam);
202
}
203
204
// Function to press enter to continue
205
void pressEnterToContinue() {
206
printf("\nPress Enter to continue...");
207
getchar(); // Wait for Enter key
208
while (getchar() != '\n'); // Ensure buffer is empty
209
}
210
211
// Function to start a study session option
212
void startStudyOption() {
213
if (loggedIn == -1 || users[loggedIn].sessionCount >= MAX_SESSIONS) {
214
printf("Error: No user logged in or maximum sessions reached.\n");
215
pressEnterToContinue();
216
return;
217
}
218
displayHeader("Study Session Options");
219
printf("1. Timer\n");
220
printf("2. Countdown\n");
221
printf("3. Pomodoro\n");
222
printf("4. Exit to User Menu\n");
223
printf("Enter your choice: ");
224
int choice;
225
scanf("%d", &choice);
226
getchar(); // Clear the newline character after the number input
227
228
switch (choice) {
229
case 1:
230
startTimer();
231
break;
232
case 2:
233
startCountdown();
234
break;
235
case 3:
236
pomodoro(loggedIn);
237
break;
238
case 4:
239
return; // Exit to user menu
240
default:
241
printf("Invalid choice. Please try again.\n");
242
pressEnterToContinue();
243
}
244
}
245
246
// Function to start a timer
247
// Function to start a timer
248
void startTimer() {
249
if (users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive) {
250
printf("Error: A session is already active. Please end the current session before starting a new one.\n");
251
pressEnterToContinue();
252
return;
253
}
254
displayHeader("Starting a New Study Session");
255
printf("Enter study category: ");
256
scanf("%49s", users[loggedIn].sessions[users[loggedIn].sessionCount].category);
257
getchar(); // Clear the newline character after the input
258
259
users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive = 1;
260
strcpy(users[loggedIn].sessions[users[loggedIn].sessionCount].sessionType, "Timer");
261
262
time_t start = time(NULL);
263
users[loggedIn].sessions[users[loggedIn].sessionCount].startTime = *localtime(&start);
264
int elapsedTime = 0;
265
char key;
266
267
displayHeader("Timer is on");
268
printf("Press 'Enter' to stop the timer, 'p' to pause, 'q' to quit...\n");
269
270
time_t current;
271
while (1) {
272
current = time(NULL);
273
int elapsed = elapsedTime + (int)difftime(current, start);
274
int hours = elapsed / 3600;
275
int minutes = (elapsed % 3600) / 60;
276
int seconds = elapsed % 60;
277
278
printf("\rElapsed time: %02d:%02d:%02d", hours, minutes, seconds);
279
fflush(stdout);
280
281
Sleep(1000);
282
283
if (_kbhit()) {
284
key = _getch();
285
if (key == '\r') { // Check for carriage return which is 'Enter' key
286
break;
287
} else if (key == 'p') {
288
printf("\nPaused. Press any key to resume, 'q' to quit...\n");
289
while (1) {
290
if (_kbhit()) {
291
key = _getch();
292
if (key == 'q') {
293
users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive = 0;
294
return;
295
}
296
break;
297
}
298
}
299
start = time(NULL); // Reset start time after resuming
300
elapsedTime = elapsed; // Store elapsed time
301
} else if (key == 'q') {
302
users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive = 0;
303
return;
304
}
305
}
306
}
307
308
time_t end = time(NULL);
309
users[loggedIn].sessions[users[loggedIn].sessionCount].endTime = *localtime(&end);
310
users[loggedIn].sessions[users[loggedIn].sessionCount].duration = (int)difftime(end, start) + elapsedTime;
311
users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive = 0;
312
users[loggedIn].sessionCount++;
313
314
printf("\nStudy session ended. Duration: %02d:%02d:%02d.\n",
315
users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration / 3600,
316
(users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration % 3600) / 60,
317
users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration % 60);
318
for (int i = 0; i < 3; i++)
319
{
320
Beep(750, 300);
321
}// Alert bell
322
pressEnterToContinue();
323
}
324
325
326
// Function to start a countdown
327
void startCountdown() {
328
if (users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive) {
329
printf("Error: A countdown is already active. Please wait for it to finish before starting a new one.\n");
330
pressEnterToContinue();
331
return;
332
}
333
displayHeader("Countdown:");
334
printf("Enter study time in HH MM SS format: ");
335
int hours, minutes, seconds;
336
scanf("%d %d %d", &hours, &minutes, &seconds);
337
getchar(); // Clear the newline character after the number input
338
int totalSeconds = hours * 3600 + minutes * 60 + seconds;
339
if (totalSeconds <= 0 || hours < 0 || minutes < 0 || minutes >= 60 || seconds < 0 || seconds >= 60) {
340
printf("Error: Invalid time.\n");
341
pressEnterToContinue();
342
return;
343
}
344
users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive = 1;
345
strcpy(users[loggedIn].sessions[users[loggedIn].sessionCount].sessionType, "Countdown");
346
time_t start = time(NULL);
347
users[loggedIn].sessions[users[loggedIn].sessionCount].startTime = *localtime(&start);
348
if (updateCountdown(totalSeconds)) {
349
time_t end = time(NULL);
350
users[loggedIn].sessions[users[loggedIn].sessionCount].endTime = *localtime(&end);
351
users[loggedIn].sessions[users[loggedIn].sessionCount].duration = totalSeconds;
352
users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive = 0;
353
users[loggedIn].sessionCount++;
354
printf("\nCountdown session ended. Duration: %02d:%02d:%02d.\n",
355
totalSeconds / 3600, (totalSeconds % 3600) / 60, totalSeconds % 60);
356
for (int i = 0; i < 3; i++) {
357
Beep(750, 300);
358
} // Alert bell
359
}
360
pressEnterToContinue();
361
}
362
363
// Function to update countdown
364
int updateCountdown(int totalSeconds) {
365
int remainingSeconds = totalSeconds;
366
displayHeader("Countdown:");
367
printf("Press 'p' to pause, 'q' to quit...\n");
368
int key;
369
while (remainingSeconds > 0) {
370
int hours = remainingSeconds / 3600;
371
int minutes = (remainingSeconds % 3600) / 60;
372
int seconds = remainingSeconds % 60;
373
374
printf("\rRemaining time: %02d:%02d:%02d", hours, minutes, seconds);
375
fflush(stdout);
376
377
Sleep(1000);
378
379
remainingSeconds--;
380
381
if (_kbhit()) {
382
key = getchar();
383
if (key == 'q') {
384
return 0;
385
} else if (key == 'p') {
386
printf("\nPaused. Press 'q' to quit or any other key to resume.\n");
387
while (1) {
388
if (_kbhit()) {
389
key = _getch();
390
if (key == 'q') {
391
return 0;
392
}
393
break;
394
}
395
}
396
}
397
}
398
}
399
return 1;
400
}
401
402
// Pomodoro timer
403
void pomodoro(int userIndex) {
404
displayHeader("Pomodoro Timer");
405
int studyMinutes, breakMinutes, sessions;
406
printf("Enter study duration (minutes): ");
407
scanf("%d", &studyMinutes);
408
printf("Enter break duration (minutes): ");
409
scanf("%d", &breakMinutes);
410
printf("Enter number of Pomodoro sessions: ");
411
scanf("%d", &sessions);
412
getchar(); // Clear the newline character after the number input
413
414
int studySeconds = studyMinutes * 60;
415
int breakSeconds = breakMinutes * 60;
416
for (int i = 0; i < sessions; i++) {
417
printf("Session %d\n", i + 1);
418
if (!updateCountdown(studySeconds)) {
419
return;
420
}
421
printf("Break %d\n", i + 1);
422
if (!updateCountdown(breakSeconds)) {
423
return;
424
}
425
}
426
printf("Pomodoro sessions completed.\n");
427
for (int i = 0; i < 3; i++) {
428
Beep(750, 300);
429
} // Alert bell
430
pressEnterToContinue();
431
}
432
433
// Function to show statistics
434
void showStatistics() {
435
displayHeader("Statistics");
436
int timerSessions = 0, countdownSessions = 0, pomodoroSessions = 0;
437
int totalTimerDuration = 0, totalCountdownDuration = 0, totalPomodoroDuration = 0;
438
for (int i = 0; i < users[loggedIn].sessionCount; i++) {
439
if (strcmp(users[loggedIn].sessions[i].sessionType, "Timer") == 0) {
440
timerSessions++;
441
totalTimerDuration += users[loggedIn].sessions[i].duration;
442
} else if (strcmp(users[loggedIn].sessions[i].sessionType, "Countdown") == 0) {
443
countdownSessions++;
444
totalCountdownDuration += users[loggedIn].sessions[i].duration;
445
} else if (strcmp(users[loggedIn].sessions[i].sessionType, "Pomodoro") == 0) {
446
pomodoroSessions++;
447
totalPomodoroDuration += users[loggedIn].sessions[i].duration;
448
}
449
}
450
printf("Total sessions: %d\n", users[loggedIn].sessionCount);
451
printf("Timer sessions: %d (Total duration: %02d:%02d:%02d)\n", timerSessions,
452
totalTimerDuration / 3600, (totalTimerDuration % 3600) / 60, totalTimerDuration % 60);
453
printf("Countdown sessions: %d (Total duration: %02d:%02d:%02d)\n", countdownSessions,
454
totalCountdownDuration / 3600, (totalCountdownDuration % 3600) / 60, totalCountdownDuration % 60);
455
printf("Pomodoro sessions: %d (Total duration: %02d:%02d:%02d)\n", pomodoroSessions,
456
totalPomodoroDuration / 3600, (totalPomodoroDuration % 3600) / 60, totalPomodoroDuration % 60);
457
pressEnterToContinue();
458
}
459
// Function to manage to-do list
460
void manageToDoList() {
461
displayHeader("To-Do List");
462
if (loggedIn == -1) {
463
printf("Error: No user logged in.\n");
464
pressEnterToContinue();
465
return;
466
}
467
printf("1. Add To-Do Item\n");
468
printf("2. Mark To-Do Item as Complete\n");
469
printf("3. Show To-Do List\n");
470
printf("4. Exit to User Menu\n");
471
printf("Enter your choice: ");
472
int choice;
473
scanf("%d", &choice);
474
getchar(); // Clear the newline character after the number input
475
476
switch (choice) {
477
case 1: {
478
if (users[loggedIn].todoCount >= MAX_TODO) {
479
printf("Error: Maximum to-do items reached.\n");
480
pressEnterToContinue();
481
return;
482
}
483
printf("Enter description: ");
484
fgets(users[loggedIn].todos[users[loggedIn].todoCount].description, 100, stdin);
485
users[loggedIn].todos[users[loggedIn].todoCount].completed = 0;
486
users[loggedIn].todoCount++;
487
printf("To-Do item added successfully.\n");
488
pressEnterToContinue();
489
break;
490
}
491
case 2: {
492
if (users[loggedIn].todoCount == 0) {
493
printf("Error: No to-do items found.\n");
494
pressEnterToContinue();
495
return;
496
}
497
for (int i = 0; i < users[loggedIn].todoCount; i++) {
498
printf("%d. %s [%s]\n", i + 1, users[loggedIn].todos[i].description, users[loggedIn].todos[i].completed ? "Completed" : "Incomplete");
499
}
500
printf("Enter item number to mark as complete: ");
501
int itemNumber;
502
scanf("%d", &itemNumber);
503
getchar(); // Clear the newline character after the number input
504
if (itemNumber < 1 || itemNumber > users[loggedIn].todoCount) {
505
printf("Error: Invalid item number.\n");
506
} else {
507
users[loggedIn].todos[itemNumber - 1].completed = 1;
508
printf("To-Do item marked as complete.\n");
509
}
510
pressEnterToContinue();
511
break;
512
}
513
case 3: {
514
if (users[loggedIn].todoCount == 0) {
515
printf("Error: No to-do items found.\n");
516
} else {
517
for (int i = 0; i < users[loggedIn].todoCount; i++) {
518
printf("%d. %s [%s]\n", i + 1, users[loggedIn].todos[i].description, users[loggedIn].todos[i].completed ? "Completed" : "Incomplete");
519
}
520
}
521
pressEnterToContinue();
522
break;
523
}
524
case 4:
525
return; // Exit to user menu
526
default:
527
printf("Invalid choice. Please try again.\n");
528
pressEnterToContinue();
529
}
530
}
531
532
// Main function
533
int main() {
534
while (1) {
535
if (loggedIn == -1) {
536
displayMainMenu();
537
int choice;
538
scanf("%d", &choice);
539
getchar(); // Clear the newline character after the number input
540
switch (choice) {
541
case 1:
542
createUser();
543
break;
544
case 2:
545
loginUser();
546
break;
547
case 3:
548
printf("Exiting the application.\n");
549
return 0;
550
default:
551
printf("Invalid choice. Please try again.\n");
552
pressEnterToContinue();
553
}
554
} else {
555
displayUserMenu();
556
int choice;
557
scanf("%d", &choice);
558
getchar(); // Clear the newline character after the number input
559
switch (choice) {
560
case 1:
561
startStudyOption();
562
break;
563
case 2:
564
showStatistics();
565
break;
566
case 3:
567
manageToDoList();
568
break;
569
case 4:
570
loggedIn = -1;
571
break;
572
default:
573
printf("Invalid choice. Please try again.\n");
574
pressEnterToContinue();
575
}
576
}
577
}
578
return 0;
579
}