Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
16 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define MAX_USERS 100
#define MAX_SESSIONS 100
#define MAX_TODO 100
#define MAX_POMODOROS 100

// Define structures for user accounts, study sessions, and to-do items
typedef struct {
    char description[100];
    int completed;
} ToDoItem;

typedef struct {
    char category[50];
    struct tm startTime;
    struct tm endTime;
    int duration; // in seconds
    int sessionActive; // Indicates if a session is currently active
    int countdownActive; // Indicates if a countdown is currently active
    int countdownDuration; // in seconds
    char sessionType[20]; // "Timer", "Countdown", "Pomodoro"
} StudySession;

typedef struct {
    int studyDuration; // in seconds
    int breakDuration; // in seconds
} PomodoroSession;

typedef struct {
    char username[50];
    int sessionCount;
    StudySession sessions[MAX_SESSIONS];
    ToDoItem todos[MAX_TODO];
    int todoCount;
    PomodoroSession pomodoros[MAX_POMODOROS];
    int pomodoroCount;
} User;

// Global array to store users
User users[MAX_USERS];
int userCount = 0;
int loggedIn = -1; // Index of logged-in user, -1 if no user is logged in

// Function prototypes
void clearScreen();
void displayHeader(const char* header);
void displayMainMenu();
void displayUserMenu();
void createUser();
void loginUser();
void startStudyOption();
void endSession();
void showStatistics();
void manageToDoList();
int findUserIndex(const char* username);
void pressEnterToContinue();
void startTimer();
void startCountdown();
void updateCountdown(int totalSeconds);
void pomodoroTimer();
void pausePomodoroTimer(int *remainingTime);

// Function to clear the screen
void clearScreen() {
    #ifdef _WIN32
        system("cls");
    #else
        system("clear");
    #endif
}

// Function to display a header
void displayHeader(const char* header) {
    clearScreen();
    printf("== %s ==\n\n", header);
}

// Function to display the main menu
void displayMainMenu() {
    system("color 3F");
    displayHeader("Main Menu");
    printf("1. Create new user\n");
    printf("2. Login\n");
    printf("3. Exit\n");
    printf("Enter your choice: ");
}

// Function to display the user menu
void displayUserMenu() {
    displayHeader("User Menu");
    printf("1. Start a study session\n");
    printf("2. Show statistics\n");
    printf("3. Manage to-do list\n");
    printf("4. Logout\n");
    printf("Enter your choice: ");
}

// Function to create a new user
void createUser() {
    displayHeader("Create New User");
    if (userCount >= MAX_USERS) {
        printf("Error: Maximum number of users reached.\n");
        pressEnterToContinue();
        return;
    }
    printf("Enter username: ");
    scanf("%49s", users[userCount].username);
    users[userCount].sessionCount = 0;
    users[userCount].todoCount = 0;
    users[userCount].pomodoroCount = 0;
    userCount++;
    printf("User created successfully.\n");
    pressEnterToContinue();
}

// Function to login a user
void loginUser() {
    displayHeader("Login");
    printf("Enter username: ");
    char username[50];
    scanf("%49s", username);
    int index = findUserIndex(username);
    if (index == -1) {
        printf("Error: User not found.\n");
        pressEnterToContinue();
    } else {
        loggedIn = index;
        printf("Login successful.\n");
        pressEnterToContinue();
    }
}

// Function to find a user index
int findUserIndex(const char* username) {
    for (int i = 0; i < userCount; i++) {
        if (strcmp(users[i].username, username) == 0) {
            return i;
        }
    }
    return -1;
}

// Function to press enter to continue
void pressEnterToContinue() {
    printf("\nPress Enter to continue...");
    getchar(); // Wait for Enter key
    while (getchar() != '\n'); // Ensure buffer is empty
}

