0% found this document useful (0 votes)
5 views7 pages

Lab 8

The document contains a C++ program that implements a car rental service with classes for car details, cars, customers, and the rental service itself. It allows adding cars, renting them to customers, and displaying available cars and rental transactions. The main function demonstrates the creation of car and customer objects, adding cars to the service, renting them out, and displaying the results.

Uploaded by

suzuki.wagnor234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Lab 8

The document contains a C++ program that implements a car rental service with classes for car details, cars, customers, and the rental service itself. It allows adding cars, renting them to customers, and displaying available cars and rental transactions. The main function demonstrates the creation of car and customer objects, adding cars to the service, renting them out, and displaying the results.

Uploaded by

suzuki.wagnor234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 8

Program:
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class CarDetails {
string make;
int year;
string type;
public:
CarDetails(const string& make, int year, const string& type)
: make(make), year(year), type(type) {}

void displayDetails() const {


cout << "Make: " << make << ", Year: " << year << ", Type: " << type << endl;
}
};

class Car {
int model;
string licence_plate;
int rate_per_day;
CarDetails car_details;
public:
Car(int model, const string& licence_plate, int rate_per_day, const CarDetails& details)
: model(model), licence_plate(licence_plate), rate_per_day(rate_per_day),
car_details(details) {}

void displayCar() const {


cout << "Model: " << model << ", Licence Plate: " << licence_plate << ", Rate per Day: "
<< rate_per_day << endl;
car_details.displayDetails();
}

int getModel() const { return model; }


const string& getLicencePlate() const { return licence_plate; }
const CarDetails& getCarDetails() const { return car_details; }
};

class Customer {
string name;
string licence_no;
int contact_no;
public:
Customer(const string& name, const string& licence_no, int contact_no)
: name(name), licence_no(licence_no), contact_no(contact_no) {}

void displayCustomer() const {


cout << "Name: " << name << ", Licence No: " << licence_no << ", Contact No: " <<
contact_no << endl;
}

const string& getName() const { return name; }


};
class Car_Rental_Service {
string company_name;
string address;
vector<Car> cars;
vector<pair<Customer, Car>> rentalTransactions;
static int carRented;

public:
Car_Rental_Service(const string& c_name, const string& addr)
: company_name(c_name), address(addr) {}

void addCar(const Car& car) {


cars.push_back(car);
cout << "Car added: " << car.getModel() << " (" << car.getLicencePlate() << ")\n";
}

void displayCars() const {


if (cars.empty()) {
cout << "No cars available in the system.\n";
return;
}
cout << "\nAvailable Cars:\n";
for (size_t i = 0; i < cars.size(); ++i) {
cout << "Car " << i + 1 << ":\n";
cars[i].displayCar();
}
}
void rentCar(const Customer& customer) {
if (!cars.empty()) {
Car car = cars.back();
rentalTransactions.push_back({ customer, car });
cars.pop_back(); // Remove the rented car from available cars
carRented++;
cout << "Car rented to: " << customer.getName() << endl;
car.displayCar();
customer.displayCustomer();
cout << "Total number of cars rented: " << carRented << endl;
} else {
cout << "No cars available for rent.\n";
}
}

void displayRentals() const {


if (rentalTransactions.empty()) {
cout << "No rental transactions available.\n";
return;
}
cout << "\nRental Transactions:\n";
for (size_t i = 0; i < rentalTransactions.size(); ++i) {
const Customer& customer = rentalTransactions[i].first;
const Car& car = rentalTransactions[i].second;
customer.displayCustomer();
car.displayCar();
}
}
};

int Car_Rental_Service::carRented = 0;

int main() {
// Creating CarDetails objects
CarDetails carDetail1("Toyota", 2020, "SUV");
CarDetails carDetail2("Honda", 2019, "Sedan");
CarDetails carDetail3("Ford", 2021, "Truck");

// Creating Car objects


Car car1(1, "ABC-123", 50, carDetail1);
Car car2(2, "XYZ-789", 60, carDetail2);
Car car3(3, "LMN-456", 70, carDetail3);

// Creating Car_Rental_Service object


Car_Rental_Service rentalService("Rent A Car", "123 Main St");

// Adding cars to the rental service


rentalService.addCar(car1);
rentalService.addCar(car2);
rentalService.addCar(car3);

// Display available cars


rentalService.displayCars();

// Creating Customer objects


Customer customer1("Sarmad Jamil", "JD145", 51131234);
Customer customer2("Jamil And Sons", "JS90", 6789);

// Renting cars to customers


rentalService.rentCar(customer1);
rentalService.rentCar(customer2);

// Display rental transactions


rentalService.displayRentals();

// Display remaining cars


rentalService.displayCars();

return 0;
}

Output:

You might also like