Project 3
#include <iostream>
#include <limits>
using namespace std;
// Creating the Base class- Vehicle:
class Vehicle{
protected:
string make;
string model;
int year;
int odo_reading;
public:
Vehicle(string make, string model, int year, int odo_reading){
this->make=make;
this->model=model;
this->year=year;
this->odo_reading=odo_reading;
}
virtual ~Vehicle() {};
// Setter and getter functions for make:
void setMake(string make){
this ->make=make;
}
string getMake(){
return make;
}
// Setter and getter functions for model:
void setModel(string model){
this ->model=model;
}
string getModel(){
return model;
}
// Setter and getter functions for year:
void setYear(int year){
this ->year=year;
}
int getYear(){
return year;
}
// Setter and getter functions for odo_reading:
void setReading(int odo_reading){
this ->odo_reading=odo_reading;
}
int getOdoReading(){
return odo_reading;
}
virtual void check_car_rotation(){}
virtual void checkChargerPower(){}
void get_car_information(){
cout<<"Maker of Car is:"<<make<<endl;
cout<<"Model of Car is:"<<model<<endl;
cout<<"Year of Car is:"<<year<<endl;
cout<<"Odometer reading of Car is:"<<odo_reading<<endl;
}
virtual string getType()=0;
virtual void oilChange(){}
};
// Creating the sub class- GasCar;
class GasCar: public Vehicle{
public:
GasCar(string make, string model, int year, int odo_reading) : Vehicle(make, model, year, odo_reading){
}
string getType(){
return "Gas Car";
};
void getFuelType(){}
void oilChange(){
int c;
cout<<"Enter % of oil in the display"<<endl;
cin>>c;
if(c>=0 && c<50){
cout<<"Oil change required!"<<endl;
}
else if(c>=50 && c<=100){
cout<<"Oil change not required!"<<endl;
}
else{
cout<<"Wrong input"<<endl;
}
}
};
// Creating the sub class- DieselCar;
class DieselCar: public Vehicle{
public:
DieselCar(string make, string model, int year, int odo_reading) : Vehicle(make, model, year, odo_reading){
}
string getType(){
return "Diesel Car";
}
void getFuelType(){}
void oilChange(){
int c;
cout<<"Enter % of oil in the display"<<endl;
cin>>c;
if(c>=0 && c<50){
cout<<"Oil change required!"<<endl;
}
else if(c>=50 && c<=100){
cout<<"Oil change not required!"<<endl;
}
else{
cout<<"Wrong input"<<endl;
}
}
};
// Creating the sub class- ElectricCar;
class ElectricCar: public Vehicle{
public:
ElectricCar(string make, string model, int year, int odo_reading) : Vehicle(make, model, year, odo_reading){
}
string getType(){
return "Electric Car";
}
void check_car_rotation(){
int d;
cout<<"Enter the distance run by the car"<<endl;
cin>>d;
if(d>=50000){
cout<<"Rotate tyres"<<endl;
}
else{
cout<<"Good to go. Remaining distance is:"<<(50000-d)<<endl;
}
}
void checkChargerPower(){
int c;
cout<<"Enter the distance run by the car after last charge"<<endl;
cin>>c;
if(c>=400){
cout<<"Recharge the car!"<<endl;
}
else{
cout<<"Good to go"<<endl;
}
}
};
int i = 0;
Vehicle* cars[100];
void addCar() {
string make, model;
int year, odometer_reading;
cout << "Enter make: ";
cin >> make;
cout << "Enter model: ";
cin >> model;
cout << "Enter year: ";
cin >> year;
if (cin.fail()) {
cout << "Invalid input for year. Car not added." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
cout << "Enter odometer reading: ";
cin >> odometer_reading;
if (cin.fail()) {
cout << "Invalid input for odometer reading. Car not added." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
string fuel_type;
cout << "Enter fuel type ('gas', 'diesel', 'electric'): ";
cin >> fuel_type;
if (fuel_type == "gas") {
cars[i] = new GasCar(make, model, year, odometer_reading);
} else if (fuel_type == "diesel") {
cars[i] = new DieselCar(make, model, year, odometer_reading);
} else if (fuel_type == "electric") {
cars[i] = new ElectricCar(make, model, year, odometer_reading);
} else {
cout << "Invalid fuel type entered. Car not added." << endl;
return;
}
i++;
}
void updateCar(){
cout<<"Enter the car number in the list to update"<<endl;
int car_no;
cin>>car_no;
string fuel_type;
cout << "Enter fuel type ('gas', 'diesel', 'electric'): ";
cin >> fuel_type;
string make, model;
int year, odometer_reading;
cout << "Enter make: ";
cin >> make;
cout << "Enter model: ";
cin >> model;
cout << "Enter year: ";
cin >> year;
cout << "Enter odometer reading: ";
cin >> odometer_reading;
if (fuel_type == "gas") {
cars[car_no] = new GasCar(make, model, year, odometer_reading);
} else if (fuel_type == "diesel") {
cars[car_no] = new DieselCar(make, model, year, odometer_reading);
} else if (fuel_type == "electric") {
cars[car_no] = new ElectricCar(make, model, year, odometer_reading);
} else {
cout << "Invalid fuel type entered. Car not added." << endl;
// continue;
}
}
void removeCar() {
int car_no;
cout << "Enter the car number in the list to remove: ";
cin >> car_no;
if (car_no < 0 || car_no >= i) {
cout << "Invalid car number entered. No car removed." << endl;
return;
}
delete cars[car_no];
for (int j = car_no; j < i - 1; j++) {
cars[j] = cars[j + 1];
}
cars[i - 1] = nullptr;
i--;
cout << "Car removed successfully." << endl;
}
void tyreRotaion(){
int car_no;
cout<<"enter the car number in the list"<<endl;
cin>>car_no;
if(cars[car_no]->getType() == "Electric Car"){
cars[car_no]->check_car_rotation();
}
else{
cout<<"wrong type"<<endl;
}
}
void CheckOil(){
int car_no;
cout<<"enter the car number in the list"<<endl;
cin>>car_no;
if(cars[car_no]->getType() == "Electric Car"){
cout<<"wrong type"<<endl;
}
else{
cars[car_no]->oilChange();
}
}
void recharge(){
int car_no;
cout<<"enter the car number in the list"<<endl;
cin>>car_no;
if(cars[car_no]->getType() == "Electric Car"){
cars[car_no]->checkChargerPower();
}
else{
cout<<"wrong type"<<endl;
}
}
int main() {
while (true) {
string s;
cout << "Enter the operation to be performed ('quit', 'add', 'list', 'update', 'remove','rotation','oilcheck'): ";
cin >> s;
if (s == "quit") {
break;
} else if (s == "add") {
addCar();
} else if (s == "list") {
for (int j = 0; j < i; j++) {
cout<<"Car Number: "<<j<<endl;
cars[j]->get_car_information();
}
} else if(s=="update"){
updateCar();
} else if(s=="remove"){
removeCar();
}else if(s=="rotation"){
tyreRotaion();
}else if(s=="recharge"){
recharge();
}
else if(s=="oilcheck"){
CheckOil();
}
else {
cout << "Invalid operation entered. Try again." << endl;
}
}
return 0;
}