-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvailableRooms.cpp
More file actions
295 lines (229 loc) · 8.15 KB
/
Copy pathAvailableRooms.cpp
File metadata and controls
295 lines (229 loc) · 8.15 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
//
// Created by Seo on 11/8/2025.
//
#include "AvailableRooms.h"
#include <filesystem>
// Reference static variables from AvailableRooms.h
std::unordered_map<int, Room> AvailableRooms::master_room_list;
std::vector<int> AvailableRooms::available_roomIDs;
std::vector<int> AvailableRooms::occupied_roomIDs;
std::vector<int>& AvailableRooms::getAvailableRoomIDs()
{
return AvailableRooms::available_roomIDs;
}
std::vector<int>& AvailableRooms::getOccupiedRoomIDs()
{
return AvailableRooms::occupied_roomIDs;
}
std::unordered_map<int, Room>& AvailableRooms::getAllRoomLookups() {
return master_room_list;
}
// whether argument r.roomID is present in any ID's listed in available_roomIDs
bool AvailableRooms::isAvailable(const Room& r)
{
return r.isRoomAvailable();
// DEPRECIATED
//for (const auto& room: available_roomIDs)
//{
// if (r.getRoomID() == room.getRoomID())
// return true;
//}
//return false;
}
bool AvailableRooms::isOccupied(const Room& r)
{
return !(r.isRoomAvailable());
// DEPRECIATED
//for (const auto& room: occupied_roomIDs)
//{
// if (r.getRoomID() == room.getRoomID())
// return true;
//}
//return false;
}
// remove possible instance from occupied_roomIDs and
// add instance to available_roomIDs without duplicates
void AvailableRooms::setRoomAvailable(Room& r)
{
r.setAvailable(true);
// find and remove from occupied_roomIDs
auto iterator = occupied_roomIDs.begin();
for (; iterator != occupied_roomIDs.end(); ++iterator)
{
if (*iterator == r.getRoomID()) // match roomID's
{
occupied_roomIDs.erase(iterator);
break;
}
}
// add room to available rooms IF needed
for (const auto& roomID : available_roomIDs)
{
if (roomID == r.getRoomID())
return; // room already exists in available_room list. Exit the function
}
// when r.roomID is not found
available_roomIDs.push_back(r.getRoomID()); // via custom copy-constructor (IMPORTANT)
}
// remove possible instance from available_room and
// add instance to occupied_roomIDs without duplicates
void AvailableRooms::setRoomOccupied(Room& r)
{
r.setAvailable(false);
// find and remove from available_roomIDs
auto iterator = available_roomIDs.begin();
for (; iterator != available_roomIDs.end(); ++iterator)
{
if (*iterator == r.getRoomID()) // match roomID's
{
available_roomIDs.erase(iterator);
break;
}
}
// add room to occupied rooms IF needed
for (const auto& roomID : occupied_roomIDs)
{
if (roomID == r.getRoomID())
return; // room already exists in occupied_room list. Exit the function
}
occupied_roomIDs.push_back(r.getRoomID()); // via custom copy-constructor (IMPORTANT)
}
Room& AvailableRooms::lookup(const int id) {
return master_room_list.at(id);
// throws an exception if ID isn't found
}
int AvailableRooms::getAvailableCount() {
return available_roomIDs.size();
}
int AvailableRooms::getOccupiedCount() {
return occupied_roomIDs.size();
}
int AvailableRooms::getTotalCount() {
return getAvailableCount() + getOccupiedCount();
}
// Very Expensive function. Please use with care
// Load room information in disk, into the available_roomIDs vectors list
void AvailableRooms::LoadRoomsFILE(const std::string& path) {
// Open file logic
std::ifstream file;
// FILE PATH ON WINDOWS via CMAKE ../Folder
try {
file.open(path);
if (!file.is_open())
throw std::runtime_error("FAILED TO OPEN FILE " + path +
" attempting to use current directory (Works for Linux/*nix)\n");
}
catch (const std::runtime_error& e) {
std::cout << e.what();
// FILE PATH ON LINUX via Bash ./Folder
try {
file.open(path.substr(1));
if (!file.is_open())
throw std::runtime_error("FAILED TO OPEN FILE " + path);
}
catch (const std::runtime_error& ee) {
std::cout << ee.what();
}
}
// reset containers
master_room_list.clear();
available_roomIDs.clear();
occupied_roomIDs.clear();
// Read file logic
std::string line_input;
while (std::getline(file, line_input)) {
// filter out white space
if (line_input.empty())
continue;
// filter out comments
if (!line_input.empty() && line_input.at(0) == '#')
continue;
// get individual room info via c++ string-stream library
std::stringstream ss(line_input);
std::string token;
while (getline(ss, token, ' ')) { // singe white-space delimiter
int ID_input = std::stoi(token); // room ID
ss >> token;
int NO_input = std::stoi(token); // room Number
ss >> token;
int FLOOR_input = std::stoi(token); // room Floor
ss >> token;
bool AVAILABLE_input = static_cast<bool>(std::stoi(token));
ss >> token;
int TYPEID_input = std::stoi(token);
ss >> token;
std::string NAME_input = token;
ss >> token;
int BED_input = std::stoi(token);
ss >> token;
int CAPACITY_input = std::stoi(token);
ss >> token;
double BASERATE_input = std::stod(token);
// Create RoomType structure
RoomType roomtype;
roomtype.typeID = TYPEID_input;
roomtype.name = NAME_input;
roomtype.beds = BED_input;
roomtype.capacity = CAPACITY_input;
roomtype.baseRate = BASERATE_input;
// Create a room object INSIDE the hashmap
// C++17 is required for this to work
master_room_list.emplace(
std::piecewise_construct, // wtf is this .... lol
std::forward_as_tuple(ID_input),
std::forward_as_tuple(
ID_input, NO_input, FLOOR_input, roomtype, AVAILABLE_input)); // Calling Room() constructor
// append to either-list, depending on what we read in the file!
if (AVAILABLE_input == true)
available_roomIDs.push_back(ID_input);
else
occupied_roomIDs.push_back(ID_input);
}
}
// make sure you close
file.close();
}
void AvailableRooms::SaveRoomsFILE(const std::string& path) {
// Open file logic
std::ofstream file;
file.open(path);
if (!file.is_open())
throw std::runtime_error("Could not open file " + path);
// output comments
const std::string multiline_comment =
"# Comments appear like this!\n"
"#\n"
"# Room class (AS of Nov-12-2025)\n"
"# private:\n"
"# int roomID;\n"
"# int roomNo;\n"
"# int roomFloor;\n"
"# RoomType roomType;\n"
"# bool isAvailable{true};\n"
"#\n"
"#\n"
"# int typeID;\n"
"# std::string name;\n"
"# int beds;\n"
"# int capacity;\n"
"# double baseRate;\n"
"#\n"
"# SCHEMA:\n"
"# roomID roomNo roomFloor isAvailable typeID name beds capacity baseRate"
"\n";
file << multiline_comment << std::endl;
// Save each room to the path
for (const auto& pair : AvailableRooms::getAllRoomLookups()) {
file << std::to_string(pair.second.getRoomID()) << " ";
file << std::to_string(pair.second.getRoomNo()) << " ";
file << std::to_string(pair.second.getRoomFloor()) << " ";
file << std::to_string(pair.second.isRoomAvailable()) << " ";
file << std::to_string(pair.second.getRoomType().typeID) << " ";
file << pair.second.getRoomType().name << " ";
file << std::to_string(pair.second.getRoomType().beds) << " ";
file << std::to_string(pair.second.getRoomType().capacity) << " ";
file << std::to_string(pair.second.getRoomType().baseRate) << std::endl;
}
// dont forget to close
file.close();
}