// Function to start a study session option
void startStudyOption() {
    if (loggedIn == -1 || users[loggedIn].sessionCount >= MAX_SESSIONS) {
        printf("Error: No user logged in or maximum sessions reached.\n");
        pressEnterToContinue();
        return;
    }
    displayHeader("Study Session Options");
    printf("1. Timer\n");
    printf("2. Countdown\n");
    printf("3. Pomodoro Timer\n");
    printf("4. Exit to User Menu\n");
    printf("Enter your choice: ");
    int choice;
    scanf("%d", &choice);
    getchar(); // Clear the newline character after the number input

    switch (choice) {
        case 1:
            startTimer();
            break;
        case 2:
            startCountdown();
            break;
        case 3:
            pomodoroTimer();
            break;
        case 4:
            return; // Exit to user menu
        default:
            printf("Invalid choice. Please try again.\n");
            pressEnterToContinue();
    }
}

// Function to start a timer
void startTimer() {
    if (users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive) {
        printf("Error: A session is already active. Please end the current session before starting a new one.\n");
        pressEnterToContinue();
        return;
    }
    displayHeader("Starting a New Study Session");
    printf("Enter study category: ");
    scanf("%49s", users[loggedIn].sessions[users[loggedIn].sessionCount].category);
    getchar(); // Clear the newline character after the input
    strcpy(users[loggedIn].sessions[users[loggedIn].sessionCount].sessionType, "Timer");
    users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive = 1;
    time_t start = time(NULL);
    users[loggedIn].sessions[users[loggedIn].sessionCount].startTime = *localtime(&start);
    displayHeader("Timer is on");
    printf("Press 'Enter' to stop the timer...\n");
    while (getchar() != '\n'); // Wait for Enter key
    time_t end = time(NULL);
    users[loggedIn].sessions[users[loggedIn].sessionCount].endTime = *localtime(&end);
    users[loggedIn].sessions[users[loggedIn].sessionCount].duration = (int)difftime(end, start);
    users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive = 0;
    users[loggedIn].sessionCount++;
    printf("Study session ended. Duration: %02d:%02d:%02d.\n",
           users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration / 3600,
           (users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration % 3600) / 60,
           users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration % 60);
    pressEnterToContinue();
}

// Function to start a countdown
void startCountdown() {
    if (users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive) {
        printf("Error: A countdown is already active. Please wait for it to finish before starting a new one.\n");
        pressEnterToContinue();
        return;
    }
    displayHeader("Starting a New Countdown");
    printf("Enter countdown time in HH MM SS format: ");
    int hours, minutes, seconds;
    scanf("%d %d %d", &hours, &minutes, &seconds);
    getchar(); // Clear the newline character after the number input
    int totalSeconds = hours * 3600 + minutes * 60 + seconds;
    if (totalSeconds <= 0 || hours < 0 || minutes < 0 || minutes >= 60 || seconds < 0 || seconds >= 60) {
        printf("Error: Invalid countdown time.\n");
        pressEnterToContinue();
        return;
    }
    users[loggedIn].sessions[users[loggedIn].sessionCount].countdownDuration = totalSeconds;
    users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive = 1;
    strcpy(users[loggedIn].sessions[users[loggedIn].sessionCount].sessionType, "Countdown");
    updateCountdown(totalSeconds);
    users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive = 0;
    users[loggedIn].sessionCount++;
}

// Function to update countdown
void updateCountdown(int totalSeconds) {
    for (int remainingSeconds = totalSeconds; remainingSeconds >= 0; remainingSeconds--) {
        clearScreen();
        printf("Countdown Timer: %02d:%02d:%02d\n", remainingSeconds / 3600, (remainingSeconds % 3600) / 60, remainingSeconds % 60);
        if (remainingSeconds > 0) {
            sleep(1);
        }
    }
    printf("Countdown ended.\n");
    pressEnterToContinue();
}

