reservation

 avatar
user_6919294
c_cpp
19 days ago
11 kB
10
Indexable
/* 

 CODE BY IRENEUSZ A.
 
 Stworzymy kompleksowy system zarządzania rezerwacjami sal konferencyjnych. 
 
 Główne funkcjonalności:
 - Zarządzanie wieloma salami konferencyjnymi
 - Sprawdzanie konfliktów czasowych
 - Dodawanie nowych rezerwacji
 - Wyświetlanie harmonogramu dla każdej sali
 
 System zawiera następujące ograniczenia i funkcje:
 - Rezerwacje możliwe w godzinach 8-20
 - Automatyczne sortowanie rezerwacji według czasu
 - Sprawdzanie konfliktów przed dodaniem nowej rezerwacji
 - Czytelne wyświetlanie harmonogramu
 ------------------------------------------------------------
 Dodałem następujące nowe funkcjonalności:
 
 Usuwanie rezerwacji:
 - Metoda removeReservation pozwala usunąć rezerwację na podstawie nazwy klienta, daty i godziny rozpoczęcia
 - System potwierdza usunięcie lub informuje o braku takiej rezerwacji
 
 Wyszukiwanie wolnych terminów:
 - Metoda findAvailableSlots znajduje wszystkie wolne przedziały czasowe w danym dniu
 - Pokazuje dostępne terminy dla każdej sali osobno
 - Uwzględnia godziny pracy (8-20)
 
 System powiadomień:
 - Metoda checkUpcomingReservations sprawdza nadchodzące rezerwacje
 - Wysyła powiadomienie na godzinę przed rozpoczęciem rezerwacji
 - Można łatwo zmodyfikować czas wyprzedzenia powiadomień
 
 Dodatkowo wprowadziłem:
 - Obsługę dat w rezerwacjach
 - Lepsze sortowanie rezerwacji (najpierw po dacie, potem po godzinie)
 - Rozszerzone wyświetlanie harmonogramu z datami
 - Więcej informacji w komunikatach dla użytkownika
 
*/

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

using namespace std;

// Struktura przechowująca informacje o rezerwacji
struct Reservation {
    string clientName;
    int startTime;
    int endTime;
    string purpose;
    time_t date;  // Data rezerwacji
};

// Struktura reprezentująca wolny przedział czasowy
struct TimeSlot {
    int startTime;
    int endTime;
};

// Klasa reprezentująca salę konferencyjną
class ConferenceRoom {
private:
    string name;
    int capacity;
    vector<Reservation> reservations;

    // Pomocnicza metoda do konwersji daty na string
    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) {}

    // Sprawdza czy występuje konflikt z istniejącymi rezerwacjami
    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;
    }

    // Dodaje nową rezerwację
    bool addReservation(const string& client, int start, int end, 
                       const string& purpose, time_t date) {
        if (start >= end || start < 8 || end > 20) {
            cout << "Błąd: Nieprawidłowe godziny rezerwacji (dostępne 8-20)" << endl;
            return false;
        }

        if (hasConflict(start, end, date)) {
            cout << "Błąd: Konflikt z istniejącą rezerwacją" << 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;
    }

    // Usuwa rezerwację
    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 << "Rezerwacja została usunięta" << endl;
            return true;
        }
        cout << "Nie znaleziono takiej rezerwacji" << endl;
        return false;
    }

    // Znajduje wolne terminy w danym dniu
    vector<TimeSlot> findAvailableSlots(time_t date) const {
        vector<TimeSlot> availableSlots;
        vector<int> busyTimes;
        
        // Dodaj wszystkie zajęte godziny
        for (const auto& res : reservations) {
            if (res.date == date) {
                for (int i = res.startTime; i < res.endTime; i++) {
                    busyTimes.push_back(i);
                }
            }
        }

        // Znajdź wolne przedziały
        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;
    }

    // Sprawdza nadchodzące rezerwacje i wysyła powiadomienia
    void checkUpcomingReservations() const {
        time_t now = time(nullptr);
        for (const auto& res : reservations) {
            // Sprawdź rezerwacje na dzisiaj
            if (res.date == now) {
                time_t currentHour = localtime(&now)->tm_hour;
                if (res.startTime - currentHour <= 1 && res.startTime - currentHour > 0) {
                    cout << "POWIADOMIENIE: Zbliżająca się rezerwacja dla " << res.clientName 
                         << " w sali " << name << " o godzinie " << res.startTime << ":00" << endl;
                }
            }
        }
    }

    // Wyświetla harmonogram rezerwacji
    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; }
};

// Klasa zarządzająca wszystkimi salami
class ReservationSystem {
private:
    vector<ConferenceRoom> rooms;

public:
    // Dodaje nową salę do systemu
    void addRoom(const string& name, int capacity) {
        rooms.emplace_back(name, capacity);
    }

    // Dodaje rezerwację do wybranej sali
    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 << "Błąd: Nie znaleziono sali o nazwie " << roomName << endl;
        return false;
    }

    // Usuwa rezerwację z wybranej sali
    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 << "Błąd: Nie znaleziono sali o nazwie " << roomName << endl;
        return false;
    }

    // Znajduje wolne terminy we wszystkich salach
    void findAvailableSlots(time_t date) const {
        cout << "\nDostępne terminy na dzień " 
             << put_time(localtime(&date), "%Y-%m-%d") << ":" << endl;
        
        for (const auto& room : rooms) {
            cout << "\nSala " << room.getName() << ":" << endl;
            auto slots = room.findAvailableSlots(date);
            if (slots.empty()) {
                cout << "Brak wolnych terminów" << endl;
            } else {
                for (const auto& slot : slots) {
                    cout << slot.startTime << ":00 - " << slot.endTime << ":00" << endl;
                }
            }
        }
    }

    // Sprawdza wszystkie nadchodzące rezerwacje
    void checkAllUpcomingReservations() const {
        for (const auto& room : rooms) {
            room.checkUpcomingReservations();
        }
    }

    // Wyświetla harmonogram wszystkich sal
    void displayAllSchedules() const {
        for (const auto& room : rooms) {
            room.displaySchedule();
            cout << endl;
        }
    }
};

// przyklad użycia
int main() {
    ReservationSystem system;
    
    // Dodajemy przykładowe sale
    system.addRoom("Room A", 10);
    system.addRoom("Room B", 20);
    system.addRoom("Room C", 30);

    // Ustawiamy przykładowe daty
    time_t now = time(nullptr);
    time_t tomorrow = now + 24*60*60;
    
    // Przykładowe rezerwacje
    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);
    
    // Wyświetlenie harmonogramu
    system.displayAllSchedules();
    
    // Znajdź wolne terminy na dziś
    system.findAvailableSlots(now);
    
    // Usuń rezerwację
    system.removeReservation("Room A", "John Smith", now, 9);
    
    // Sprawdź nadchodzące rezerwacje
    system.checkAllUpcomingReservations();
    
    return 0;
}
Leave a Comment