Challenge

Project 3

//Answer to Project03
//This program has a user libraray
//for Automobile Maintenance tracking System
//It takes make, model,
// year of manufacture, fuel type
// and odometer reading
// and keeps a log for tracking

#include <iostream>

#include <string>

#include <vector>

enum Type {

GAS,

DIESEL,

ELECTRIC

};

// Base class "Vehicle"

class Vehicle

{

protected:

std::string make;

std::string model;

int year;

int odometer;

public:

Vehicle(const std::string& mk, const std::string& mdl, int yr, int odo)

: make(mk), model(mdl), year(yr), odometer(odo) {}

virtual ~Vehicle() {} // Destructor of "Vehicle"

virtual Type getType() = 0;

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

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

int getYear() const { return year; }

int getOdometer() const { return odometer; }

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

void getCarInformation() const

{

std::cout << "Make: " << make << ", Model: " << model

<< ", Year: " << year << ", Odometer: " << odometer << " km\n";

}

virtual void checkTireRotation() const {

if (odometer >= 50000 && (odometer % 50000) >= 0)

{

std::cout << "Advise tire rotation.\n";

}

else

{

std::cout << "Tires are good to go.\n";

}

}

};

// Abstract class "FuelVehicle", for gas and diesel vehicles

class FuelVehicle : public Vehicle

{

public:

FuelVehicle(const std::string& mk, const std::string& mdl, int yr, int odo)

: Vehicle(mk, mdl, yr, odo) {}

virtual std::string getFuelType() const = 0;

void checkOil() const {

if (odometer >= 10000 && (odometer % 10000) >= 0) {

std::cout << "Advise oil change.\n";

} else {

std::cout << "Oil is good.\n";

}

}

};

// GasCar class

class GasCar : public FuelVehicle

{

public:

GasCar(const std::string& mk, const std::string& mdl, int yr, int odo)

: FuelVehicle(mk, mdl, yr, odo) {}

Type getType() override { return GAS; }

std::string getFuelType() const override { return "Gas"; }

};

// DieselCar class

class DieselCar : public FuelVehicle

{

public:

DieselCar(const std::string& mk, const std::string& mdl, int yr, int odo)

: FuelVehicle(mk, mdl, yr, odo) {}

Type getType() override { return DIESEL; }

std::string getFuelType() const override { return "Diesel"; }

};

// ElectricCar class

class ElectricCar : public Vehicle

{

public:

ElectricCar(const std::string& mk, const std::string& mdl, int yr, int odo)

: Vehicle(mk, mdl, yr, odo) {}

Type getType() override { return ELECTRIC; }

void checkTireRotation() const override {

if (odometer >= 50000 && (odometer % 50000) >= 0) {

std::cout << "Advise tire rotation.\n";

} else {

std::cout << "Tires are good to go.\n";

}

}

void checkChargerPower() const {

if (odometer >= 400 && (odometer % 400) >= 0)

{

std::cout << "Advise to recharge battery.\n";

}

else

{

std::cout << "Battery is good.\n";

}

}

};

// Inventory class

class Inventory

{

private:

std::vector<Vehicle*> vehicles;

public:

~Inventory() {

for (auto vehicle : vehicles)

{

delete vehicle;

}

}

void addVehicle(Vehicle* vehicle)

{

vehicles.push_back(vehicle);

}

void listVehicles() const {

for (size_t i = 0; i < vehicles.size(); ++i) {

std::cout << i + 1 << ". ";

vehicles[i]->getCarInformation();

}

}

void updateOdometer(size_t index, int newOdometer) {

if (index < vehicles.size()) {

vehicles[index]->setOdometer(newOdometer);

std::cout << "Odometer updated successfully.\n";

} else {

std::cout << "Invalid vehicle index.\n";

}

}

void removeVehicle(size_t index) {

if (index < vehicles.size()) {

delete vehicles[index];

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

std::cout << "Vehicle removed successfully.\n";

} else {

std::cout << "Invalid vehicle index.\n";

}

}

Vehicle* getVehicle(size_t index) {

if (index < vehicles.size()) {

return vehicles[index];

}

return nullptr;

}

size_t size() const { return vehicles.size(); }

};

// Function to get user input safely

int getUserChoice()

{

int choice;

std::cout << "Enter choice: ";

std::cin >> choice;

while (std::cin.fail() || choice < 0 || choice > 7)

{

std::cin.clear();

std::cin.ignore(100, '\n');

std::cout << "Invalid input. Please try again: ";

std::cin >> choice;

}

return choice;

}

int main()

{

Inventory inventory;

int userChoice;

do {

std::cout << "\n--- Vehicle Maintenance Menu ---\n";

std::cout << "0. Quit\n";

std::cout << "1. List inventory\n";

std::cout << "2. Add vehicle\n";

std::cout << "3. Update vehicle odometer\n";

std::cout << "4. Remove vehicle\n";

std::cout << "5. Tire Rotation\n";

std::cout << "6. Oil Change\n";

std::cout << "7. Recharge\n";

userChoice = getUserChoice();

switch (userChoice)

{

case 0:

std::cout << "Exiting...\n";

break;

case 1:

inventory.listVehicles();

break;

case 2:

{

std::string make, model;

int year, odo, type;

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 reading: "; std::cin >> odo;

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

if (type == 0) {

inventory.addVehicle(new GasCar(make, model, year, odo));

} else if (type == 1) {

inventory.addVehicle(new DieselCar(make, model, year, odo));

} else if (type == 2) {

inventory.addVehicle(new ElectricCar(make, model, year, odo));

}

break;

}

case 3:

{

inventory.listVehicles();

int index; int newOdo;

std::cout << "Select vehicle to update (index): ";

std::cin >> index;

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

std::cin >> newOdo;

inventory.updateOdometer(index - 1, newOdo);

break;

}

case 4:

{

inventory.listVehicles();

int index;

std::cout << "Select vehicle to remove (index): ";

std::cin >> index;

inventory.removeVehicle(index - 1);

break;

}

case 5:

{

inventory.listVehicles();

int index;

std::cout << "Select vehicle for tire rotation check (index): ";

std::cin >> index;

Vehicle* car = inventory.getVehicle(index - 1);

if (car) {

car->checkTireRotation();

}

break;

}

case 6:

{

inventory.listVehicles();

int index;

std::cout << "Select vehicle for oil change check (index): ";

std::cin >> index;

FuelVehicle* fuelCar = dynamic_cast<FuelVehicle*>(inventory.getVehicle(index - 1));

if (fuelCar)

{

fuelCar->checkOil();

}

else

{

std::cout << "This vehicle does not require an oil change.\n";

}

break;

}

case 7: {

inventory.listVehicles();

int index;

std::cout << "Select electric vehicle to check recharge (index): ";

std::cin >> index;

ElectricCar* electricCar = dynamic_cast<ElectricCar*>(inventory.getVehicle(index - 1));

if (electricCar) {

electricCar->checkChargerPower();

}

else

{

std::cout << "This is not an electric vehicle.\n";

}

break;

}

default:

std::cout << "Invalid choice.\n";

}

} while (userChoice != 0);

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.