Challenge

Project 3

Program:-

#include <iostream>

#include <vector>

#include <string>

// Enum for fuel types

enum class Type {

GAS,

DIESEL,

ELECTRIC

};

// Vehicle class

class Vehicle {

protected:

std::string make;

std::string model;

int year;

int odometer;

public:

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

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

// Getter and setter functions

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

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

int getYear() const { return year; }

int getOdometer() const { return odometer; }

// Maintenance activities

virtual void checkTireRotation() const = 0;

virtual void getCarInformation() const = 0;

// Pure virtual function to get fuel type

virtual Type getType() const = 0;

};

// GasCar class

class GasCar : public Vehicle {

public:

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

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

Type getType() const override { return Type::GAS; }

void checkTireRotation() const override {

if (odometer % 50000 == 0)

std::cout << "Gas car: Tire rotation is recommended." << std::endl;

else

std::cout << "Gas car: Tires are in good condition." << std::endl;

}

void getCarInformation() const override {

std::cout << "Gas Car: " << make << ", " << model << ", " << year << ", Odometer: " << odometer << std::endl;

}

};

// DieselCar class

class DieselCar : public Vehicle {

public:

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

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

Type getType() const override { return Type::DIESEL; }

void checkTireRotation() const override {

if (odometer % 50000 == 0)

std::cout << "Diesel car: Tire rotation is recommended." << std::endl;

else

std::cout << "Diesel car: Tires are in good condition." << std::endl;

}

void changeOil() const {

if (odometer >= 40000)

std::cout << "Need to Change oil for Diesel car " << std::endl;

else

std::cout << "No Need to change oil for Diesel car" << std::endl;

}

void getCarInformation() const override {

std::cout << "Diesel Car: " << make << ", " << model << ", " << year << ", Odometer: " << odometer << std::endl;

}

};

// ElectricCar class

class ElectricCar : public Vehicle {

public:

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

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

Type getType() const override { return Type::ELECTRIC; }

void checkTireRotation() const override {

if (odometer % 50000 == 0)

std::cout << "Electric car: Tire rotation is recommended." << std::endl;

else

std::cout << "Electric car: Tires are in good condition." << std::endl;

}

void checkChargerPower() const {

if (odometer % 400 == 0)

std::cout << "Electric car: Recharge battery." << std::endl;

else

std::cout << "Electric car: Battery is in good condition." << std::endl;

}

void getCarInformation() const override {

std::cout << "Electric Car: " << make << ", " << model << ", " << year << ", Odometer: " << odometer << std::endl;

}

};

// Main driver code

int main() {

std::vector<Vehicle*> inventory;

while (true) {

std::cout << "\nMenu:\n"

"0. Quit.\n"

"1. List inventory.\n"

"2. Add vehicle.\n"

"3. Update vehicle.\n"

"4. Remove vehicle.\n"

"5. Tire Rotation.\n"

"6. Oil Change.\n"

"7. Recharge.\n"

"Enter your choice: ";

int choice;

std::cin >> choice;

switch (choice) {

case 0:

std::cout << "Exiting program." << std::endl;

return 0;

case 1:

std::cout << "Inventory:\n";

for (const auto& vehicle : inventory) {

vehicle->getCarInformation();

}

break;

case 2: {

std::string make, model;

int year, odometer;

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

std::cin >> make;

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

std::cin >> model;

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

std::cin >> year;

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

std::cin >> odometer;

int type_choice;

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

std::cin >> type_choice;

Vehicle* newVehicle;

switch (type_choice) {

case 0:

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

break;

case 1:

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

break;

case 2:

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

break;

default:

std::cerr << "Invalid type choice. Vehicle not added." << std::endl;

continue;

}

inventory.push_back(newVehicle);

std::cout << "Vehicle added to inventory." << std::endl;

break;

}

case 3: {

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

int index;

std::cin >> index;

if (index < 0 || index >= inventory.size()) {

std::cerr << "Invalid index." << std::endl;

continue;

}

std::string make, model;

int year, odometer;

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

std::cin >> make;

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

std::cin >> model;

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

std::cin >> year;

std::cout << "Enter updated odometer: ";

std::cin >> odometer;

inventory[index] = new GasCar(make, model, year, odometer); // Assuming gas car for simplicity

std::cout << "Vehicle updated." << std::endl;

break;

}

case 4: {

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

int index;

std::cin >> index;

if (index < 0 || index >= inventory.size()) {

std::cerr << "Invalid index." << std::endl;

continue;

}

delete inventory[index];

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

std::cout << "Vehicle removed from inventory." << std::endl;

break;

}

case 5: {

std::cout << "Performing Tire Rotation check:\n";

for (const auto& vehicle : inventory) {

vehicle->checkTireRotation();

}

break;

}

case 6: {

std::cout << "Performing Oil Change check:\n";

for (const auto& vehicle : inventory) {

if (dynamic_cast<DieselCar*>(vehicle) != nullptr) {

dynamic_cast<DieselCar*>(vehicle)->changeOil();

}

}

break;

}

case 7: {

std::cout << "Performing Recharge check:\n";

for (const auto& vehicle : inventory) {

if (dynamic_cast<ElectricCar*>(vehicle) != nullptr) {

dynamic_cast<ElectricCar*>(vehicle)->checkChargerPower();

}

}

break;

}

default:

std::cerr << "Invalid choice. Please try again." << std::endl;

break;

}

}

return 0;

}

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.