Challenge

Project 3

Project 3 ans-

�

�

#include //this is C++ Standard Library headers: for input and output operations
#include //her we used the vector header file because we used vector term keyword in our program
using namespace std;// this is used for commonly used object and functions in c++ language.

enum Type // it is enumerated data type i.e. named as 'type' , here we used all types of our cars and by using enum we are denoted that cars integer type values 0 1 and 2 respectively.
{
��� GAS,
��� DIESEL,
��� ELECTRIC
};

class Vehicle // here we create class for entering the vehicle details
{
protected:
��� string make;
��� string model;
��� int year;
��� int odometer;

public:
��� Vehicle() : make(), model(), year(0), odometer(0) {}// it is constructor that making the protected class data's to the public use

��� void setMake(string _make) // here we used set function for making the protected class members to be used.
��� {
������� make = _make;
��� }
��� void setModel(string _model) {
������� model = _model;
��� }
��� void setYear(int _year) {
������� year = _year;
��� }
��� void setOdometer(int _odometer) {
������� odometer = _odometer;
��� }

��� string getMake() // here we used get function for using the protected class members.
��� {
������� return make;
��� }
��� string getModel() {
������� return model;
��� }
��� int getYear() {
������� return year;
��� }
��� int getOdometer() {
������� return odometer;
��� }

��� virtual Type getType() = 0;//here by using the virtual we are defining that this is the our base function i.e. going to be overridden

��� virtual void checkOil() //here by using the virtual we are defining that this is the our base function i.e. going to be overridden
��� {

��� }

��� virtual void checkChargerPower() //here by using the virtual we are defining that this is the our base function i.e. going to be overridden
��� {

��� }

��� void checkTireRotation() // this function is used to check our tire condition
��� {
������� int tireCycle = 50000;//this is the lifecycle of the tire i.e. already given in the question
������� int distanceRun = tireCycle - odometer;
������� if (distanceRun < 0)
������� {
����������� cout << "Advice: change the tires." << std::endl;
������� } else {
����������� cout << "Tires are in good condition." << std::endl;
������� }
��� }

��� void getCarInformation() //this function used to showing the information of the cars i.e. provided by the users.
��� {
������� cout << "Make: " << make << std::endl;
������� cout << "Model: " << model << std::endl;
������� cout << "Year: " << year << std::endl;
������� cout << "Odometer Reading: " << odometer << " km" << std::endl;
��� }
};

class GasCar : public Vehicle // This line is defining a new class called GasCar, which inherits from another class named Vehicle.
//It means that GasCar will have all the properties and methods of Vehicle, and you can add more to it.
{
public:
��� Type getType() //This line defines a member function (method) called getType for the GasCar class. This function returns a value of type Type
��� {
������� return GAS;
��� }

��� void checkOil() override // It is marked with the override keyword, which indicates that it is intended to replace a function with the same name in the base class Vehicle.
��� {
������� cout << "Checking oil for " << make << " car." << endl;// we used this output for only gas car and diesel car
��� }
};

class DieselCar : public Vehicle // similarly we used for diesel car and electric car
{
public:
��� Type getType() {
������� return DIESEL;
��� }

��� void checkOil() override {
������� cout << "Checking oil for " << make << " car." << endl;
��� }
};

class ElectricCar : public Vehicle {
public:
��� Type getType() {
������� return ELECTRIC;
��� }

��� void checkChargerPower() override
��� {
������� int rechargeDistance = 400;// this recharge distance is already defined in the question
������� if (odometer % rechargeDistance == 0)// by using this operator we will get only remainder, if it gets 0 that means car needs to be charged
������� {
����������� cout << "Advice: Recharge the vehicle's power." << std::endl;
������� } else {
����������� cout << "The vehicle's power is sufficient." << std::endl;
������� }
��� }
};

class CarInventory //This line defines a new class called CarInventory.
{
public:
��� vector vehicles;//This line declares a vector named vehicles that will store pointers to objects of the Vehicle class. It's used to manage a collection of vehicle objects.

��� void addVehicle(Vehicle* vehicle) // It takes a pointer to a Vehicle object as an argument and adds it to the vehicles vector.
��� {
������� vehicles.push_back(vehicle);// Inside the addVehicle function, this line adds the given vehicle pointer to the end of the vehicles vector.
��� }

��� void listVehicles() // It's responsible for listing the information about the vehicles in the inventory.
��� {
������� cout << "Vehicle Inventory:" << endl;
������� for (int i = 0; i < vehicles.size(); ++i) // here we are using vector predefined function .size , it is used when we don't know the actual size of our data type
������� {
����������� cout << i + 1 << "_ _ _ _ _ _" << endl;
����������� vehicles[i]->getCarInformation();// This line calls the getCarInformation method on the i-th vehicle to display information about that vehicle.
����������� cout << "Type: " << vehicles[i]->getType() << endl;
����������� vehicles[i]->checkTireRotation();//t calls the checkTireRotation method for the vehicle to check if it needs a tire rotation.
����������� cout << endl;
������� }
��� }

��� void updateVehicle(int index, int newOdometer, string Newmake, string Newmodel)//It takes an index to specify which vehicle to update and new values for the odometer reading, make, and model of the vehicle.
��� {
������� if (index >= 0 && index < vehicles.size()) //This line checks if the index is within a valid range, ensuring that the index corresponds to an existing vehicle in the inventory.
������� {
����������� vehicles[index]->checkTireRotation();// after update now we are going to check every details about our vehicle, starting with tire.
����������� vehicles[index]->checkChargerPower();
����������� vehicles[index]->checkOil();
����������� vehicles[index]->setOdometer(newOdometer);
����������� vehicles[index]->setMake(Newmake);
����������� vehicles[index]->setModel(Newmodel);
����������� cout << "Vehicle updated." << endl;
������� } else {
����������� cout << "Invalid index. Vehicle not updated." << endl;
������� }
��� }

��� void removeVehicle(int index) //This line defines a member function named removeVehicle that takes an index as an argument.
��� //The purpose of this function is to remove a vehicle from the vehicles vector in the CarInventory class.
��� {
������� if (index >= 0 && index < vehicles.size())
{
����������� delete vehicles[index];//This line deletes the vehicle object at the specified index. This is important to free up memory associated with the object, as the vector stores pointers to objects.
����������� vehicles.erase(vehicles.begin() + index);//: Here, it removes the pointer to the vehicle from the vehicles vector, effectively removing it from the inventory.
����������� cout << "Vehicle removed from inventory." << endl;
������� } else {
����������� cout << "Invalid index. Vehicle not removed." << endl;
������� }
��� }
};

