-
Notifications
You must be signed in to change notification settings - Fork 0
/
booking.cpp
158 lines (142 loc) · 5.45 KB
/
booking.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "Booking.h"
#include "Utilities.h"
#include "Hotel.h"
#include <iostream>
#include <vector>
#include <algorithm>
void addBooking(Hotel &hotel, const std::string &customerName, int roomID, const std::string &checkInDate, const std::string &checkOutDate, int guests)
{
std::vector<Booking> bookings = Utilities::readBookingsCSV("bookings.csv");
if (!hotel.isRoomAvailable(roomID))
{
std::cerr << "Room ID " << roomID << " is not available for rental.\n";
return;
}
// to find the last ID used in csv
int maxID = 0;
for (const auto &booking : bookings)
{
if (booking.bookingID > maxID)
{
maxID = booking.bookingID;
}
}
int newBookingID = maxID + 1; // to create a new ID
int days = hotel.dateDifference(checkInDate, checkOutDate);
if (days < 1 || days > 10)
{
std::cerr << "Invalid booking duration: " << days << " days. Must be between 1 and 10 days.\n";
return;
}
double price = hotel.getRoomPrice(roomID);
if (price == 0)
{
std::cerr << "Room not found with ID: " << roomID << "\n";
return;
}
// check for overlapping bookings
for (const auto &booking : bookings)
{
if (booking.roomID == roomID && booking.status != "cancelled")
{
// calculate the number of days since the reference date for existing booking dates
int existingCheckIn = hotel.dateDifference("2024-01-01", booking.checkInDate);
int existingCheckOut = hotel.dateDifference("2024-01-01", booking.checkOutDate);
// calculate the number of days since the reference date for new booking dates
int newCheckIn = hotel.dateDifference("2024-01-01", checkInDate);
int newCheckOut = hotel.dateDifference("2024-01-01", checkOutDate);
// check for overlap
if ((newCheckIn >= existingCheckIn && newCheckIn < existingCheckOut) ||
(newCheckOut > existingCheckIn && newCheckOut <= existingCheckOut) ||
(newCheckIn <= existingCheckIn && newCheckOut >= existingCheckOut))
{
std::cerr << "Room ID " << roomID << " is already booked for the selected period.\n";
return;
}
}
}
double totalCost = price * days;
Booking newBooking(newBookingID, customerName, roomID, checkInDate, checkOutDate, guests, totalCost, "confirmed");
bookings.push_back(newBooking);
Utilities::writeBookingsCSV("bookings.csv", bookings);
std::cout << "Booking added successfully. Total cost: $" << totalCost << "\n";
}
void cancelBooking(const std::string &loggedInUser, int bookingID)
{
std::vector<Booking> bookings = Utilities::readBookingsCSV("bookings.csv");
auto it = std::find_if(bookings.begin(), bookings.end(), [bookingID](const Booking &b) {
return b.bookingID == bookingID;
});
if (it != bookings.end())
{
if (it->customerName == loggedInUser)
{
char confirmation;
std::cout << "Are you sure you want to cancel the booking with ID " << bookingID << "? This operation is irreversible. (y/n): ";
std::cin >> confirmation;
if (confirmation == 'y' || confirmation == 'Y')
{
it->status = "cancelled";
Utilities::writeBookingsCSV("bookings.csv", bookings);
std::cout << "Booking cancelled successfully. We hope you'll be back!\n";
}
else
{
std::cout << "Cancellation aborted.\n";
}
}
else
{
std::cout << "You can only cancel your own bookings.\n";
}
}
else
{
std::cout << "Booking ID not found.\n";
}
}
void displayBookingDetails(int bookingID, const std::string &loggedInUser, bool isAdmin)
{
std::vector<Booking> bookings = Utilities::readBookingsCSV("bookings.csv");
auto it = std::find_if(bookings.begin(), bookings.end(), [bookingID](const Booking &b)
{ return b.bookingID == bookingID; });
if (it != bookings.end())
{
if (isAdmin || it->customerName == loggedInUser)
{
std::cout << "Booking ID: " << it->bookingID << "\n"
<< "Customer Name: " << it->customerName << "\n"
<< "Room ID: " << it->roomID << "\n"
<< "Check-In Date: " << it->checkInDate << "\n"
<< "Check-Out Date: " << it->checkOutDate << "\n"
<< "Guests: " << it->guests << "\n"
<< "Total Cost: $" << it->totalCost << "\n"
<< "Status: " << it->status << "\n";
}
else
{
std::cout << "Access denied. You can only view your own bookings.\n";
}
}
else
{
std::cout << "No booking found with ID: " << bookingID << "\n";
}
}
// for users
void displayUserBookings(const std::string &customerName, bool isAdmin)
{
std::vector<Booking> bookings = Utilities::readBookingsCSV("bookings.csv");
std::cout << "Booking Details:\n";
std::cout << "Booking ID | Customer Name | Room ID | Check-In Date | Check-Out Date | Guests | Total Cost | Status\n";
std::cout << "------------------------------------------------------------------------------------------------------\n";
for (const auto &booking : bookings)
{
if (isAdmin || booking.customerName == customerName)
{
std::cout << booking.bookingID << " | " << booking.customerName << " | " << booking.roomID << " | "
<< booking.checkInDate << " | " << booking.checkOutDate << " | " << booking.guests << " | $"
<< booking.totalCost << " | " << booking.status << "\n";
}
}
}