Challenge

Project 3

#include

#include

#include

// Define an enumeration for fuel types

enum class FuelType{

 GAS,

 DIESEL,

 ELECTRIC

};

// Define a base class Vehicle

class Vehicle

{

protected:

 std::string make;

 std::string model;

 int year;

 int odometer;

public:

 // Constructor for Vehicle class

 Vehicle(const std::string& make, const std::string& model, int year, int odometer)

 : make(make), model(model), year(year), odometer(odometer) {}

 // Getter methods for Vehicle class

 std::string getMake() const { return make; }

 std::string getModel() const { return model; }

 int getYear() const { return year; }

 int getOdometer() const { return odometer; }

 // Setter method for odometer

 void setOdometer(int newOdometer) { odometer = newOdometer; }

 // Pure virtual function to get the fuel type of the vehicle

 virtual FuelType getFuelType() const = 0;

 // Pure virtual function to perform maintenance on the vehicle

 virtual void performMaintenance() = 0;

 // Virtual function to rotate tires

 virtual void rotateTires()

 {

 std::cout << "Rotating tires for " << make>

 }

 // Virtual function to perform oil change

 virtual void performOilChange()

 {

 std::cout << "Performing oil change for " << make>

 }

 // Virtual function to recharge the vehicle

 virtual void recharge()

 {

 std::cout << "Recharging " << make>

 }

 // Virtual function to check if tire rotation is needed

 virtual void checkTireRotation()

 {

 int tireCycle = 50000; // Tire rotation cycle in kilometers

 int distanceRun = getOdometer() % tireCycle;

 if (distanceRun == 0)

 {

 std::cout << "Time to rotate tires for " << make>

std::endl;

 }

 else

 {

 std::cout << "Tires are good to go for " << make>

std::endl;

 int remainder = tireCycle - distanceRun;

 std::cout << "Remaining distance for tire rotation: " << remainder>

<< std>

 }

 }

 // Virtual function to check if battery recharge is needed

 virtual void checkPower()

 {

 int rechargeDistance = 400; // Recharge distance in kilometers

 int distanceRun = getOdometer() % rechargeDistance;

 if (distanceRun == 0)

 {

 std::cout << "Time to recharge the battery for " << make>

std::endl;

 }

 else

 {

 std::cout << "Your vehicle is good to go for " << make>

std::endl;

 int remainder = rechargeDistance - distanceRun;

 std::cout << "Remaining distance for recharge: " << remainder>

std::endl;

 }

 }

 // Virtual destructor for the base class

 virtual ~Vehicle() {}

};

class GasCar : public Vehicle {

public:

 // Constructor for GasCar class

 GasCar(const std::string& make, const std::string& model, int year, int odometer)

 : Vehicle(make, model, year, odometer) {}

 // Return the fuel type of GasCar

 FuelType getFuelType() const override {

 return FuelType::GAS;

 }

 // Perform maintenance on GasCar

 void performMaintenance() override {

 std::cout << "Performing maintenance on GasCar" << std>

 }

 // Rotate tires for GasCar

 void rotateTires() override {

 std::cout << "Rotating tires for GasCar" << std>

 }

 // Perform oil change for GasCar

 void performOilChange() override {

 std::cout << "Performing oil change for GasCar" << std>

 }

};

class DieselCar : public Vehicle {

public:

 // Constructor for DieselCar class

 DieselCar(const std::string& make, const std::string& model, int year, int odometer)

 : Vehicle(make, model, year, odometer) {}

 // Return the fuel type of DieselCar

 FuelType getFuelType() const override {

 return FuelType::DIESEL;

 }

 // Perform maintenance on DieselCar

 void performMaintenance() override {

 std::cout << "Performing maintenance on DieselCar" << std>

 }

 // Rotate tires for DieselCar

 void rotateTires() override {

 std::cout << "Rotating tires for DieselCar" << std>

 }

 // Perform oil change for DieselCar

 void performOilChange() override {

 std::cout << "Performing oil change for DieselCar" << std>

 }

};

class ElectricCar : public Vehicle {

public:

 // Constructor for ElectricCar class

 ElectricCar(const std::string& make, const std::string& model, int year, int

odometer)

 : Vehicle(make, model, year, odometer) {}

 // Return the fuel type of ElectricCar

 FuelType getFuelType() const override {

 return FuelType::ELECTRIC;

 }

 // Perform maintenance on ElectricCar

 void performMaintenance() override {

 std::cout << "Performing maintenance on ElectricCar" << std>

 }

 // Recharge ElectricCar

 void recharge() override {

 std::cout << "Recharging ElectricCar" << std>

 }

};

class MaintenanceLibrary {

private:

 std::vector inventory;

public:

 // Destructor for MaintenanceLibrary class

 ~MaintenanceLibrary() {

 // Delete each vehicle in the inventory

 for (Vehicle* vehicle : inventory) {

 delete vehicle;

 }

 }

 // Add a vehicle to the inventory

 void addVehicle(Vehicle* vehicle) {

 inventory.push_back(vehicle);

 }

 // List all vehicles in the inventory

 void listVehicles() const {

 int index = 0;

 // Iterate over each vehicle in the inventory

 for (const Vehicle* vehicle : inventory) {

 std::cout << "Index: " << index>

 std::cout << "Make: " << vehicle>getMake() << std>

 std::cout << "Model: " << vehicle>getModel() << std>

 std::cout << "Year: " << vehicle>getYear() << std>

 std::cout << "Odometer: " << vehicle>getOdometer() << std>

 std::cout << "Fuel Type: ";

 // Print the fuel type based on the enum value

 switch (vehicle->getFuelType()) {

 case FuelType::GAS:

 std::cout << "Gas" << std>

 break;

 case FuelType::DIESEL:

 std::cout << "Diesel" << std>

 break;

 case FuelType::ELECTRIC:

 std::cout << "Electric" << std>

 break;

 }

 std::cout << std>

 ++index;

 }

 }

 // Update the odometer reading of a vehicle in the inventory

 void updateVehicle(int index, int newOdometer) {

 if (index >= 0 && static_cast(index) < inventory>

 Vehicle* vehicle = inventory[index];

 vehicle->performMaintenance();

 vehicle->setOdometer(newOdometer);

 }

 }

 // Remove a vehicle from the inventory

 void removeVehicle(int index) {

 if (index >= 0 && static_cast(index) < inventory>

 delete inventory[index];

 inventory.erase(inventory.begin() + index);

 }

 }

 // Rotate tires for a vehicle in the inventory

 void rotateTires(int index) {

 if (index >= 0 && static_cast(index) < inventory>

 Vehicle* vehicle = inventory[index];

 vehicle->rotateTires();

 }

 }

 // Perform an oil change for a vehicle in the inventory

 void performOilChange(int index) {

 if (index >= 0 && static_cast(index) < inventory>

 Vehicle* vehicle = inventory[index];

 vehicle->performOilChange();

 }

 }

 // Recharge an electric vehicle in the inventory

 void recharge(int index) {

 if (index >= 0 && static_cast(index) < inventory>

 Vehicle* vehicle = inventory[index];

 vehicle->recharge();

 }

 }

 // Check if tire rotation is due for a vehicle in the inventory

 void checkTireRotation(int index) {

 if (index >= 0 && static_cast(index) < inventory>

 Vehicle* vehicle = inventory[index];

 int currentOdometer = vehicle->getOdometer();

 int tireCycle = 50000;

 int remainder = currentOdometer % tireCycle;

 if (currentOdometer >= tireCycle && remainder >= 0) {

 std::cout << "Tire rotation is due for " << vehicle>getMake() << " "

<< vehicle>getModel() << std>

 std::cout << "Current odometer reading: " << currentOdometer>

<< std>

 std::cout << "Tire rotation cycle: " << tireCycle>

 std::cout << "Remaining distance for tire rotation: " << tireCycle>

remainder << " km" << std>

 } else {

 std::cout << "Tires are good to go for " << vehicle>getMake() << " "

<< vehicle>getModel() << std>

 std::cout << "Current odometer reading: " << currentOdometer>

<< std>

 std::cout << "Tire rotation cycle: " << tireCycle>

 std::cout << "Remaining distance for tire rotation: " << tireCycle>

remainder << " km" << std>

 }

 }

 }

 // Check if battery recharge is due for an electric vehicle in the inventory

 void checkBatteryPower(int index) {

 if (index >= 0 && static_cast(index) < inventory>

 Vehicle* vehicle = inventory[index];

 int currentOdometer = vehicle->getOdometer();

 int powerCycle = 400;

 int remainder = currentOdometer % powerCycle;

 if (currentOdometer >= powerCycle && remainder == 0) {

 std::cout << "Battery recharge is due for " << vehicle>getMake() << " " << vehicle>getModel() << std>

 std::cout << "Current odometer reading: " << currentOdometer>

<< std>

 std::cout << "Battery recharge cycle: " << powerCycle>

std::endl;

 } else {

 std::cout << "Battery is good to go for " << vehicle>getMake() << " "

<< vehicle>getModel() << std>

 std::cout << "Current odometer reading: " << currentOdometer>

<< std>

 std::cout << "Battery recharge cycle: " << powerCycle>

std::endl;

 std::cout << "Remaining distance for battery recharge: " << powerCycle>

- remainder << " km" << std>

 }

 }

 }

};

