Challenge

Project 3

#include <iostream>

#include <vector>

#include <string>

#include <cmath>

enum Type {

GAS,

DIESEL,

ELECTRIC

};

class Vehicle {

protected:

std::string make;

std::string model;

int year;

int odometer;

public:

Vehicle(std::string _make, std::string _model, int _year, int _odometer)

: make(_make), model(_model), year(_year), odometer(_odometer) {}

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

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

int getYear() const { return year; }

int getOdometer() const { return odometer; }

virtual Type getType() = 0;

virtual void checkTireRotation() = 0;

virtual void checkPower() = 0;

void getCarInformation() const {

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

}

};

class GasCar : public Vehicle {

public:

GasCar(std::string _make, std::string _model, int _year, int _odometer)

: Vehicle(_make, _model, _year, _odometer) {}

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

void checkTireRotation() override {

int cycles = odometer / 50000;

int remainder = odometer % 50000;

if (remainder >= 0)

std::cout << "Advice: Rotate tires." << std::endl;

}

void checkPower() override {

if (odometer % 400 == 0)

std::cout << "Advice: Recharge battery." << std::endl;

}

};

class DieselCar : public Vehicle {

public:

DieselCar(std::string _make, std::string _model, int _year, int _odometer)

: Vehicle(_make, _model, _year, _odometer) {}

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

void checkTireRotation() override {

int cycles = odometer / 50000;

int remainder = odometer % 50000;

if (remainder >= 0)

std::cout << "Advice: Rotate tires." << std::endl;

}

void checkPower() override {

if (odometer % 400 == 0)

std::cout << "Advice: Recharge battery." << std::endl;

}

};

class ElectricCar : public Vehicle {

public:

ElectricCar(std::string _make, std::string _model, int _year, int _odometer)

: Vehicle(_make, _model, _year, _odometer) {}

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

void checkTireRotation() override {

int cycles = odometer / 50000;

int remainder = odometer % 50000;

if (remainder >= 0)

std::cout << "Advice: Rotate tires." << std::endl;

}

void checkPower() override {

if (odometer % 400 == 0)

std::cout << "Advice: Recharge battery." << std::endl;

}

};

class Maintenance {

public:

virtual void checkOil() = 0;

virtual Type getFuelType() = 0;

};

class FuelVehicle : public Vehicle, public Maintenance {

public:

FuelVehicle(std::string _make, std::string _model, int _year, int _odometer)

: Vehicle(_make, _model, _year, _odometer) {}

virtual void checkOil() = 0;

virtual Type getFuelType() = 0;

};

class Library {

private:

std::vector<Vehicle*> vehicles;

public:

~Library() {

for (auto vehicle : vehicles) {

delete vehicle;

}

}

void addVehicle(Vehicle* vehicle) {

vehicles.push_back(vehicle);

}

void listInventory() {

for (auto vehicle : vehicles) {

vehicle->getCarInformation();

}

}

void updateVehicle(int index) {

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

vehicles[index]->getCarInformation();

// Provide options to update vehicle details

}

}

void removeVehicle(int index) {

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

delete vehicles[index];

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

}

}

void tireRotation(int index) {

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

vehicles[index]->checkTireRotation();

}

}

void oilChange(int index) {

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

// Logic for oil change

}

}

void recharge(int index) {

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

vehicles[index]->checkPower();

}

}

};

int main() {

Library library;

// Add sample vehicles

library.addVehicle(new GasCar("Toyota", "Corolla", 2015, 60000));

library.addVehicle(new DieselCar("Honda", "Civic", 2018, 45000));

library.addVehicle(new ElectricCar("Tesla", "Model S", 2020, 35000));

int choice;

do {

std::cout << "Menu:" << std::endl;

std::cout << "0. Quit" << std::endl;

std::cout << "1. List inventory" << std::endl;

std::cout << "2. Add vehicle" << std::endl;

std::cout << "3. Update vehicle" << std::endl;

std::cout << "4. Remove vehicle" << std::endl;

std::cout << "5. Tire Rotation" << std::endl;

std::cout << "6. Oil Change" << std::endl;

std::cout << "7. Recharge" << std::endl;

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

std::cin >> choice;

switch(choice) {

case 1:

library.listInventory();

break;

case 2:

// Add vehicle

break;

case 3:

// Update vehicle

break;

case 4:

// Remove vehicle

break;

case 5:

// Tire Rotation

break;

case 6:

// Oil Change

break;

case 7:

// Recharge

break;

case 0:

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

break;

default:

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

}

} while(choice != 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.