Challenge

Project 2

Documentation

Introduction

The provided source code implements an Auto Maintenance Library, which is a simple system for managing and maintaining a collection of vehicles. The library supports different types of vehicles, such as Gas, Diesel, and Electric cars. It includes features for adding, updating, listing, and removing vehicles, as well as specific maintenance checks like tire rotation, oil change, and charger power for electric cars.

Source Code Overview

1. Enumerations

enum Type {

GAS,

DIESEL,

ELECTRIC

};

Type represents the fuel type of a vehicle and includes GAS, DIESEL, and ELECTRIC.

2. Base Class: Vehicle

class Vehicle {

// ... (private members)

public:

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

virtual Type getType() const = 0;

virtual void checkChargerPower() const = 0;

virtual void updateOdometer(int newOdometerReading);

void getCarInformation() const;

void checkTireRotation();

// ... (other member functions)

};

The Vehicle class is an abstract base class that provides common attributes and functions for all types of vehicles.

Derived classes must implement the pure virtual functions getType and checkChargerPower.

3. Derived Classes: GasCar, DieselCar, ElectricCar

class GasCar : public Vehicle {

// ... (additional members and functions)

};

class DieselCar : public Vehicle {

// ... (additional members and functions)

};

class ElectricCar : public Vehicle {

// ... (additional members and functions)

};

These classes represent specific types of vehicles, implementing the pure virtual functions from the Vehicle base class.

4. Derived Class: FuelVehicle

class FuelVehicle : public Vehicle {

// ... (additional members and functions)

};

FuelVehicle is an abstract class derived from Vehicle, introducing additional functionality specific to fuel-based vehicles.

5. Class: AutoMaintenanceLibrary

class AutoMaintenanceLibrary {

// ... (private members)

public:

~AutoMaintenanceLibrary();

void addVehicle(Vehicle* vehicle);

const std::vector<Vehicle*>& listVehicles() const;

void updateVehicle(int index, int newOdometerReading);

void removeVehicle(int index);

// ... (other member functions)

};

AutoMaintenanceLibrary manages a collection of vehicles, providing functionality to add, list, update, and remove vehicles.

6. Main Function

int main() {

// ... (main program logic)

}

The main function provides a simple menu-driven interface for interacting with the AutoMaintenanceLibrary.

Usage

Include the necessary headers:

#include <iostream>

#include <vector>

#include <limits>

Define the Type enumeration and the Vehicle base class along with its derived classes.

Implement the AutoMaintenanceLibrary class to manage the collection of vehicles.

Create an instance of AutoMaintenanceLibrary in the main function and use it to interact with the library.

Example Usage in Main

AutoMaintenanceLibrary library;

// Adding vehicles

library.addVehicle(new GasCar("Toyota", "Camry", 2022, 20000));

library.addVehicle(new DieselCar("Ford", "F-150", 2021, 30000));

library.addVehicle(new ElectricCar("Tesla", "Model 3", 2023, 15000));

// Listing vehicles

library.listVehicles();

// Updating vehicle

library.updateVehicle(0, 25000);

// Removing vehicle

library.removeVehicle(1);

// ... (other menu options)

Menu Options

The main function presents a menu with the following options:

0: Quit

1: List inventory

2: Add vehicle

3: Update vehicle

4: Remove vehicle

5: Tire Rotation

6: Oil Change

7: Recharge

Conclusion

The Auto Maintenance Library provides a modular and extensible framework for managing different types of vehicles and performing maintenance checks. Developers can extend the functionality by adding new vehicle types and implementing specific maintenance tasks. The menu-driven interface in the main function allows users to interact with the library effectively.

Source Code

#include <iostream>

#include <vector>

#include <limits>

enum Type {

GAS,

DIESEL,

ELECTRIC

};

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) {}

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

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

int getYear() const { return year; }

int getOdometer() const { return odometer; }

virtual Type getType() const = 0;

void checkTireRotation() {

int tireCycle = 50000;

int distance = odometer % tireCycle;

if (distance >= 0) {

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

// Update odometer reading after tire rotation

odometer += tireCycle - distance;

} else {

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

}

}

virtual void checkChargerPower() const = 0;

virtual void updateOdometer(int newOdometerReading) {

// Update the odometer reading

odometer = newOdometerReading;

}

void getCarInformation() const {

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

<< ", Odometer Reading: " << odometer << std::endl;

}

};

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 GAS; }

void checkChargerPower() const override {}

void checkOil() const {

// Assuming oil change is recommended every 5000 miles for gas cars

int oilChangeInterval = 5000;

if (odometer % oilChangeInterval == 0) {

std::cout << "Advice: Change oil." << std::endl;

} else {

std::cout << "Oil is in good condition." << std::endl;

}

}

void updateOdometer(int newOdometerReading) override {

// Additional logic for gas cars if needed

Vehicle::updateOdometer(newOdometerReading);

}

};

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 DIESEL; }

void checkChargerPower() const override {}

void checkOil() const {

// Assuming oil change is recommended every 5000 miles for diesel cars

int oilChangeInterval = 5000;

if (odometer % oilChangeInterval == 0) {

std::cout << "Advice: Change oil." << std::endl;

} else {

std::cout << "Oil is in good condition." << std::endl;

}

}

void updateOdometer(int newOdometerReading) override {

// Additional logic for diesel cars if needed

Vehicle::updateOdometer(newOdometerReading);

}

};

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 ELECTRIC; }

void checkTireRotation() {

int tireCycle = 50000;

int distance = odometer % tireCycle;

if (distance >= 0) {

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

} else {

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

}

}

void checkChargerPower() const override {

int distance = 400;

if (odometer % distance == 0) {

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

} else {

std::cout << "Vehicle is good to go." << std::endl;

}

}

void updateOdometer(int newOdometerReading) override {

// Additional logic for electric cars if needed

Vehicle::updateOdometer(newOdometerReading);

}

};

class FuelVehicle : public Vehicle {

public:

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

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

Type getType() const override = 0;

virtual void checkOil() const {

// Assuming oil change is recommended every 5000 miles

int oilChangeInterval = 5000;

if (odometer % oilChangeInterval == 0) {

std::cout << "Advice: Change oil." << std::endl;

} else {

std::cout << "Oil is in good condition." << std::endl;

}

}

void updateOdometer(int newOdometerReading) override {

// Additional logic for fuel vehicles if needed

Vehicle::updateOdometer(newOdometerReading);

}

};

class AutoMaintenanceLibrary {

private:

std::vector<Vehicle*> inventory;

public:

~AutoMaintenanceLibrary() {

for (auto& vehicle : inventory) {

delete vehicle;

}

}

void addVehicle(Vehicle* vehicle) {

inventory.push_back(vehicle);

}

const std::vector<Vehicle*>& listVehicles() const {

for (const auto& vehicle : inventory) {

vehicle->getCarInformation();

}

return inventory;

}

void updateVehicle(int index, int newOdometerReading) {

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

// Check tire rotation and charger power before updating the odometer

inventory[index]->checkTireRotation();

inventory[index]->checkChargerPower();

// Additional maintenance checks based on new odometer reading

if (newOdometerReading > inventory[index]->getOdometer()) {

std::cout << "Additional maintenance checks based on new odometer reading." << std::endl;

}

// Update the odometer reading

inventory[index]->updateOdometer(newOdometerReading);

// Check if the object is of type FuelVehicle before calling checkOil

FuelVehicle* fuelVehicle = dynamic_cast<FuelVehicle*>(inventory[index]);

if (fuelVehicle != nullptr) {

fuelVehicle->checkOil();

}

// Check charger power again after updating the odometer

inventory[index]->checkChargerPower();

// Display the updated car information

inventory[index]->getCarInformation();

} else {

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

}

}

void removeVehicle(int index) {

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

delete inventory[index];

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

} else {

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

}

}

};

int main() {

AutoMaintenanceLibrary library;

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"

<< "Choose an option: ";

int choice;

std::cin >> choice;

if (std::cin.fail()) {

std::cin.clear();

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

std::cout << "Invalid input. Please enter a number.\n";

continue;

}

switch (choice) {

case 0:

return 0;

case 1:

library.listVehicles();

break;

case 2: {

std::string make, model;

int year, odometer;

Type 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 >> odometer;

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

int typeInt;

std::cin >> typeInt;

type = static_cast<Type>(typeInt);

if (type == GAS) {

library.addVehicle(new GasCar(make, model, year, odometer));

} else if (type == DIESEL) {

library.addVehicle(new DieselCar(make, model, year, odometer));

} else if (type == ELECTRIC) {

library.addVehicle(new ElectricCar(make, model, year, odometer));

} else {

std::cout << "Invalid fuel type." << std::endl;

}

break;

}

case 3: {

int index, newOdometerReading;

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

std::cin >> index;

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

std::cin >> newOdometerReading;

library.updateVehicle(index, newOdometerReading);

break;

}

case 4: {

int index;

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

std::cin >> index;

library.removeVehicle(index);

break;

}

case 5: {

int index;

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

std::cin >> index;

const std::vector<Vehicle*>& vehicles = library.listVehicles();

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

vehicles[index]->checkTireRotation();

} else {

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

}

break;

}

case 6: {

int index;

std::cout << "Enter the index of the vehicle for oil change check: ";

std::cin >> index;

const std::vector<Vehicle*>& vehicles = library.listVehicles();

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

// Check if the object is of type FuelVehicle before calling checkOil

FuelVehicle* fuelVehicle = dynamic_cast<FuelVehicle*>(vehicles[index]);

if (fuelVehicle != nullptr) {

fuelVehicle->checkOil();

} else {

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

}

} else {

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

}

break;

}

case 7: {

int index;

std::cout << "Enter the index of the electric vehicle for recharge check: ";

std::cin >> index;

const std::vector<Vehicle*>& vehicles = library.listVehicles();

if (index >= 0 && index < vehicles.size() && vehicles[index]->getType() == ELECTRIC) {

vehicles[index]->checkChargerPower();

} else {

std::cout << "Invalid index or not an electric vehicle." << std::endl;

}

break;

}

default:

std::cout << "Invalid option. Please choose a valid option.\n";

break;

}

}

return 0;

}

Output

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.