Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
5
Indexable
Never
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

struct Booking {
    string username;
    string startTime;
    string endTime;
};

bool checkOverlap(const vector<Booking>& bookings, const string& startTime, const string& endTime) {
    for (const Booking& booking : bookings) {
        if ((startTime >= booking.startTime && startTime < booking.endTime) ||
            (endTime > booking.startTime && endTime <= booking.endTime) ||
            (startTime <= booking.startTime && endTime >= booking.endTime)) {
            return true;
        }
    }
    return false;
}

int main() {
    map<string, string> users;
    vector<Booking> bookings;
    string username, password, choice, startTime, endTime;
    bool isRunning = true;
    
    while (isRunning) {
        cout << "Welcome to the Hall Registration and Login System!" << endl;
        cout << "1. Register" << endl;
        cout << "2. Login" << endl;
        cout << "3. Book Hall" << endl;
        cout << "4. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
        
        if (choice == "1") {
            cout << "Enter your username: ";
            cin >> username;
            cout << "Enter your password: ";
            cin >> password;
            
            // add user to map
            users[username] = password;
            cout << "Registration successful!" << endl;
        } else if (choice == "2") {
            cout << "Enter your username: ";
            cin >> username;
            cout << "Enter your password: ";
            cin >> password;
            
            // check if user exists and password is correct
            if (users.find(username) != users.end() && users[username] == password) {
                cout << "Login successful!" << endl;
            } else {
                cout << "Invalid username or password. Please try again." << endl;
            }
        } else if (choice == "3") {
            cout << "Enter start time (hh:mm): ";
            cin >> startTime;
            cout << "Enter end time (hh:mm): ";
            cin >> endTime;
            
            // validate booking time
            if (startTime >= endTime) {
                cout << "Invalid booking time. End time must be after start time." << endl;
            } else if (checkOverlap(bookings, startTime, endTime)) {
                cout << "This time slot is already booked. Please choose a different time." << endl;
            } else {
                Booking booking = {username, startTime, endTime};
                bookings.push_back(booking);
                cout << "Booking successful!" << endl;
            }
        } else if (choice == "4") {
            isRunning = false;
        } else {
            cout << "Invalid choice. Please enter a valid choice." << endl;
        }
    }
    
    cout << "Thank you for using the Hall Registration and Login System!" << endl;
    
    return 0;
}