-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
391 lines (329 loc) · 10.4 KB
/
main.cpp
File metadata and controls
391 lines (329 loc) · 10.4 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <regex>
#include <iomanip>
#include <thread>
#include <chrono>
class User
{
public:
User() = default;
User(const std::string& first, const std::string& last, const std::string& password, const std::string& number);
std::string getFirstName() const { return firstName; }
std::string getLastName() const { return lastName; }
std::string getEmailAddress() const { return emailAddress; }
std::string getPassword() const { return password; }
void setEmailAddress(const std::string& email) { emailAddress = email; }
void setPassword(const std::string& password) { this->password = password; }
std::string getAccountNumber() const { return AccountNumber; }
std::string toString() const;
private:
std::string firstName;
std::string lastName;
std::string emailAddress;
std::string password;
};
class Car
{
public:
Car() = default;
Car(const std::string& name, int quantity, double dailyFee);
std::string getName() const { return name; }
int getQuantity() const { return quantity; }
double getDailyFee() const { return dailyFee; }
bool reserve();
double calculateRentalCost(int days) const;
private:
std::string name;
int quantity;
double dailyFee;
};
class CarRentalSystem
{
public:
CarRentalSystem();
void run();
private:
std::unordered_map<std::string, User> users;
std::unordered_map<std::string, Car> carInventory;
std::string loggedInUserEmail;
void loadUsers();
void saveUsers() const;
void printCarInventory() const;
void loginUser();
void createUserAccount();
bool isValidEmail(const std::string& email) const;
int getUserInputInteger(const std::string& prompt) const;
void reserveCar();
void chargeUser(const std::string& email, double amount) const;
void displayWelcomeMessage() const;
void displayGoodByeMessage() const;
};
User::User(const std::string& first, const std::string& last, const std::string& password, const std::string& number)
: firstName(first), lastName(last), password(password) {}
User::toString()
{
std::stringstream ss;
ss << firstName << "," << lastName << "," << password;
return ss;
}
Car::Car(const std::string& name, int quantity, double dailyFee) : name(name), quantity(quantity), dailyFee(dailyFee) {}
bool Car::reserve()
{
if (quantity > 0)
{
--quantity;
return true;
}
return false;
}
double Car::calculateRentalCost(int days) const;
{
return dailyFee * days;
}
CarRental::CarRental()
{
loadUsers();
carInventory = {
{ "Car A", Car("Car A", 2, 50.00) },
{ "Car B", Car("Car B", 2, 50.00) },
{ "Car C", Car("Car C", 2, 70.00) },
{ "Car D", Car("Car D", 2, 70.00) }
};
}
void CarRentalSystem::loadUsers()
{
ifstream infile("Users.txt");
if (!infile.is_open())
{
cout << "No user data found. Starting with an empty user database." << std::endl;
return;
}
std::string line = "";
while (getline(infile, line))
{
std::istringstream iss(line);
std::string email = "", firstName = "", lastName = "", password = "";
if (getline(iss, email, ',') && getline(iss, firstName, ',') && getline(iss, lastName, ',') && getline(iss, password))
{
users.emplace(email, User(firstName, lastName, password));
}
}
infile.close();
}
void CarRentalSystem::saveUsers() const
{
std::ofstream outfile("Users.txt");
if (!outfile.is_open())
{
std::cout << "Failed to save user data." << std::endl;
}
for (const auto& user : users)
{
outfile << user.first << "," << user.second.toString() << std::endl;
}
outfile.close();
}
void CarRentalSystem::printCarInventory() const
{
for (const auto& car : carInventory)
{
std::cout << car.second.getName() << "(Quantity: " << car.second.getQuantity() << ")" << std::endl;
}
}
void CarRentalSystem::loginUser()
{
std::string email;
std::cout << "Enter your email: ";
getline(std::cin, email);
if (!isValidEmail(email))
{
std::cout << "Invalid email address. Please enter a valid email." << std::endl;
return;
}
auto itr = users.find(email);
if (itr != users.end())
{
std::string password;
std::cout << "Enter your password: ";
getline(std::cin, password);
if (itr->second.getPassword() == password)
{
loggedInUserEmail = email;
std::cout << "Login successful. Welcome, " << itr->second.getFirstName() << " " << itr->second.LastName << "!" << std::endl;
}
else
{
std::cout << "Incorrect password." << std::endl;
}
}
else
{
std::cout << "Email is not found. Do you want to create an account? (y/n): ";
std::string response;
getline(std::cin, response);
if (response == "y" || response == "Y")
{
createUserAccount();
}
}
}
void createUserAccount()
{
std::string email;
std::cout << "Enter your email: ";
getline(std::cin, email);
if (!isValidEmail(email))
{
std::cout << "Invalid email address. Please enter an valid email." << std::endl;
return;
}
auto itr = users.find(email);
if (itr != users.end())
{
std::cout << "Email already exists." << std::endl;
return;
}
std::string firstName;
std::cout << "Enter your first name: ";
getline(std::cin, firstName);
std::string lastName;
std::cout << "Enter your last name: ";
getline(std::cin, lastName);
std::string password;
std::cout << "Enter a password: ";
getline(std::cin, password);
users.emplace(email, User(firstName, lastName, password));
std::cout << "Your account has been successfully added." << std::endl;
std::cout << "Welcome " << firstName << " " << lastName << std::endl;
saveUsers();
}
bool CarRentalSystem::isValidEmail(const std::string& email) const
{
const std::regex pattern(R"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})");
return std::regex_match(email, pattern);
}
int CarRentalSystem::getUserInputInteger(const std::string& prompt) const
{
int value = 0;
while (true)
{
std::cout << prompt;
if (std::cin >> value)
{
std::cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
break;
}
else
{
std::cout << "Invalid input. Please enter a valid number." << std::endl;
std::cin.clear();
std::cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
}
return value;
}
void CarRentalSystem::reserveCar()
{
printCarInventory();
std::string carName = "";
std::cout << "\nEnter the name of the car you want to reserve (or type 'q' to quit): ";
getline(std::cin, carName);
if (carName == 'q' || carName == 'Q') { return; }
auto itr = carInventory.find(carName);
if (itr != carInventory.end())
{
Car& selectedCar = itr->second();
if (selectedCar.reserve())
{
int rentalDays = getUserInputInteger("Enter how many days you want to rent?");
double rentalCost = selectedCar.calculateRentalDays();
std::cout << "Total Rental Cost $" << std::fixed << std::setprecision(2) << rentalCost << std::endl;
std::string confirmation = "";
while (true)
{
std::cout << "Confirm your order (y/n): ";
std::getline(std::cin, confirmation);
if (confirmation == "y" || confirmaion == "Y")
{
chargeUser(loggedInUserEmail, rentalCost);
std::cout << "Car reserved successfully. Enjoy your ride!" << std::endl;
break;
}
else if (confirmation == "n" || confirmation == "N")
{
std::cout << "Order canceled." << std::endl;
selectedCar.reserve(); // Cancel the reservation and make the car available again
break;
}
else
{
std::cout << "Invalid input. Please enter 'y' or 'n'." << std::endl;
}
}
}
else
{
std::cout << "Invalid car selection or no cars available. Please try again." << std::endl;
}
}
else
{
std::cout << "Car not found. Please enter a valid car name." << std::endl;
}
}
void CarRentalSystem::chargeUser(const std::string& email, double amount) const
{
// Placeholder Function to simulate charging the user.
std::cout << "Charging user " << email << " $" << std::fixed << std::setprecision(2) << amount << std::endl;
std::this_thread::sleep_for()std::chrono::second(2)); // Simulate payment processing delay
}
void CarRentalSystem::displayWelcomeMessage() const
{
std::cout << "Welcome to the Drive Safe Car Rental!" << std::endl;
}
void CarRentalSystem::displayGoodbyeMessage() const
{
std::cout << "Thank you for choosing Drive Safe Car Rental! Please come again!!" std::endl;
}
void CarRentalSystem::run()
{
displayWelcomeMessage();
while (true)
{
std::cout << "\nMenu Options:" << std::endl;
std::cout << "1. Login" << std::endl;
std::cout << "2. Create an Account" << std::endl;
std::cout << "3. Reserve a Car" << std::endl;
std::cout << "4. Quit" << std::endl;
int selection = getUserInputInteger("Enter your selection");
switch (selection)
{
case 1:
loginUser();
break;
case 2:
createUserAccount();
break;
case 3:
reserveCar();
break;
case 4:
saveUsers();
displayGoodbyeMessage();
return;
default:
std::cout << "Invalid choice. Please enter a valid menu option." << std::endl;
}
}
}
int main(void)
{
CarRentalSystem carRentalSystem;
carRentalSystem.run();
return 0;
}