-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarrental.cpp
77 lines (60 loc) · 1.94 KB
/
carrental.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
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
class Car {
public:
string model;
string licensePlate;
bool isRented;
Car(string m, string lp) : model(m), licensePlate(lp), isRented(false) {}
};
class CarRentalSystem {
private:
vector<Car> cars;
public:
void addCar(string model, string licensePlate) {
cars.push_back(Car(model, licensePlate));
}
void displayAvailableCars() {
cout << "Available Cars:" << endl;
cout << setw(15) << "Model" << setw(15) << "License Plate" << endl;
cout << "-----------------------------------" << endl;
for (const auto& car : cars) {
if (!car.isRented) {
cout << setw(15) << car.model << setw(15) << car.licensePlate << endl;
}
}
}
void rentCar(string licensePlate) {
for (auto& car : cars) {
if (car.licensePlate == licensePlate && !car.isRented) {
car.isRented = true;
cout << "Car rented successfully!" << endl;
return;
}
}
cout << "Car not available for rent." << endl;
}
void returnCar(string licensePlate) {
for (auto& car : cars) {
if (car.licensePlate == licensePlate && car.isRented) {
car.isRented = false;
cout << "Car returned successfully!" << endl;
return;
}
}
cout << "Invalid license plate or car not rented." << endl;
}
};
int main() {
CarRentalSystem rentalSystem;
rentalSystem.addCar("Toyota Camry", "ABC123");
rentalSystem.addCar("Honda Accord", "XYZ789");
rentalSystem.displayAvailableCars();
rentalSystem.rentCar("ABC123");
rentalSystem.displayAvailableCars();
rentalSystem.returnCar("ABC123");
rentalSystem.displayAvailableCars();
return 0;
}