fperson.reservation

 avatar
user_6919294
c_cpp
20 days ago
10 kB
1
Indexable
/* CODE BY FPERSON
 
 A comprehensive conference room reservation management system. 
 
 Main functionalities:
 
 - Managing multiple conference rooms
 - Checking for time conflicts
 - Adding new reservations
 - Displaying schedule for each room
 
 System includes the following constraints and features:
 - Reservations available between 8-20 hours
 - Automatic sorting of reservations by time
 - Conflict checking before adding new reservations
 - Clear schedule display
 
 ------------------------------------------------------------
 
 Added new functionalities:
 
 Reservation removal:
 - removeReservation method allows removing a reservation based on client name, date, and start time
 - System confirms deletion or notifies if reservation not found
 
 Finding available time slots:
 - findAvailableSlots method finds all available time slots for a given day
 - Shows available times for each room separately
 - Takes into account working hours (8-20)
 
 Notification system:
 - checkUpcomingReservations method checks for upcoming reservations
 - Sends notification one hour before reservation starts
 - Notification time can be easily modified
 
 Additional improvements:
 - Date handling in reservations
 - Better reservation sorting (first by date, then by time)
 - Extended schedule display with dates
 - More detailed user messages
*/

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iomanip>
#include <ctime>
#include <chrono>

using namespace std;

// Structure storing reservation information
struct Reservation {
    string clientName;
    int startTime;
    int endTime;
    string purpose;
    time_t date;  // Reservation date
};

// Structure representing an available time slot
struct TimeSlot {
    int startTime;
    int endTime;
};

// Class representing a conference room
class ConferenceRoom {
private:
    string name;
    int capacity;
    vector<Reservation> reservations;

    // Helper method for date to string conversion
    string dateToString(time_t date) const {
        char buffer[26];
        strftime(buffer, 26, "%Y-%m-%d", localtime(&date));
        return string(buffer);
    }

public:
    ConferenceRoom(string n, int c) : name(n), capacity(c) {}

    // Checks for conflicts with existing reservations
    bool hasConflict(int start, int end, time_t date) const {
        for (const auto& res : reservations) {
            if (res.date == date) {
                if ((start >= res.startTime && start < res.endTime) ||
                    (end > res.startTime && end <= res.endTime) ||
                    (start <= res.startTime && end >= res.endTime)) {
                    return true;
                }
            }
        }
        return false;
    }

    // Adds a new reservation
    bool addReservation(const string& client, int start, int end, 
                       const string& purpose, time_t date) {
        if (start >= end || start < 8 || end > 20) {
            cout << "Error: Invalid reservation hours (available 8-20)" << endl;
            return false;
        }

        if (hasConflict(start, end, date)) {
            cout << "Error: Conflict with existing reservation" << endl;
            return false;
        }

        Reservation newReservation{client, start, end, purpose, date};
        reservations.push_back(newReservation);
        sort(reservations.begin(), reservations.end(), 
            [](const Reservation& a, const Reservation& b) {
                return (a.date < b.date) || 
                       (a.date == b.date && a.startTime < b.startTime);
            });
        return true;
    }

    // Removes a reservation
    bool removeReservation(const string& client, time_t date, int startTime) {
        auto it = find_if(reservations.begin(), reservations.end(),
            [&](const Reservation& res) {
                return res.clientName == client && 
                       res.date == date && 
                       res.startTime == startTime;
            });

        if (it != reservations.end()) {
            reservations.erase(it);
            cout << "Reservation has been removed" << endl;
            return true;
        }
        cout << "Reservation not found" << endl;
        return false;
    }

    // Finds available time slots for a given day
    vector<TimeSlot> findAvailableSlots(time_t date) const {
        vector<TimeSlot> availableSlots;
        vector<int> busyTimes;
        
        // Add all occupied hours
        for (const auto& res : reservations) {
            if (res.date == date) {
                for (int i = res.startTime; i < res.endTime; i++) {
                    busyTimes.push_back(i);
                }
            }
        }

        // Find available slots
        int currentTime = 8;
        while (currentTime < 20) {
            if (find(busyTimes.begin(), busyTimes.end(), currentTime) == busyTimes.end()) {
                int startSlot = currentTime;
                while (currentTime < 20 && 
                       find(busyTimes.begin(), busyTimes.end(), currentTime) == busyTimes.end()) {
                    currentTime++;
                }
                availableSlots.push_back({startSlot, currentTime});
            } else {
                currentTime++;
            }
        }

        return availableSlots;
    }

