Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (133 loc) · 3.99 KB
/
Copy pathmain.cpp
File metadata and controls
151 lines (133 loc) · 3.99 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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Car {
private:
string brand;
string model;
int year;
double pricePerDay;
bool isAvailable;
public:
Car(string b, string m, int y, double price) : brand(b), model(m), year(y), pricePerDay(price), isAvailable(true) {}
// Show car details
void showDetails() const {
cout << brand << " " << model << " (" << year << ") - $" << pricePerDay << " per day" << (isAvailable ? " [Available]" : " [Rented]") << endl;
}
// Rent the car
bool rentCar() {
if (isAvailable) {
isAvailable = false;
return true;
}
return false;
}
// Return the car
void returnCar() {
isAvailable = true;
}
// Check if car is available
bool availability() const {
return isAvailable;
}
double getPricePerDay() const {
return pricePerDay;
}
};
class CarRentalSystem {
private:
vector<Car> cars;
public:
CarRentalSystem() {
// Predefined cars available for rent
cars.push_back(Car("Toyota", "Corolla", 2020, 50));
cars.push_back(Car("Ford", "Fiesta", 2019, 40));
cars.push_back(Car("Tesla", "Model 3", 2021, 100));
cars.push_back(Car("BMW", "X5", 2018, 90));
}
// Display available cars
void showAvailableCars() const {
cout << "Available cars for rent:" << endl;
for (const auto& car : cars) {
if (car.availability()) {
car.showDetails();
}
}
}
// Rent a car
void rentCar(int index, int days) {
if (index >= 0 && index < cars.size()) {
if (cars[index].availability()) {
if (cars[index].rentCar()) {
cout << "You have rented " << cars[index].getPricePerDay() << " per day." << endl;
cout << "Total cost for " << days << " days: $" << cars[index].getPricePerDay() * days << endl;
}
} else {
cout << "Sorry, this car is already rented." << endl;
}
} else {
cout << "Invalid car selection." << endl;
}
}
// Return a rented car
void returnCar(int index) {
if (index >= 0 && index < cars.size()) {
cars[index].returnCar();
cout << "You have returned the car." << endl;
} else {
cout << "Invalid car selection." << endl;
}
}
// Display all cars
void showAllCars() const {
cout << "All cars:" << endl;
for (const auto& car : cars) {
car.showDetails();
}
}
};
int main() {
CarRentalSystem rentalSystem;
int choice;
while (true) {
cout << "\nCar Rental System Menu:\n";
cout << "1. Show Available Cars\n";
cout << "2. Rent a Car\n";
cout << "3. Return a Car\n";
cout << "4. Show All Cars\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
rentalSystem.showAvailableCars();
break;
case 2: {
int carIndex, days;
cout << "Enter the car index (0, 1, 2, etc.) to rent: ";
cin >> carIndex;
cout << "Enter the number of days you want to rent: ";
cin >> days;
rentalSystem.rentCar(carIndex, days);
break;
}
case 3: {
int carIndex;
cout << "Enter the car index to return: ";
cin >> carIndex;
rentalSystem.returnCar(carIndex);
break;
}
case 4:
rentalSystem.showAllCars();
break;
case 5:
cout << "Exiting the system..." << endl;
return 0;
default:
cout << "Invalid option, please try again." << endl;
}
}
return 0;
}