Challenge

Project 3

#include
#include
#include
#include
#include


// Fuel types enumeration
enum Type {
 GAS,
 DIESEL,
 ELECTRIC
};

// Vehicle class
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) {}

// Getter and setter functions
 std::string getMake() const { return make; }
std::string getModel() const { return model; }
 int getYear() const { return year; }
 int getOdometer() const { return odometer; }

 void setMake(const std::string& make) { this->make = make; }
 void setModel(const std::string& model) { this->model = model; }
 void setYear(int year) { this->year = year; }
 void setOdometer(int odometer) { this->odometer = odometer; }

 // Maintenance activities
 virtual void checkTireRotation() const = 0;
virtual void checkChargerPower() const = 0;

 // Get the fuel type
 virtual Type getType() const = 0;

 // Display car information
 virtual void getCarInformation() const {
 std::cout << "Make: " << make> }
};

// GasCar class
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 checkTireRotation() const override {
 int remainder = odometer P000;
 if (remainder >= 0) {
 std::cout << "Gas car needs tire rotation" << std> } 
 else {
 std::cout << "Gas car tires are good to go" << std> }
 }

 void checkChargerPower() const override {
 std::cout << "Gas car does not need a recharge" << std> }
};

// DieselCar class
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 checkTireRotation() const override {
 int remainder = odometer P000;
 if (remainder >= 0) {
 std::cout << "Diesel car needs tire rotation" << std> } else {
 std::cout << "Diesel car tires are good to go" << std> }
 }

 void checkChargerPower() const override {
 std::cout << "Diesel car does not need a recharge" << std> }
};

// ElectricCar class
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() const override {
 int remainder = odometer P000;
 if (remainder >= 0) {
 std::cout << "Electric car needs tire rotation" << std> }
 else {
 std::cout << "Electric car tires are good to go" << std> }
 }

 void checkChargerPower() const override {
 if (odometer @0 == 0) {
 std::cout << "Electric car needs to be recharged" << std> } else {
 std::cout << "Electric car has enough power to continue" << std> }
 }
};

// FuelVehicle abstract class
class FuelVehicle : public Vehicle {
public:
 FuelVehicle(const std::string& make, const std::string& model, int year, int odometer)
 : Vehicle(make, model, year, odometer) {}

 // Pure virtual function to get the fuel type
 virtual Type getFuelType() const = 0;

 void checkOil() const {
 std::cout << "Checking oil for " << getFuelTypeString> }

private:
 std::string getFuelTypeString() const {
 switch (getFuelType()) {
 case GAS:
 return "Gas";
 case DIESEL:
 return "Diesel";
 case ELECTRIC:
 return "Electric";
 }
 return "Unknown";
 }
};

// Helper function to retrieve user input
int getUserInputInt() {
 int input;
 while (!(std::cin >> input)) {
 std::cin.clear();
 std::cin.ignore(std::numeric_limits::max(), '\n');
 std::cout << "Invalid input Please enter a valid number: ";
 }
 return input;
}

// Helper function to retrieve user input for vehicle attributes
std::string getUserInputString() {
 std::string input;
 while (std::cin.peek() == '\n') {
 std::cin.ignore();
 }
 std::getline(std::cin, input);
 return input;
}

