Challenge

Project 3

* Class and Interface

#include <iostream>

#include <vector>

#include <memory>

#include <string>

using namespace std;

// Enum for car types

enum Type {

GAS,

DIESEL,

ELECTRIC

};

// Abstract base class for all vehicles

class Vehicle {

protected:

string make;

string model;

int year;

int odometer;

public:

// Constructor

Vehicle(string m, string mo, int y, int odo)

: make(m), model(mo), year(y), odometer(odo) {}

// Getter and Setter methods

string getMake() const { return make; }

void setMake(const string& m) { make = m; }

string getModel() const { return model; }

void setModel(const string& mo) { model = mo; }

int getYear() const { return year; }

void setYear(int y) { year = y; }

int getOdometer() const { return odometer; }

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

virtual void checkTireRotation() const = 0; // Pure virtual method

virtual Type getType() const = 0; // Pure virtual method

// Print car information

void getCarInformation() const {

cout << "Make: " << make << ", Model: " << model << ", Year: " << year << ", Odometer: " << odometer << " km" << endl;

}

};

// Abstract base class for fuel-based vehicles

class FuelVehicle : public Vehicle {

public:

// Constructor

FuelVehicle(string m, string mo, int y, int odo)

: Vehicle(m, mo, y, odo) {}

virtual void checkOil() const = 0; // Pure virtual method

virtual Type getFuelType() const = 0; // Pure virtual method

};

// Class for Gas Cars

class GasCar : public FuelVehicle {

public:

// Constructor

GasCar(string m, string mo, int y, int odo)

: FuelVehicle(m, mo, y, odo) {}

// Implement the pure virtual methods

void checkTireRotation() const override {

if (odometer % 50000 == 0) {

cout << "GasCar needs tire rotation." << endl;

} else {

cout << "GasCar tires are good to go." << endl;

}

}

void checkOil() const override {

if (odometer % 10000 == 0) {

cout << "GasCar needs oil change." << endl;

} else {

cout << "GasCar oil level is fine." << endl;

}

}

Type getFuelType() const override { return GAS; }

Type getType() const override { return GAS; }

};

// Class for Diesel Cars

class DieselCar : public FuelVehicle {

public:

// Constructor

DieselCar(string m, string mo, int y, int odo)

: FuelVehicle(m, mo, y, odo) {}

// Implement the pure virtual methods

void checkTireRotation() const override {

if (odometer % 50000 == 0) {

cout << "DieselCar needs tire rotation." << endl;

} else {

cout << "DieselCar tires are good to go." << endl;

}

}

void checkOil() const override {

if (odometer % 10000 == 0) {

cout << "DieselCar needs oil change." << endl;

} else {

cout << "DieselCar oil level is fine." << endl;

}

}

Type getFuelType() const override { return DIESEL; }

Type getType() const override { return DIESEL; }

};

// Class for Electric Cars

class ElectricCar : public Vehicle {

public:

// Constructor

ElectricCar(string m, string mo, int y, int odo)

: Vehicle(m, mo, y, odo) {}

// Implement the pure virtual methods

void checkTireRotation() const override {

if (odometer % 50000 == 0) {

cout << "ElectricCar needs tire rotation." << endl;

} else {

cout << "ElectricCar tires are good to go." << endl;

}

}

void checkChargerPower() const {

if (odometer % 400 == 0) {

cout << "ElectricCar needs a recharge." << endl;

} else {

cout << "ElectricCar battery level is fine." << endl;

}

}

Type getType() const override { return ELECTRIC; }

};

*Inventory Management System

// Inventory Manager class

class InventoryManager {

private:

vector<shared_ptr<Vehicle>> vehicles;

public:

// Add a new vehicle to the inventory

void addVehicle(shared_ptr<Vehicle> v) {

vehicles.push_back(v);

}

// List all vehicles in the inventory

void listVehicles() const {

for (const auto& vehicle : vehicles) {

vehicle->getCarInformation();

}

}

// Update an existing vehicle in the inventory

void updateVehicle(int index, const shared_ptr<Vehicle>& v) {

if (index >= 0 && index < vehicles.size()) {

vehicles[index] = v;

} else {

cout << "Invalid index!" << endl;

}

}

// Remove a vehicle from the inventory

void removeVehicle(int index) {

if (index >= 0 && index < vehicles.size()) {

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

} else {

cout << "Invalid index!" << endl;

}

}

// Check tire rotation for all vehicles

void checkTireRotation() const {

for (const auto& vehicle : vehicles) {

vehicle->checkTireRotation();

}

}

// Check oil change for all fuel-based vehicles

void checkOilChange() const {

for (const auto& vehicle : vehicles) {

if (auto fv = dynamic_pointer_cast<FuelVehicle>(vehicle)) {

fv->checkOil();

}

}

}

// Check recharge for electric cars

void checkRecharge() const {

for (const auto& vehicle : vehicles) {

if (auto ec = dynamic_pointer_cast<ElectricCar>(vehicle)) {

ec->checkChargerPower();

}

}

}

};

*Driver code

int main() {

InventoryManager manager;

while (true) {

cout << "\nMenu:\n";

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

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

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

cout << "3. Update vehicle\n";

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

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

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

cout << "7. Recharge\n";

int choice;

cin >> choice;

if (choice == 0) {

break;

} else if (choice == 1) {

manager.listVehicles();

} else if (choice == 2) {

string make, model;

int year, odo, type;

cout << "Enter make: ";

cin >> make;

cout << "Enter model: ";

cin >> model;

cout << "Enter year (4 digits): ";

cin >> year;

cout << "Enter odometer reading: ";

cin >> odo;

cout << "Enter type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): ";

cin >> type;

shared_ptr<Vehicle> newVehicle;

if (type == GAS) {

newVehicle = make_shared<GasCar>(make, model, year, odo);

} else if (type == DIESEL) {

newVehicle = make_shared<DieselCar>(make, model, year, odo);

} else if (type == ELECTRIC) {

newVehicle = make_shared<ElectricCar>(make, model, year, odo);

} else {

cout << "Invalid type!" << endl;

continue;

}

manager.addVehicle(newVehicle);

} else if (choice == 3) {

int index;

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

cin >> index;

string make, model;

int year, odo, type;

cout << "Enter new make: ";

cin >> make;

cout << "Enter new model: ";

cin >> model;

cout << "Enter new year (4 digits): ";

cin >> year;

cout << "Enter new odometer reading: ";

cin >> odo;

cout << "Enter new type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): ";

cin >> type;

shared_ptr<Vehicle> updatedVehicle

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.