This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
forked from iamiranjbar/Hotel-Transylvania
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHotelTransylvania.cpp
283 lines (255 loc) · 7.85 KB
/
HotelTransylvania.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#define GUESTFILE "guest.txt"
#define ROOMFILE "rooms.txt"
#define serviceRate 10000
using namespace std;
struct Guest{
string name;
string number;
};
struct Room{
int id;
string type;
int capacity;
int rate;
bool reserved;
};
struct Reserve{
Guest guest;
Room room;
int days;
int serviceNum;
};
void openFile(string fileName , ifstream &fileData);
void readGuestsData(ifstream &fileData , vector <Guest> &guests);
void readGuestDetails(string line , Guest &guest);
void readRoomsData(ifstream &fileData , vector <Room> &rooms);
void readRoomDetails(string line , Room &room);
vector<string> getCommand();
void runCommand(vector<string> commandParts, vector<Guest> &guests, vector<Room> &rooms,
vector<Reserve> &reserves);
vector<Room> getStatus(vector<Room> rooms, string type, string value);
vector<Room> getStatusByID(vector<Room> rooms, string id);
vector<Room> getStatusByType(vector<Room> rooms, string type);
void showRooms(vector<Room> rooms);
void reserve(vector<Reserve> &reserves,vector<Guest> guests,vector<Room> &rooms,
string id, string guestName, string days);
void service(vector<Reserve> &reserves, string roomID, string guestName);
void checkout(vector<Reserve> &reserves, string roomID, string guestName);
Reserve findReserve(vector<Reserve> &reserves, string guestName, int roomID, bool del);
Guest findGuestByName(vector<Guest> guests, string name);
Room findRoomByID(vector<Room> &rooms, int id, bool reserving);
vector<string> splitBySpace(string statement);
int main(){
vector<Reserve> reserves;
vector <Guest> guests;
ifstream guestsFile;
openFile(GUESTFILE , guestsFile);
if(!guestsFile){
return 0;
}
readGuestsData(guestsFile , guests);
vector <Room> rooms;
ifstream roomsFile;
openFile(ROOMFILE , roomsFile);
if(!roomsFile){
return 0;
}
readRoomsData(roomsFile , rooms);
roomsFile.close();
while (cin != NULL){
vector <string> commandParts = getCommand();
runCommand(commandParts , guests , rooms, reserves);
commandParts.clear();
}
return 0;
}
void openFile(string fileName , ifstream &fileData){
fileData.open(fileName.c_str());
if (!fileData){
cerr << "can't open file [" << fileName << "]\n";
}
}
void readGuestsData(ifstream &fileData , vector <Guest> &guests){
string line;
Guest guest;
while (fileData.tellg()!=EOF){
getline(fileData, line);
readGuestDetails(line , guest);
guests.push_back(guest);
}
}
void readGuestDetails(string line , Guest &guest){
vector<string> details = splitBySpace(line);
guest.name = details[0];
guest.number = details[1];
}
void readRoomsData(ifstream &fileData , vector <Room> &rooms){
string line;
Room room;
while (fileData.tellg()!=EOF){
getline(fileData, line);
readRoomDetails(line , room);
rooms.push_back(room);
}
}
void readRoomDetails(string line , Room &room){
vector<string> details = splitBySpace(line);
room.id = stoi(details[0]);
room.type = details[1];
room.capacity = stoi(details[2]);
room.rate = stoi(details[3]);
room.reserved = false;
}
vector<string> getCommand(){
string line;
getline (cin , line);
vector<string> commandParts = splitBySpace(line);
return commandParts;
}
void runCommand(vector<string> commandParts,vector<Guest> &guests,vector<Room> &rooms,
vector<Reserve> &reserves){
if(commandParts[0] == "status"){
vector<Room> roomsToShow = getStatus(rooms, commandParts[1], commandParts[2]);
showRooms(roomsToShow);
}
else if(commandParts[0] == "listAllRooms")
showRooms(rooms);
else if(commandParts[0] == "reserve")
reserve(reserves, guests, rooms, commandParts[1], commandParts[2], commandParts[3]);
else if(commandParts[0] == "service")
service(reserves, commandParts[1],commandParts[2]);
else if(commandParts[0] == "checkout")
checkout(reserves, commandParts[1],commandParts[2]);
else
cout << "Invalid command!" << endl;
}
vector<Room> getStatus(vector<Room> rooms, string type, string value){
vector <Room> roomsToShow;
if(type == "id")
roomsToShow = getStatusByID(rooms, value);
else if(type == "type")
roomsToShow = getStatusByType(rooms, value);
else
cout << "Command is invalid! Type should be id or type." << endl;
return roomsToShow;
}
vector<Room> getStatusByID(vector<Room> rooms, string id){
vector<Room> roomsToShow;
roomsToShow.push_back(findRoomByID(rooms, stoi(id), false));
return roomsToShow;
}
vector<Room> getStatusByType(vector<Room> rooms, string type){
vector<Room> roomsToShow;
for (uint i = 0; i < rooms.size(); i++){
if (rooms[i].type == type)
roomsToShow.push_back(rooms[i]);
}
return roomsToShow;
}
void showRooms(vector<Room> rooms){
if (rooms.size() != 0){
cout << "Room id\tRoom type\tRoom capacity\tRoom rate\tRoom status" << endl;
cout << "_________________________________________________________" << endl;
for (uint i = 0; i < rooms.size(); i++){
cout << rooms[i].id << "\t" << rooms[i].type << "\t" << rooms[i].capacity << "\t" <<
rooms[i].rate << "\t";
if (rooms[i].reserved == false)
cout << "empty :)" << endl;
else
cout << "reserved :(" << endl;
}
}else
cout<< "No rooms with this details found!" << endl;
}
void reserve(vector<Reserve> &reserves,vector<Guest> guests,vector<Room> &rooms,
string id, string guestName, string days){
Reserve newReserve;
Guest guest = findGuestByName(guests, guestName);
if (guest.number == "-1")
return;
Room room = findRoomByID(rooms, stoi(id) , true);
if (room.capacity == -1)
return;
if (room.reserved == false){
newReserve.guest = guest;
newReserve.room = room;
newReserve.days = stoi(days);
newReserve.serviceNum = 0;
reserves.push_back(newReserve);
}else
cout << "Room already is reserved." << endl;
}
void service(vector<Reserve> &reserves, string roomID, string guestName){
for (uint i = 0; i < reserves.size(); i++){
if (guestName == reserves[i].guest.name && stoi(roomID) == reserves[i].room.id)
reserves[i].serviceNum += 1;
}
}
void checkout(vector<Reserve> &reserves, string roomID, string guestName){
Reserve reserve = findReserve(reserves, guestName, stoi(roomID) , true);
if (reserve.days == -1)
return;
cout << guestName << " " << roomID << " " << reserve.days << endl;
int roomPrice = reserve.days * reserve.room.rate;
cout << "Room price:" << reserve.days << " * " << reserve.room.rate << " = " << roomPrice << endl;
int servicePrice = reserve.serviceNum * serviceRate;
cout << "Service prive:" << reserve.serviceNum << " * " << serviceRate << " = " << servicePrice << endl;
int priceToPay = roomPrice + servicePrice;
cout << "Price To pay = " << priceToPay << endl;
}
Reserve findReserve(vector<Reserve> &reserves, string guestName, int roomID, bool del){
for (uint i = 0; i < reserves.size(); i++){
if (guestName == reserves[i].guest.name && roomID == reserves[i].room.id){
Reserve temp = reserves[i];
if (del == true)
reserves.erase(reserves.begin() + i);
return temp;
}
}
cout << "reserve not found!" << endl;
Reserve dummy;
dummy.days = -1;
return dummy;
}
Guest findGuestByName(vector<Guest> guests, string name){
for (uint i = 0; i < guests.size(); i++){
if (name == guests[i].name)
return guests[i];
}
cout << "Guest not found!" << endl;
Guest dummy;
dummy.number = "-1";
return dummy;
}
Room findRoomByID(vector<Room> &rooms, int id, bool reserving){
for (uint i = 0; i < rooms.size(); i++){
if (rooms[i].id == id){
Room temp = rooms[i];
if (reserving == true)
rooms[i].reserved = true;
return temp;
}
}
Room dummy;
dummy.capacity = -1;
return dummy;
}
vector<string> splitBySpace(string statement){
vector<string> result;
string temp = "";
string token;
for(uint i=0; i<statement.length(); i++)
if(statement[i] != ' '){
token += statement[i];
}
else if(token.length()) {
result.push_back(token);
token = "";
}
result.push_back(token);
return result;
}