// Function to start a Pomodoro timer
void pomodoroTimer() {
    if (users[loggedIn].pomodoroCount >= MAX_POMODOROS) {
        printf("Error: Maximum number of Pomodoro sessions reached.\n");
        pressEnterToContinue();
        return;
    }
    displayHeader("Starting a Pomodoro Timer");
    printf("Enter study duration in minutes: ");
    int studyMinutes;
    scanf("%d", &studyMinutes);
    getchar(); // Clear the newline character after the number input
    printf("Enter break duration in minutes: ");
    int breakMinutes;
    scanf("%d", &breakMinutes);
    getchar(); // Clear the newline character after the number input
    if (studyMinutes <= 0 || breakMinutes <= 0) {
        printf("Error: Invalid durations.\n");
        pressEnterToContinue();
        return;
    }
    int studySeconds = studyMinutes * 60;
    int breakSeconds = breakMinutes * 60;
    users[loggedIn].pomodoros[users[loggedIn].pomodoroCount].studyDuration = studySeconds;
    users[loggedIn].pomodoros[users[loggedIn].pomodoroCount].breakDuration = breakSeconds;
    users[loggedIn].pomodoroCount++;
    // Start study session
    printf("Pomodoro Study Session Started. Duration: %02d:%02d.\n", studyMinutes / 60, studyMinutes % 60);
    pausePomodoroTimer(&studySeconds);
    // Start break session
    printf("Pomodoro Break Session Started. Duration: %02d:%02d.\n", breakMinutes / 60, breakMinutes % 60);
    pausePomodoroTimer(&breakSeconds);
}

// Function to pause Pomodoro timer
void pausePomodoroTimer(int *remainingTime) {
    while (*remainingTime > 0) {
        clearScreen();
        printf("Remaining Time: %02d:%02d\n", *remainingTime / 60, *remainingTime % 60);
        sleep(1);
        (*remainingTime)--;
    }
    printf("Session Ended.\n");
    pressEnterToContinue();
}

// Function to end a study session
void endSession() {
    if (loggedIn == -1) {
        printf("Error: No user logged in.\n");
        pressEnterToContinue();
        return;
    }
    int sessionIndex = users[loggedIn].sessionCount - 1;
    if (sessionIndex < 0 || !users[loggedIn].sessions[sessionIndex].sessionActive) {
        printf("Error: No active session to end.\n");
        pressEnterToContinue();
        return;
    }
    time_t end = time(NULL);
    users[loggedIn].sessions[sessionIndex].endTime = *localtime(&end);
    users[loggedIn].sessions[sessionIndex].duration = (int)difftime(end, mktime(&users[loggedIn].sessions[sessionIndex].startTime));
    users[loggedIn].sessions[sessionIndex].sessionActive = 0;
    printf("Study session ended. Duration: %02d:%02d:%02d.\n",
           users[loggedIn].sessions[sessionIndex].duration / 3600,
           (users[loggedIn].sessions[sessionIndex].duration % 3600) / 60,
           users[loggedIn].sessions[sessionIndex].duration % 60);
    pressEnterToContinue();
}

// Function to show statistics
void showStatistics() {
    if (loggedIn == -1) {
        printf("Error: No user logged in.\n");
        pressEnterToContinue();
        return;
    }
    displayHeader("Statistics");
    printf("Total study sessions: %d\n", users[loggedIn].sessionCount);
    int totalDuration = 0;
    int timerCount = 0;
    int countdownCount = 0;
    int pomodoroCount = 0;
    for (int i = 0; i < users[loggedIn].sessionCount; i++) {
        totalDuration += users[loggedIn].sessions[i].duration;
        if (strcmp(users[loggedIn].sessions[i].sessionType, "Timer") == 0) {
            timerCount++;
        } else if (strcmp(users[loggedIn].sessions[i].sessionType, "Countdown") == 0) {
            countdownCount++;
        } else if (strcmp(users[loggedIn].sessions[i].sessionType, "Pomodoro") == 0) {
            pomodoroCount++;
        }
    }
    printf("Total study duration: %02d:%02d:%02d\n", totalDuration / 3600, (totalDuration % 3600) / 60, totalDuration % 60);
    printf("Timer sessions: %d\n", timerCount);
    printf("Countdown sessions: %d\n", countdownCount);
    printf("Pomodoro sessions: %d\n", pomodoroCount);
    pressEnterToContinue();
}

