Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- bool isRunning = true;
- class Fillup {
- float amountOfGas = 0, pricePaid = 0, odometerReading = 0;
- public:
- Fillup(float a, float b, float c) { amountOfGas = a; pricePaid = b; odometerReading = c; }
- int getTravelDifference(int initialMiles) { return odometerReading - initialMiles; }
- int getAmountOfGas() { return amountOfGas; }
- int getPricePaid() { return pricePaid; }
- int getOdometer() { return odometerReading; }
- bool getInfo() {
- cout << endl << "Enter Amount Of Gas Purchased: ";
- cin >> amountOfGas;
- if(isNotValid(amountOfGas)) {
- return false;
- };
- cout << "Enter Price Paid: ";
- cin >> pricePaid;
- if(isNotValid(pricePaid)) {
- return false;
- };
- cout << "Enter Odometer Reading: ";
- cin >> odometerReading;
- if(isNotValid(odometerReading)) {
- return false;
- };
- return true;
- }
- bool isNotValid(float test) {
- if (test < 1) {
- return true;
- } else {
- return false;
- }
- }
- };
- class Travel {
- int initialMiles;
- vector<Fillup> fillups;
- float milesTraveled = 0, milesPerGallon = 0, pricePerGallon = 0, milesPerDollar = 0, totalAmountOfGas = 0, totalPricePaid = 0, lastOdometerRead = 0;
- public:
- Travel(float a) { initialMiles = a;}
- void addFillup(Fillup fillup) {
- fillups.push_back(fillup);
- }
- void calculateTotal() {
- milesTraveled = 0;
- totalAmountOfGas = 0;
- totalPricePaid = 0;
- for (auto & fillup : fillups) {
- milesTraveled = fillup.getTravelDifference(initialMiles);
- totalAmountOfGas = fillup.getAmountOfGas() + totalAmountOfGas;
- totalPricePaid = fillup.getPricePaid() + totalPricePaid;
- }
- milesPerGallon = milesTraveled / totalAmountOfGas;
- pricePerGallon = totalPricePaid / totalAmountOfGas;
- }
- void calculateSingle(Fillup fillup) {
- if (fillups.size() < 2 ){
- lastOdometerRead = initialMiles;
- }
- milesTraveled = fillup.getTravelDifference(lastOdometerRead);
- milesPerGallon = milesTraveled / fillup.getAmountOfGas();
- pricePerGallon = fillup.getPricePaid() / fillup.getAmountOfGas();
- lastOdometerRead = fillup.getOdometer();
- }
- void getInitialMiles() {
- cout << "Enter Initial Odometer Reading: ";
- cin >> initialMiles;
- }
- void printStats(const char* prefix = "") {
- cout << endl << prefix << "Miles Traveled: " << milesTraveled << endl;
- cout << prefix << "Miles Per Gallon: " << milesPerGallon << endl;
- cout << prefix << "Price Per Gallon: " << pricePerGallon << endl;
- if (fillups.size() > 1 ){
- cout << prefix << "Number Of Trips: " << fillups.size() << endl;
- }
- }
- };
- int main () {
- Travel travel (0);
- travel.getInitialMiles();
- while (isRunning) {
- Fillup fill (0,0,0);
- if (!fill.getInfo()) {
- break;
- }
- travel.addFillup(fill);
- travel.calculateSingle(fill);
- travel.printStats();
- }
- travel.calculateTotal();
- travel.printStats("Total ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement