Challenge

Project 3

#include <iostream>
#include <vector>
#include <string>

enum class 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() = 0;
virtual std::string getFuelType() = 0;
};

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() override { return Type::GAS; }
std::string getFuelType() override { return "Petrol"; }
};

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() override { return Type::DIESEL; }
std::string getFuelType() override { return "Diesel"; }
};

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() override { return Type::ELECTRIC; }
std::string getFuelType() override { return "Electricity"; }

bool checkTireRotation() {
int tireCycle = 50000;
int distance = getOdometer();
int remainder = distance % tireCycle;

if (remainder >= 0) {
std::cout << "It's time to rotate the tires.\n";
return true;
} else {
std::cout << "Tire rotation is not required.\n";
return false;
}
}

bool checkPower() {
int distance = getOdometer();
int rechargeDistance = 400;

if (distance % rechargeDistance == 0) {
std::cout << "Battery recharge is required.\n";
return true;
} else {
std::cout << "No need to recharge the battery.\n";
return false;
}
}
};

class MaintenanceTracker {
private:
std::vector<Vehicle*> vehicles;

public:
void addVehicle(Vehicle* vehicle) {
vehicles.push_back(vehicle);
}

void removeVehicle(int index) {
if (index >= 0 && index < vehicles.size()) {
delete vehicles[index];
vehicles.erase(vehicles.begin() + index);
}
}

void updateVehicle(int index, int newOdometer) {
if (index >= 0 && index < vehicles.size()) {
vehicles[index]->odometer = newOdometer;
}
}

void listVehicles() const {
std::cout << "Vehicle Inventory:\n";
for (int i = 0; i < vehicles.size(); ++i) {
std::cout << "Vehicle #" << i + 1 << "\n";
std::cout << "Make: " << vehicles[i]->getMake() << "\n";
std::cout << "Model: " << vehicles[i]->getModel() << "\n";
std::cout << "Year: " << vehicles[i]->getYear() << "\n";
std::cout << "Odometer: " << vehicles[i]->getOdometer() << " km\n";
std::cout << "Fuel Type: " << vehicles[i]->getFuelType() << "\n";
std::cout << "---------------------------------\n";
}
}
};

int main() {
MaintenanceTracker tracker;

while (true) {
std::cout << "Menu:\n";
std::cout << " 0. Quit.\n";
std::cout << " 1. List inventory.\n";
std::cout << " 2. Add vehicle.\n";
std::cout << " 3. Update vehicle.\n";
std::cout << " 4. Remove vehicle.\n";
std::cout << " 5. Check Tire Rotation.\n";
std::cout << " 6. Check Oil Change.\n";
std::cout << " 7. Check Power.\n";

int choice;
std::cout << "Enter your choice: ";
std::cin >> choice;

if (choice == 0) {
break;
} else if (choice == 1) {
tracker.listVehicles();
} else if (choice == 2) {
std::string make, model;
int year, odometer;
int type;

std::cout << "Enter make: ";
std::cin.ignore();
std::getline(std::cin, make);

std::cout << "Enter model: ";
std::getline(std::cin, model);

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

std::cout << "Enter odometer reading: ";
std::cin >> odometer;

std::cout << "Enter fuel type (0 - GAS, 1 - DIESEL, 2 - ELECTRIC): ";
std::cin >> type;

Vehicle* vehicle;
switch (type) {
case 0:
vehicle = new GasCar(make, model, year, odometer);
break;
case 1:
vehicle = new DieselCar(make, model, year, odometer);
break;
case 2:
vehicle = new ElectricCar(make, model, year, odometer);
break;
default:
std::cout << "Invalid fuel type. Vehicle not added.\n";
continue;
}

tracker.addVehicle(vehicle);
std::cout << "Vehicle added successfully.\n";
} else if (choice == 3) {
int index, newOdometer;
std::cout << "Enter the index of the vehicle to update: ";
std::cin >> index;

std::cout << "Enter the new odometer reading: ";
std::cin >> newOdometer;

tracker.updateVehicle(index - 1, newOdometer);
std::cout << "Vehicle updated successfully.\n";
} else if (choice == 4) {
int index;
std::cout << "Enter the index of the vehicle to remove: ";
std::cin >> index;

tracker.removeVehicle(index - 1);
std::cout << "Vehicle removed successfully.\n";
} else if (choice == 5) {
int index;
std::cout << "Enter the index of the vehicle to check tire rotation: ";
std::cin >> index;

ElectricCar* electricCar = dynamic_cast<ElectricCar*>(tracker.getVehicle(index - 1));
if (electricCar) {
electricCar->checkTireRotation();
} else {
std::cout << "Tire rotation check not applicable for this vehicle.\n";
}
} else if (choice == 6) {
// Check oil change
// Not implemented in this example
} else if (choice == 7) {
int index;
std::cout << "Enter the index of the electric vehicle to check power: ";
std::cin >> index;

ElectricCar* electricCar = dynamic_cast<ElectricCar*>(tracker.getVehicle(index - 1));
if (electricCar) {
electricCar->checkPower();
} else {
std::cout << "Power check not applicable for this vehicle.\n";
}
} else {
std::cout << "Invalid choice. Please try again.\n";
}
}

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.