Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Solved Programs on C++ » Simulate a Car

C++ Program to Simulate a Car - Run the Car, Fill Fuel, Stop Car

Problem Statement:-
Design a set of classes that work together to simulate a car’s fuel gauge and odometer. The classes designed will include:
The FuelGauge class: this class will simulate a fuel gauge and will be responsible for:
1. Knowing the car’s current amount of fuel in gallons
2. Reporting the car’s current amount of fuel in gallons
3. Incrementing the amount of fuel by one (1) gallon (putting fuel in the car).
a. The car can hold a maximum of 15 gallons of fuel
4. Decrementing the amount of fuel by 1 gallon (if the amount of fuel is greater than 0 gallons (simulating burning fuel as the car runs).



The Odometer class: this class will simulate the car’s odometer and will be responsible for :
1. Knowing the car’s current mileage
2. Reporting the car’s current mileage
3. Incrementing the car’s current mileage by 1 mile
a. The maximum is 999,999 miles
b. When this amount is exceeded, the odometer resets the current mileage to zero
4. Working with the fuelGauge object
a. Decreasing the FuelGauge object’s current amount of fuel by 1 gallon for every
24 miles travelled.

Solution :
Please note this Solution has 5 files: main.cpp, FuelGuage.h, FuelGuage.cpp, Odometer.h and Odometer.cpp

//main.cpp #include <iostream> #include "FuelGuage.h" #include "Odometer.h" using namespace std; int main() { FuelGuage fuel; Odometer odometer; char choice = 'y'; while(choice != 'n') { unsigned int fuelqty; cout<<"\n Set the amount of Fuel (max 15 gallons) : "; cin>>fuelqty; if(fuelqty>15) { cout<<"\n Can't fill more than 15 Gallons of fuel"; continue; } for(int i=0;i=1) { fuel.burnFuel(); odometer.increamentMileage(); odometer.getCurrentMileage(); fuel.getFuel(); //added to have some delay :) for(int i=0;i<15000;i++) for(int j=0;j<15000;j++) { //do nothing. Delay to make program fancy } fuelLeft = fuel.getFuel(); } cout<<"\n\n Car Stopeed. No fuel left... "; cout<<"\n\n Do you want to run the car again ? y/n "; cin>>choice; } cout<<"\n\n You must have enjoyed it !! :D :D. thank you !"; return 0; } /*----------------------------end of main.cpp-------------------------------*/ //FuelGuage.cpp #include "FuelGuage.h" #include <iostream> using namespace std; FuelGuage::FuelGuage(){ fuelQty = 0 ; } //simulating putting fuel in car void FuelGuage::putFuel(){ if(fuelQty<15) { fuelQty++; cout<<"\n filling fuel.. current qty : "<=1) fuelQty--; } unsigned int FuelGuage:: getFuel(){ cout<<"\n Current Amount of Fuel : "<> temp; if(temp<=15) fuelQty = temp; else cout<<"\n Fuel amount can't be more than 15 Gallons"; } /*---------------- end of FuelGuage.cpp ---------------*/ //FuelGuage.h class FuelGuage{ private: unsigned int fuelQty; public: FuelGuage(); //simulating putting fuel in car void putFuel(); //simulating burning fuel of car void burnFuel(); unsigned int getFuel(); void reportFuel(); }; /*---------- end of FuelGuage.h ---------------*/ //Odometer.cpp #include "Odometer.h" Odometer::Odometer(){ currentMileage = 0; } int Odometer::reportMileage() { unsigned long int temp; cout<<"\n Set current mileage : "; cin>>temp; if(temp <= 99999) { currentMileage = temp; return 1; } else { cout<<"\n Mileage can't be more than 99,999"; return 0; } } void Odometer::getCurrentMileage(){ cout<<"\n Current Mileage of Car : "<< currentMileage; } void Odometer::increamentMileage(){ if(currentMileage<99999) currentMileage++; else currentMileage = 0; } /*--------------------------- Odometer.cpp -------------*/ //Odometer.h #include <iostream> using namespace std; class Odometer{ private: unsigned long int currentMileage; public: Odometer(); int reportMileage(); void getCurrentMileage(); void increamentMileage(); }; /*---------------- end of odometer.h ----------*/