int main() //here inside the main function we are going to ask to the user about what we wants to do with our vehicle
{
��� CarInventory inventory;// here we are giving an object to the carinventory class

��� while (true) // here by using true as statement we are going to run that while loop again and again
��� {
������� cout << "Menu:\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";

������� int choice;
������� cin >> choice;

������� if (choice == 0) // this condition is used for quit from the program
������� {
����������� break;
������� }

������� switch (choice) //here we are using the switch case for all written menu accordingly
������� {
����������� case 1:
��������������� inventory.listVehicles();// here we are calling for the inventory class with the list of the vehicles details
��������������� break;
����������� case 2: {
��������������� string make, model;
��������������� int year, odometer, type;
��������������� cout << "Enter vehicle details:" << endl;

��������������� cout << "Make: ";
��������������� cin >> make;

��������������� cout << "Model: ";
��������������� cin >> model;

��������������� cout << "Year: ";
��������������� cin >> year;
��������������� cout << "Odometer Reading: ";
��������������� cin >> odometer;
��������������� cout << "Type (0 - Gas, 1 - Diesel, 2 - Electric): ";
��������������� cin >> type;

��������������� Vehicle* newVehicle = nullptr;// here we gave the null value for the newvehicle object of the vehicle pointer , that defines to us that initial there is nothing inside the vehicle pointer.

��������������� if (type == 0) {
������������������� newVehicle = new GasCar;//this line dynamically allocates memory to create a new GasCar object and assigns its memory address to the newVehicle pointer.
������������������� //This means that newVehicle now points to a GasCar object.
��������������� } else if (type == 1) {
������������������� newVehicle = new DieselCar;
��������������� } else if (type == 2) {
������������������� newVehicle = new ElectricCar;
��������������� } else {
������������������� cout << "Invalid type. Vehicle not added." << endl;
������������������� break;
��������������� }

��������������� newVehicle->setMake(make);
��������������� newVehicle->setModel(model);
��������������� newVehicle->setYear(year);
��������������� newVehicle->setOdometer(odometer);

��������������� inventory.addVehicle(newVehicle);//here after giving all the data to the vehicle we are going to add the vehicle details.
��������������� cout << "Vehicle added to inventory." << endl;
��������������� break;
����������� }
����������� case 3: {
��������������� int index, newOdometer;
��������������� string Newmake, Newmodel;
��������������� cout << "Enter the number of the vehicle to update: ";
��������������� cin >> index;
��������������� cout << "Enter the new odometer reading: ";
��������������� cin >> newOdometer;

��������������� cout << "Enter the new car make: ";
��������������� cin >> Newmake;

��������������� cout << "Enter the new model name: ";
��������������� cin >> Newmodel;
��������������� inventory.updateVehicle(index - 1, newOdometer, Newmake, Newmodel);// here we are calling for the inventory class with there updated data as an argument
��������������� //here we used index-1 because our initial car enum type is 0.
��������������� break;
����������� }
����������� case 4: {
��������������� int index;
��������������� cout << "Enter the index of the vehicle to remove: ";
��������������� cin >> index;
��������������� inventory.removeVehicle(index - 1);
��������������� break;
����������� }
����������� case 5: {
��������������� cout << "Checking tire rotation for all vehicles:" << endl;
��������������� inventory.listVehicles();
��������������� break;
����������� }
����������� case 6: {
��������������� cout << "Checking oil change for all fuel-based vehicles:" << endl;
��������������� for (auto vehicle : inventory.vehicles) // here we used auto keyword because here we don't even know the data type of the vehicle class because we used different types of data inside that vehicle class
��������������� {
������������������� vehicle->checkOil();
��������������� }

��������������� break;
����������� }
����������� case 7: {
��������������� cout << "Checking recharge for electric vehicles:" << endl;
��������������� for (auto vehicle : inventory.vehicles)// similarly here we used auto for the vehicle details
��������������� {
������������������� vehicle->checkChargerPower();
��������������� }
��������������� break;
����������� }
����������� default:
��������������� cout << "Invalid choice. Please try again." << endl;
��������������� break;
������� }
��� }

��� 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.