int main() {

MaintenanceLibrary library;

 while (true) {

 // Display menu options

 std::cout << "Menu:" << std>

 std::cout << "0. Quit." << std>

 std::cout << "1. List inventory." << std>

 std::cout << "2. Add vehicle." << std>

 std::cout << "3. Update vehicle." << std>

 std::cout << "4. Remove vehicle." << std>

 std::cout << "5. Check tire rotation." << std>

 std::cout << "6. Check battery power." << std>

 std::cout << "7. Rotate tires." << std>

 std::cout << "8. Perform oil change." << std>

 std::cout << "9. Recharge vehicle." << std>

 std::cout << "Select an option: ";

 int option;

 std::cin >> option;

 if (option == 0) {

 break; // Exit the loop and quit the program

 } else if (option == 1) {

 library.listVehicles(); // List the vehicles in the inventory

 } else if (option == 2) {

 // Add a new vehicle to the inventory

 std::cout << "Enter vehicle make: ";

 std::string make;

 std::cin >> make;

 std::cout << "Enter vehicle model: ";

 std::string model;

 std::cin >> model;

 std::cout << "Enter vehicle year: ";

 int year;

 std::cin >> year;

 std::cout << "Enter vehicle odometer reading: ";

 int odometer;

 std::cin >> odometer;

 std::cout << "Select fuel type (0: Gas, 1: Diesel, 2: Electric): ";

 int fuelTypeOption;

 std::cin >> fuelTypeOption;

 FuelType fuelType;

 if (fuelTypeOption == 0) {

 fuelType = FuelType::GAS;

 } else if (fuelTypeOption == 1) {

 fuelType = FuelType::DIESEL;

 } else if (fuelTypeOption == 2) {

 fuelType = FuelType::ELECTRIC;

 } else {

 std::cout << "Invalid fuel type. Vehicle not added." << std>

 continue; // Skip to the next iteration of the loop

 }

 // Create a new vehicle based on the selected fuel type

 Vehicle* vehicle = nullptr;

 switch (fuelType) {

 case FuelType::GAS:

 vehicle = new GasCar(make, model, year, odometer);

 break;

 case FuelType::DIESEL:

 vehicle = new DieselCar(make, model, year, odometer);

 break;

 case FuelType::ELECTRIC:

 vehicle = new ElectricCar(make, model, year, odometer);

 break;

 }

 library.addVehicle(vehicle); // Add the vehicle to the inventory

 std::cout << "Vehicle added successfully." << std>

 }

else if (option == 3) {

 // Update the odometer reading of a vehicle in the inventory

 std::cout << "Enter index of the vehicle to update: ";

 int index;

 std::cin >> index;

 std::cout << "Enter new odometer reading: ";

 int newOdometer;

 std::cin >> newOdometer;

 library.updateVehicle(index, newOdometer);

 } else if (option == 4) {

 // Remove a vehicle from the inventory

 std::cout << "Enter index of the vehicle to remove: ";

 int index;

 std::cin >> index;

 library.removeVehicle(index);

 } else if (option == 5) {

 // Check tire rotation for a vehicle in the inventory

 std::cout << "Enter index of the vehicle to check tire rotation: ";

 int index;

 std::cin >> index;

 library.checkTireRotation(index);

 } else if (option == 6) {

 // Check battery power for an electric vehicle in the inventory

 std::cout << "Enter index of the vehicle to check battery power: ";

 int index;

 std::cin >> index;

 library.checkBatteryPower(index);

 } else if (option == 7) {

 // Rotate tires for a vehicle in the inventory

 std::cout << "Enter index of the vehicle to rotate tires: ";

 int index;

 std::cin >> index;

 library.rotateTires(index);

 } else if (option == 8) {

 // Perform an oil change for a vehicle in the inventory

 std::cout << "Enter index of the vehicle to perform oil change: ";

 int index;

 std::cin >> index;

 library.performOilChange(index);

 } else if (option == 9) {

 // Recharge the battery of an electric vehicle in the inventory

 std::cout << "Enter index of the vehicle to recharge: ";

 int index;

 std::cin >> index;

 library.recharge(index);

 } else {

 std::cout << "Invalid option. Try again." << std>

 }

 std::cout << std>

 }

 return 0;

}

EXPLANATION:

  1. Enumerations: The program defines an enumeration FuelType to represent different fuel types: GAS, DIESEL, and ELECTRIC.
  2. Base Class Vehicle: The Vehicle class is the base class for all vehicle types. It contains common attributes such as make, model, year, and odometer reading. It also declares pure virtual functions for getting the fuel type and performing maintenance, as well as virtual functions for rotating tires, performing oil changes, and recharging.
  3. Derived Classes: The program defines three derived classes GasCar, DieselCar, and ElectricCar, each representing a specific type of vehicle. These classes override the base class functions to provide specific implementations for fuel type, maintenance, tire rotation, oil change, and recharging.
  4. MaintenanceLibrary Class: The MaintenanceLibrary class manages the inventory of vehicles. It contains a vector of pointers to Vehicle objects. The class provides functions to add vehicles, list vehicles, update odometer readings, remove vehicles, check tire rotation and battery power, rotate tires, perform oil changes, and recharge vehicles.
  5. Main Function: The main function serves as the user interface for interacting with the maintenance library. Users are presented with a menu of options to choose from, and based on their input, the program performs the corresponding maintenance tasks.
  6. Memory Management: The MaintenanceLibrary class takes care of memory management by using pointers and dynamically allocated memory for vehicles. It ensures proper deletion of vehicles when they are removed from the inventory or when the program terminates.
  7. In the output we can add vehicle details , update them and remove them from the console. 
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.