Saturday, January 13, 2018

Programming Challenge 14.15 - Car Instrument Simulator

Example Files: CarInstrumentSimulator.7z

FuelGauge.h


#ifndef FUEL_GAUGE_H_
#define FUEL_GAUGE_H_

class FuelGauge
{
    private:
        const  int MAX_CAPACITY = 15;        // The max fuel capacity
        static int fuelCapacity;            // Holds the fuel capacity

    public:
        FuelGauge()
        { }

        int getFuelCapacity() const
        { return fuelCapacity; }

        // Overloaded operator functions
        int operator ++(int);                // Postfix ++
        int operator --(int);                // Postfix --
};

#endif

FuelGauge.cpp


#include "FuelGauge.h"

// Definition of fuelCapacity
int FuelGauge::fuelCapacity = 0;

/* **********************************************************
            Overloaded ++ operator
    ********************************************************** */

int FuelGauge::operator ++(int)
{
    if (fuelCapacity < MAX_CAPACITY)
    {
        fuelCapacity++;
    }
    else
    {
        fuelCapacity = MAX_CAPACITY;
    }

    return fuelCapacity;
}

/* **********************************************************
            Overloaded -- operator
   ********************************************************** */

int FuelGauge::operator --(int)
{
    if (fuelCapacity > 0)
    {
        fuelCapacity--;
    }
    else
    {
        fuelCapacity = 0;
    }
   
    return fuelCapacity;
}

Odometer.h


#ifndef ODOMETER_H_
#define ODOMETER_H_

#include "FuelGauge.h"

class Odometer
{
    private:
        const int MAX_MILEAGE = 999999;    // The maximum mileage an odometer can display

        static int mileage;                    // Holding the mileage of a car
        static int milesDriven;                // Counter variable

        // Class object
        FuelGauge gaugeCapacity;

    public:
        Odometer()
        { }

        Odometer(int);

        int getMilesDriven() const
        { return milesDriven; }

        int getCurrentMileage() const
        { return mileage; }

        // Overloaded operator function
        int operator ++(int);                // Postfix ++
};

#endif

Odometer.cpp


#include "Odometer.h"

// Definition of static variables mileage and milesDriven
int Odometer::mileage = 0;
int Odometer::milesDriven = -1;

/* **********************************************************
            Odometer::Odometer() - Constructor : int
   ********************************************************** */

Odometer::Odometer(int miles)
{
    if (mileage >= 0 && mileage <= MAX_MILEAGE)
    {
        mileage = miles;
    }
    else
    {
        miles = 0;
    }
}

/* **********************************************************
            Overloaded Postfix ++
    The postfix operator increments the mileage, and decreases
    the FuelGauge object's current amount of fuel by 1 gallon
    for every 24 miles traveled. If mileage reaches 999999,
    the mileage counter is reset to 0.
   ********************************************************** */

int Odometer::operator ++(int)
{
    if (mileage < MAX_MILEAGE)
    {
        mileage++;

        if (++milesDriven == 24)
        {
            milesDriven = 0;
            gaugeCapacity--;
        }
    }
    else if (mileage >= MAX_MILEAGE)
    {
        mileage = 0;
    }

    return mileage;
}

CarInstrumentSimulator.cpp


#include "FuelGauge.h"
#include "Odometer.h"

#include <iomanip>
using std::left;
using std::right;
using std::setw;

#include <iostream>
using std::cin;
using std::cout;

void timer();
void refillTank(FuelGauge &);
void instrumentSimulator(Odometer &, const FuelGauge &);

int main()
{
    FuelGauge gauge;
    Odometer meter(999998);

    cout << "CAR INSTRUMENT SIMULATOR\n\n";

    refillTank(gauge);
    instrumentSimulator(meter, gauge);

    cin.get();
    cin.ignore();
    return 0;
}

/* **********************************************************
            timer()
    A very simple timer function.
   ********************************************************** */

void timer()
{
    static int seconds = 5;
    static int timeLeft = 0;
    static int counter = 193559599;

    while (timeLeft > 0)
    {
        timeLeft--;
    }

    timeLeft = counter - seconds;
}

/* **********************************************************
            refillTank() : obj &
    This function accepts a FuelGauge object passed to it by
    reference as argument. It simulates a refueling process.
   ********************************************************** */

void refillTank(FuelGauge &gauge)
{
    cout << "Fuel meter E |";

    for (int i = gauge.getFuelCapacity(); i < 15; i++)
    {
        gauge++;
        timer();

        cout << (i + 1) << "*|";
    }

    cout << " F\n\n";
}

/* **********************************************************
            instrumentSimulator() : obj &, const obj &
    This function simulates the main instrument panel of a
    car. It outputs the current mileage driven and current
    gauge capacity.
   ********************************************************** */

void instrumentSimulator(Odometer &meter, const FuelGauge &gauge)
{
    while (gauge.getFuelCapacity() >= 1)
    {
            meter++;

            if (gauge.getFuelCapacity() >= 3)
            {
                cout << "Miles Driven: " << setw(25) << left << meter.getCurrentMileage();
                cout << "Gas Gauge: " << right << gauge.getFuelCapacity() << "\n";
            }
            else if (gauge.getFuelCapacity() > 0 && gauge.getFuelCapacity() <= 3)
            {
                cout << "Miles Driven: " << setw(25) << left << meter.getCurrentMileage();
                cout << "Gas Gauge: "    << right << gauge.getFuelCapacity() << " FUEL LOW!\n";
            }
            else if (gauge.getFuelCapacity() == 0)
            {
                cout << "\nOUT OF FUEL!\n";
            }

            timer();
    }
}

Example Output:


Animation created with ScreenToGif


No comments:

Post a Comment