    // Checks for upcoming reservations and sends notifications
    void checkUpcomingReservations() const {
        time_t now = time(nullptr);
        for (const auto& res : reservations) {
            // Check today's reservations
            if (res.date == now) {
                time_t currentHour = localtime(&now)->tm_hour;
                if (res.startTime - currentHour <= 1 && res.startTime - currentHour > 0) {
                    cout << "NOTIFICATION: Upcoming reservation for " << res.clientName 
                         << " in room " << name << " at " << res.startTime << ":00" << endl;
                }
            }
        }
    }

    // Displays reservation schedule
    void displaySchedule() const {
        cout << "\nSchedule for room: " << name << " (Capacity: " << capacity << " people)" << endl;
        cout << string(70, '-') << endl;
        cout << setw(15) << "Date" << setw(15) << "Hours" << setw(15) 
             << "Client" << setw(25) << "Purpose" << endl;
        cout << string(70, '-') << endl;

        for (const auto& res : reservations) {
            cout << setw(15) << dateToString(res.date)
                 << setw(5) << res.startTime << "-" << setw(8) << res.endTime 
                 << setw(15) << res.clientName << setw(25) << res.purpose << endl;
        }
        cout << string(70, '-') << endl;
    }

    string getName() const { return name; }
};

// Class managing all rooms
class ReservationSystem {
private:
    vector<ConferenceRoom> rooms;

public:
    // Adds a new room to the system
    void addRoom(const string& name, int capacity) {
        rooms.emplace_back(name, capacity);
    }

    // Adds a reservation to selected room
    bool addReservation(const string& roomName, const string& client, 
                       int start, int end, const string& purpose, time_t date) {
        for (auto& room : rooms) {
            if (room.getName() == roomName) {
                return room.addReservation(client, start, end, purpose, date);
            }
        }
        cout << "Error: Room " << roomName << " not found" << endl;
        return false;
    }

    // Removes a reservation from selected room
    bool removeReservation(const string& roomName, const string& client, 
                          time_t date, int startTime) {
        for (auto& room : rooms) {
            if (room.getName() == roomName) {
                return room.removeReservation(client, date, startTime);
            }
        }
        cout << "Error: Room " << roomName << " not found" << endl;
        return false;
    }

    // Finds available slots in all rooms
    void findAvailableSlots(time_t date) const {
        cout << "\nAvailable slots for " 
             << put_time(localtime(&date), "%Y-%m-%d") << ":" << endl;
        
        for (const auto& room : rooms) {
            cout << "\nRoom " << room.getName() << ":" << endl;
            auto slots = room.findAvailableSlots(date);
            if (slots.empty()) {
                cout << "No available slots" << endl;
            } else {
                for (const auto& slot : slots) {
                    cout << slot.startTime << ":00 - " << slot.endTime << ":00" << endl;
                }
            }
        }
    }

    // Checks all upcoming reservations
    void checkAllUpcomingReservations() const {
        for (const auto& room : rooms) {
            room.checkUpcomingReservations();
        }
    }

    // Displays schedule for all rooms
    void displayAllSchedules() const {
        for (const auto& room : rooms) {
            room.displaySchedule();
            cout << endl;
        }
    }
};

int main() {
    ReservationSystem system;
    
    // Add example rooms
    system.addRoom("Room A", 10);
    system.addRoom("Room B", 20);
    system.addRoom("Room C", 30);

    // Set example dates
    time_t now = time(nullptr);
    time_t tomorrow = now + 24*60*60;
    
    // Example reservations
    system.addReservation("Room A", "John Smith", 9, 11, "Team meeting", now);
    system.addReservation("Room A", "Anna Wilson", 14, 16, "Training", now);
    system.addReservation("Room B", "Peter Brown", 10, 12, "Presentation", now);
    
    // Display schedule
    system.displayAllSchedules();
    
    // Find available slots for today
    system.findAvailableSlots(now);
    
    // Remove a reservation
    system.removeReservation("Room A", "John Smith", now, 9);
    
    // Check upcoming reservations
    system.checkAllUpcomingReservations();
    
    return 0;
}
Editor is loading...
Leave a Comment