// Function to manage to-do list
void manageToDoList() {
    if (loggedIn == -1) {
        printf("Error: No user logged in.\n");
        pressEnterToContinue();
        return;
    }
    while (1) {
        displayHeader("To-Do List Management");
        printf("To-Do List:\n");
        for (int i = 0; i < users[loggedIn].todoCount; i++) {
            printf("%d. %s [%s]\n", i + 1, users[loggedIn].todos[i].description, users[loggedIn].todos[i].completed ? "Completed" : "Incomplete");
        }
        printf("\n1. Add To-Do\n");
        printf("2. Mark To-Do as Completed\n");
        printf("3. Delete To-Do\n");
        printf("4. Exit to User Menu\n");
        printf("Enter your choice: ");
        int choice;
        scanf("%d", &choice);
        getchar(); // Clear the newline character after the number input
        if (choice == 1) {
            if (users[loggedIn].todoCount >= MAX_TODO) {
                printf("Error: Maximum number of to-do items reached.\n");
                pressEnterToContinue();
                continue;
            }
            printf("Enter to-do description: ");
            fgets(users[loggedIn].todos[users[loggedIn].todoCount].description, sizeof(users[loggedIn].todos[users[loggedIn].todoCount].description), stdin);
            // Remove newline character at the end of the input string if present
            size_t len = strlen(users[loggedIn].todos[users[loggedIn].todoCount].description);
            if (len > 0 && users[loggedIn].todos[users[loggedIn].todoCount].description[len - 1] == '\n') {
                users[loggedIn].todos[users[loggedIn].todoCount].description[len - 1] = '\0';
            }
            users[loggedIn].todos[users[loggedIn].todoCount].completed = 0;
            users[loggedIn].todoCount++;
            printf("To-do item added.\n");
            pressEnterToContinue();
        } else if (choice == 2) {
            if (users[loggedIn].todoCount == 0) {
                printf("Error: No to-do items to mark as completed.\n");
                pressEnterToContinue();
                continue;
            }
            printf("Enter the number of the to-do item to mark as completed: ");
            int index;
            scanf("%d", &index);
            getchar(); // Clear the newline character after the number input
            if (index < 1 || index > users[loggedIn].todoCount) {
                printf("Error: Invalid to-do item number.\n");
                pressEnterToContinue();
                continue;
            }
            users[loggedIn].todos[index - 1].completed = 1;
            printf("To-do item marked as completed.\n");
            pressEnterToContinue();
        } else if (choice == 3) {
            if (users[loggedIn].todoCount == 0) {
                printf("Error: No to-do items to delete.\n");
                pressEnterToContinue();
                continue;
            }
            printf("Enter the number of the to-do item to delete: ");
            int index;
            scanf("%d", &index);
            getchar(); // Clear the newline character after the number input
            if (index < 1 || index > users[loggedIn].todoCount) {
                printf("Error: Invalid to-do item number.\n");
                pressEnterToContinue();
                continue;
            }
            // Shift the to-do items after the deleted item to fill the gap
            for (int i = index; i < users[loggedIn].todoCount; i++) {
                users[loggedIn].todos[i - 1] = users[loggedIn].todos[i];
            }
            users[loggedIn].todoCount--;
            printf("To-do item deleted.\n");
            pressEnterToContinue();
        } else if (choice == 4) {
            return; // Exit to user menu
        } else {
            printf("Invalid choice. Please try again.\n");
            pressEnterToContinue();
        }
    }
}
Leave a Comment