Project 3
Here is the code :
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
enum Type
{
PETROL,
DIESEL,
ELECTRIC
};
class Vehicle
{
protected:
string make;
string model;
int year;
int odometer;
public:
Vehicle(const string &make, const string &model, int year, int odometer) : make(make),
model(model),
year(year),
odometer(odometer) {}
string getMake()
{
return make;
}
string getModel()
{
return model;
}
int getYear()
{
return year;
}
int getOdometer()
{
return odometer;
}
void setMake(const string &make)
{
this->make = make;
}
void setModel(const string &model)
{
this->model = model;
}
void setYear(int year)
{
this->year = year;
}
void setOdometer(int odometer)
{
this->odometer = odometer;
}
virtual Type getType() = 0;
void checkTireRotation()
{
int distanceRun = odometer;
if (distanceRun >= 50000)
{
cout << "Tire rotation needed." << endl;
}
else
{
cout << "Tire rotation not needed." << endl;
}
}
void getCarInformation()
{
cout << "Make: " << make << endl;
cout << "Model: " << model << endl;
cout << "Year: " << year << endl;
cout << "Odometer Reading: " << odometer << " km" << endl;
}
};
class PetrolCar : public Vehicle
{
public:
PetrolCar(const string &make, const string &model, int year, int odometer) : Vehicle(make, model, year, odometer) {}
Type getType() override { return Type::PETROL; }
string getFuelType() { return "Petrol"; }
};
class DieselCar : public Vehicle
{
public:
DieselCar(const string &make, const string &model, int year, int odometer) : Vehicle(make, model, year, odometer) {}
Type getType() override { return Type::DIESEL; }
string getFuelType() { return "Diesel"; }
};
class ElectricCar : public Vehicle
{
public:
ElectricCar(const string &make, const string &model, int year, int odometer) : Vehicle(make, model, year, odometer) {}
Type getType() override { return Type::ELECTRIC; }
string getFuelType() { return "Electric"; }
void checkChargerPower()
{
int distanceRun = odometer % 400; // Recharge needed every 400 km
if (distanceRun >= 0)
{
cout << "Charger power check needed." << endl;
}
else
{
cout << "Charger power check not needed." << endl;
}
}
};
void displayMenu()
{
cout << "====================================\n";
cout << " VEHICLE INVENTORY \n";
cout << "====================================\n";
cout << " 0. Quit.\n";
cout << " 1. List inventory.\n";
cout << " 2. Add vehicle.\n";
cout << " 3. Update vehicle.\n";
cout << " 4. Remove vehicle.\n";
cout << " 5. Tire Rotation.\n";
cout << " 6. Oil Change.\n";
cout << " 7. Recharge.\n";
cout << "====================================\n";
cout << "Enter your choice: ";
}
int main()
{
int Vehicle_Type, Oddometer, year;
string maker, model;
cout << "Welcome to Vehicle maintanence" << endl;
Starting:
displayMenu();
int x;
cin >> x;
if (x == 0)
{
exit(0);
}
else if (x == 1)
{
ifstream inFile;
inFile.open("inventory2.txt", ios::in);
if (inFile.is_open())
{
string line;
while (getline(inFile, line))
{
cout << line << endl;
}
inFile.close();
}
else
{
cout << "Failed to open the file." << endl;
}
}
else if (x == 2)
{
Input:
cout << "Which type of vehicle you have? " << endl
<< "Enter 1 for Petrol" << endl
<< "Enter 2 for Deisel" << endl
<< "Enter 3 for Electric" << endl;
cin >> Vehicle_Type;
cout << "Type Name of the maker of the vehicle: " << endl;
cin >> maker;
cout << "What is the model of the vehicle? " << endl;
cin >> model;
cout << "What is the the purchase year " << endl;
cin >> year;
cout << "Reading in oddometer ? " << endl;
cin >> Oddometer;
if (Vehicle_Type = 1)
{
PetrolCar newcar(maker, model, year, Oddometer);
newcar.getCarInformation();
newcar.checkTireRotation();
ofstream outFile;
outFile.open("Inventory2.txt", ios::out);
if (outFile.is_open())
{
outFile << "The Maker of the car is " << maker << endl;
outFile << "The model of the car is " << model << endl;
outFile << "The year of purchase of the car is " << year << endl;
outFile << "The oddometer rating of the car is " << Oddometer << endl;
outFile.close();
cout << "Your Inventory is updated" << endl;
}
else
{
cout << "Failed to open the file." << endl;
}
}
if (Vehicle_Type = 2)
{
DieselCar newcar(maker, model, year, Oddometer);
newcar.getCarInformation();
newcar.checkTireRotation();
ofstream outFile;
outFile.open("Inventory2.txt", ios::out);
if (outFile.is_open())
{
outFile << "The Maker of the car is " << maker << endl;
outFile << "The model of the car is " << model << endl;
outFile << "The year of purchase of the car is " << year << endl;
outFile << "The oddometer rating of the car is " << Oddometer << endl;
outFile.close();
cout << "Your Inventory is updated" << endl;
}
else
{
cout << "Failed to open the file." << endl;
}
}
if (Vehicle_Type = 3)
{
ElectricCar newcar(maker, model, year, Oddometer);
newcar.getCarInformation();
newcar.checkTireRotation();
newcar.checkChargerPower();
ofstream outFile;
outFile.open("Inventory2.txt", ios::out);
if (outFile.is_open())
{
outFile << "The Maker of the car is " << maker << endl;
outFile << "The model of the car is " << model << endl;
outFile << "The year of purchase of the car is " << year << endl;
outFile << "The oddometer rating of the car is " << Oddometer << endl;
outFile.close();
cout << "Your Inventory is updated" << endl;
}
else
{
cout << "Failed to open the file." << endl;
}
}
}
else if (x == 3)
{
int update_reading;
cout << "Do you want to update the vehicle reading ?" << endl;
cout << "Enter 1 for yes and 2 for NO" << endl;
cin >> update_reading;
if (update_reading == 1)
{
goto Input;
}
else
{
cout << "Thank you" << endl;
}
}
else if (x == 4)
{
ofstream file("inventory2.txt", ios::out | ios::trunc);
if (file.is_open())
{
file.close();
cout << "Inventory cleared successfully." << endl;
}
else
{
cout << "Failed to open the Inventory." << endl;
}
}
else if (x==6 || x==7)
{
goto Input;
}
else
{
cout<<"Try again and enter correct details"<<endl;
goto Starting;
}
return 0;
}