C++ Projects: Car Rental System
This system helps customers rent available cars. It maintains a list of different cars, their rental costs, and rental status. The system utilizes Object-Oriented Programming (OOP) concepts such as classes, file handling, and inheritance.
Features
- Adding new cars to the system.
- Displaying available cars and their rental status.
- Renting a car.
- Returning a car.
- Calculating the rental price based on rental hours or days.
- Saving rental car data to files.
Skills Used
- OOP Concepts: Classes, encapsulation, and different methods.
- File Handling: Saving and loading car data.
- Memory Management: Efficient handling of stored data.
- Vector Usage: Managing car data dynamically.
- Quality Assurance: Ensuring cars are rented and returned accurately.
Code Implementation
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
// Class to represent a single car
class Car {
private:
int id;
string model;
string manufacturer;
double dailyRate;
bool isRented;
public:
Car(int carId, string carModel, string carManufacturer, double rate)
: id(carId), model(carModel), manufacturer(carManufacturer), dailyRate(rate), isRented(false) {}
int getId() const { return id; }
string getModel() const { return model; }
string getManufacturer() const { return manufacturer; }
double getRate() const { return dailyRate; }
bool getStatus() const { return isRented; }
void rentCar() { isRented = true; }
void returnCar() { isRented = false; }
void display() const {
cout << "ID: " << id << ", Model: " << model << ", Manufacturer: " << manufacturer
<< ", Rate: $" << dailyRate << "/day, Status: " << (isRented ? "Rented" : "Available") << endl;
}
};
// Class for managing the rental system
class CarRentalSystem {
private:
vector<Car> cars;
void saveToFile() {
ofstream file("cars_data.txt");
for (auto& car : cars) {
file << car.getId() << ";" << car.getModel() << ";" << car.getManufacturer()
<< ";" << car.getRate() << ";" << car.getStatus() << ";" << endl;
}
file.close();
}
void loadFromFile() {
ifstream file("cars_data.txt");
if (file.is_open()) {
string line;
while (getline(file, line)) {
size_t pos = 0;
vector<string> tokens;
while ((pos = line.find(";")) != string::npos) {
tokens.push_back(line.substr(0, pos));
line.erase(0, pos + 1);
}
int id = stoi(tokens[0]);
string model = tokens[1];
string manufacturer = tokens[2];
double rate = stod(tokens[3]);
bool status = (tokens[4] == "1");
Car car(id, model, manufacturer, rate);
if (status) car.rentCar();
cars.push_back(car);
}
file.close();
}
}
public:
CarRentalSystem() { loadFromFile(); }
~CarRentalSystem() { saveToFile(); }
void addCar(int id, const string& model, const string& manufacturer, double rate) {
cars.emplace_back(id, model, manufacturer, rate);
cout << "Car added successfully!" << endl;
}
void displayCars() const {
if (cars.empty()) {
cout << "No cars available in the system." << endl;
return;
}
for (const auto& car : cars) {
car.display();
}
}
void rentCar(int id) {
for (auto& car : cars) {
if (car.getId() == id) {
if (!car.getStatus()) {
car.rentCar();
cout << "Car rented successfully!" << endl;
} else {
cout << "Car is already rented." << endl;
}
return;
}
}
cout << "Car not found." << endl;
}
void returnCar(int id, int days) {
for (auto& car : cars) {
if (car.getId() == id) {
if (car.getStatus()) {
car.returnCar();
double cost = days * car.getRate();
cout << "Car returned successfully! Total cost: $" << cost << endl;
} else {
cout << "Car was not rented." << endl;
}
return;
}
}
cout << "Car not found." << endl;
}
};
int main() {
CarRentalSystem system;
int choice;
do {
cout << "\n=== Car Rental System ===" << endl;
cout << "1. Add Car" << endl;
cout << "2. Display All Cars" << endl;
cout << "3. Rent Car" << endl;
cout << "4. Return Car" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int id;
string model, manufacturer;
double rate;
cout << "Enter car ID: "; cin >> id;
cin.ignore();
cout << "Enter car model: "; getline(cin, model);
cout << "Enter manufacturer: "; getline(cin, manufacturer);
cout << "Enter daily rate: "; cin >> rate;
system.addCar(id, model, manufacturer, rate);
break;
}
case 2:
system.displayCars();
break;
case 3: {
int id;
cout << "Enter Car ID to rent: ";
cin >> id;
system.rentCar(id);
break;
}
case 4: {
int id, days;
cout << "Enter car ID to return: "; cin >> id;
cout << "Enter number of rental days: "; cin >> days;
system.returnCar(id, days);
break;
}
case 5:
cout << "Exiting... Bye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 5);
return 0;
}
Real-World Examples and Case Studies
Enterprise Car Rental Systems
Major rental services like Avis, Hertz, and Enterprise use advanced systems to manage fleets across multiple locations. Features include:
- GPS tracking for rented vehicles.
- Integration with virtual booking systems.
- Dynamic pricing based on demand.
Ride-Sharing Services
Companies like Uber and Lyft offer rental services with advanced features such as:
- Automatic billing based on distance, time, and usage.
- User verification through ID and driver’s license.
Airport Car Rentals
- Fleet management for quick vehicle turnover.
- Integration with airlines for pre-booking.
Problem-Solving Approaches
- Fraud Prevention & Security: Encryption and OTP verification for customer data.
- Handling High Traffic: Database optimization and multi-threading.
- Scalability: Using
unordered_map
for efficient fleet management.
Thanks for reading! Gear up with your code editor and build something amazing!
Comments
Post a Comment