// Main function
int main() {
 std::vector inventory;
 int choice;
 int inventoryempty;


 do {
std::cout << " . Select an option:" << std> std::cout << " . 0 Quit" << std>std::cout << " . 1 List inventory" << std>std::cout << " . 2 Add vehicle" << std> std::cout << " . 3 Update vehicle" << std> std::cout << " . 4 Remove vehicle" << std> std::cout << " . 5 Tire Rotation" << std> std::cout << " . 6 Oil Change" << std> std::cout << " . 7 Recharge" << std>

choice = getUserInputInt();

switch (choice) {
 case 0:
std::cout << "Exiting program" << std> break;
case 1:
if (inventoryempty=0) {
std::cout << "Inventory is empty" << std>}
else {
 std::cout << "Current Inventory: " << std>for (const auto& vehicle : inventory) {
 vehicle->getCarInformation();
}
}
break;
case 2: {
 std::cout << "Enter make: ";
 std::string make = getUserInputString();
 std::cout << "Enter model: ";
std::string model = getUserInputString();
 std::cout << "Enter year: ";
 int year = getUserInputInt();
 std::cout << "Enter odometer reading: ";
int odometer = getUserInputInt();

 std::cout << "Select fuel type (0-GAS, 1-DIESEL, 2-ELECTRIC): ";
int fuelTypeInput = getUserInputInt();
 Type fuelType;
 switch (fuelTypeInput) {
 case 0:
fuelType = GAS;
break;
case 1:
fuelType = DIESEL;
break;
case 2:
fuelType = ELECTRIC;
break;
default:
std::cout << "Invalid fuel type Adding GAS car by default" << std>fuelType = GAS;
break;
}

Vehicle* newVehicle;
switch (fuelType) {
case GAS:
newVehicle = new GasCar(make, model, year, odometer);
break;
case DIESEL:
newVehicle = new DieselCar(make, model, year, odometer);
break;
case ELECTRIC:
newVehicle = new ElectricCar(make, model, year, odometer);
break;
default:
std::cout << "Unknown fuel type Adding GAS car by default" << std>newVehicle = new GasCar(make, model, year, odometer);
break;
}

inventory.push_back(newVehicle);
std::cout << "Vehicle added to inventory" << std>break;
}
case 3: {
if (inventory.empty()) {
std::cout << "Inventory is empty Nothing to update" << std>} else {
std::cout << "Select a vehicle to update (0-" << inventory>int index = getUserInputInt();
if (index >= 0 && index < inventory>std::cout << "Enter updated make: ";
inventory[index]->setMake(getUserInputString());
std::cout << "Enter updated model: ";
inventory[index]->setModel(getUserInputString());
std::cout << "Enter updated year: ";
inventory[index]->setYear(getUserInputInt());
std::cout << "Enter updated odometer reading: ";
inventory[index]->setOdometer(getUserInputInt());
std::cout << "Vehicle updated" << std>} else {
std::cout << "Invalid vehicle index" << std>}
}
break;
}
case 4: {
if (inventory.empty()) {
std::cout << "Inventory is empty Nothing to remove" << std>} else {
std::cout << "Select a vehicle to remove (0-" << inventory>int index = getUserInputInt();
if (index >= 0 && index < inventory>delete inventory[index];
inventory.erase(inventory.begin() + index);
std::cout << "Vehicle removed from inventory" << std>} else {
std::cout << "Invalid vehicle index" << std>}
}
break;
}
case 5: {
if (inventory.empty()) {
std::cout << "Inventory is empty Nothing to check for tire rotation" << std>} else {
std::cout << "Select a vehicle to check for tire rotation (0-" << inventory>int index = getUserInputInt();
if (index >= 0 && index < inventory>inventory[index]->checkTireRotation();
} else {
std::cout << "Invalid vehicle index" << std>}
}
break;
}
case 6: {
if (inventory.empty()) {
std::cout << "Inventory is empty Nothing to check for oil change" << std>} else {
std::cout << "Select a vehicle to check for oil change (0-" << inventory>int index = getUserInputInt();
if (index >= 0 && index < inventory>if (dynamic_cast(inventory[index])) {
dynamic_cast(inventory[index])->checkOil();
} else {
std::cout << "Oil change not applicable for Electric car" << std>}
} else {
std::cout << "Invalid vehicle index" << std>}
}
break;
}
case 7: {
if (inventory.empty()) {
std::cout << "Inventory is empty Nothing to check for recharge" << std>} else {
std::cout << "Select a vehicle to check for recharge (0-" << inventory>int index = getUserInputInt();
if (index >= 0 && index < inventory>inventory[index]->checkChargerPower();
} else {
std::cout << "Invalid vehicle index" << std>}
}
break;
}
default:
std::cout << "Invalid option Please try again" << std>break;
 }
} while (choice != 0);

// Clean up memory
for (const auto& vehicle : inventory) {
delete vehicle;
}

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.