Answer: provided in the explanation section
Explanation:
This was implemented in C++
Filename: Car.cpp
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "Odometer.h"
using namespace std;
int main()
{
char response;
do
{
FuelGuage* myFuel = new FuelGuage(6);
int fuelLevel = myFuel->getFuel();
cout << "Car gas level: " << fuelLevel << endl;
cout << "Car is filling up" << endl;
while(myFuel->getFuel() < 15)
{
myFuel->addFuel();
}
Odometer* car = new Odometer(myFuel, 999990);
cout << "Car gas level: " << car->getFuelGuage()->getFuel() << endl;
cout << "Car is off!" << endl;
cout << "--------------------------" << endl;
while(car->getFuelGuage()->getFuel() > 0)
{
car->addMile();
int miles = car->getMileage();
cout << "Mileage: " << setw(6) << miles << ", Fuel Level: " << car->getFuelGuage()->getFuel() << endl;
cout<<"--------------------------------------------------------------"<<endl;
}
delete myFuel;
delete car;
cout << "Would you like to run the car again(Y/N)? ";
cin >> response;
system("cls||clear");
} while(toupper(response) != 'N');
return 0;
}
Filename: FuelGuage.cpp
#include "FuelGuage.h"
FuelGuage::FuelGuage()
{
init();
}
FuelGuage::FuelGuage(int fuel)
{
init();
this->fuel = fuel;
}
FuelGuage::FuelGuage(const FuelGuage& copy)
{
this->fuel = copy.fuel;
}
void FuelGuage::init()
{
this->fuel = 0;
}
int FuelGuage::getFuel()
{
return fuel;
}
void FuelGuage::addFuel()
{
fuel++;
}
void FuelGuage::burnFuel()
{
fuel--;
}
Filename:FuelGuage.h
#ifndef FUEL_GUAGE_H
#define FUEL_GUAGE_H
class FuelGuage
{
private:
int fuel;
void init();
public:
FuelGuage();
FuelGuage(int fuel);
FuelGuage(const FuelGuage& copy);
int getFuel();
void addFuel();
void burnFuel();
};
#endif
Filename : Odometer.cpp
#include "Odometer.h"
Odometer::Odometer()
{
init();
}
Odometer::Odometer(FuelGuage* inFuel, int inMileage)
{
init();
this->fuel = new FuelGuage(*inFuel);
this->mileage = inMileage;
this->milesSinceAddingFuel = 0;
}
void Odometer::init()
{
fuel = new FuelGuage();
mileage = 0;
milesSinceAddingFuel = 0;
}
FuelGuage* Odometer::getFuelGuage()
{
return fuel;
}
int Odometer::getMileage()
{
return mileage;
}
void Odometer::addMile()
{
if(mileage < 999999)
{
mileage++;
milesSinceAddingFuel++;
}
else
{
mileage = 0;
milesSinceAddingFuel++;
}
if((milesSinceAddingFuel % 24) == 0)
{
fuel->burnFuel();
}
}
void Odometer::resetMiles()
{
milesSinceAddingFuel = 0;
}
Odometer::~Odometer()
{
delete fuel;
}
Filename: Odometer.h
#ifndef ODOMETER_H
#define ODOMETER_H
#include "FuelGuage.h"
class Odometer
{
private:
void init();
int mileage;
int milesSinceAddingFuel;
FuelGuage* fuel;
public:
Odometer();
Odometer(FuelGuage* inFuel, int inMileage);
FuelGuage* getFuelGuage();
int getMileage();
void addMile();
void resetMiles();
~Odometer();
};
#endif