Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. bool isRunning = true;
  6.  
  7. class Fillup {
  8. float amountOfGas = 0, pricePaid = 0, odometerReading = 0;
  9. public:
  10. Fillup(float a, float b, float c) { amountOfGas = a; pricePaid = b; odometerReading = c; }
  11. int getTravelDifference(int initialMiles) { return odometerReading - initialMiles; }
  12. int getAmountOfGas() { return amountOfGas; }
  13. int getPricePaid() { return pricePaid; }
  14. int getOdometer() { return odometerReading; }
  15. bool getInfo() {
  16. cout << endl << "Enter Amount Of Gas Purchased: ";
  17. cin >> amountOfGas;
  18. if(isNotValid(amountOfGas)) {
  19. return false;
  20. };
  21. cout << "Enter Price Paid: ";
  22. cin >> pricePaid;
  23. if(isNotValid(pricePaid)) {
  24. return false;
  25. };
  26. cout << "Enter Odometer Reading: ";
  27. cin >> odometerReading;
  28. if(isNotValid(odometerReading)) {
  29. return false;
  30. };
  31. return true;
  32. }
  33. bool isNotValid(float test) {
  34. if (test < 1) {
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. }
  40. };
  41.  
  42. class Travel {
  43. int initialMiles;
  44. vector<Fillup> fillups;
  45. float milesTraveled = 0, milesPerGallon = 0, pricePerGallon = 0, milesPerDollar = 0, totalAmountOfGas = 0, totalPricePaid = 0, lastOdometerRead = 0;
  46. public:
  47. Travel(float a) { initialMiles = a;}
  48. void addFillup(Fillup fillup) {
  49. fillups.push_back(fillup);
  50. }
  51. void calculateTotal() {
  52. milesTraveled = 0;
  53. totalAmountOfGas = 0;
  54. totalPricePaid = 0;
  55. for (auto & fillup : fillups) {
  56. milesTraveled = fillup.getTravelDifference(initialMiles);
  57. totalAmountOfGas = fillup.getAmountOfGas() + totalAmountOfGas;
  58. totalPricePaid = fillup.getPricePaid() + totalPricePaid;
  59. }
  60. milesPerGallon = milesTraveled / totalAmountOfGas;
  61. pricePerGallon = totalPricePaid / totalAmountOfGas;
  62. }
  63. void calculateSingle(Fillup fillup) {
  64. if (fillups.size() < 2 ){
  65. lastOdometerRead = initialMiles;
  66. }
  67. milesTraveled = fillup.getTravelDifference(lastOdometerRead);
  68. milesPerGallon = milesTraveled / fillup.getAmountOfGas();
  69. pricePerGallon = fillup.getPricePaid() / fillup.getAmountOfGas();
  70. lastOdometerRead = fillup.getOdometer();
  71. }
  72. void getInitialMiles() {
  73. cout << "Enter Initial Odometer Reading: ";
  74. cin >> initialMiles;
  75. }
  76. void printStats(const char* prefix = "") {
  77. cout << endl << prefix << "Miles Traveled: " << milesTraveled << endl;
  78. cout << prefix << "Miles Per Gallon: " << milesPerGallon << endl;
  79. cout << prefix << "Price Per Gallon: " << pricePerGallon << endl;
  80. if (fillups.size() > 1 ){
  81. cout << prefix << "Number Of Trips: " << fillups.size() << endl;
  82. }
  83. }
  84. };
  85.  
  86. int main () {
  87. Travel travel (0);
  88. travel.getInitialMiles();
  89. while (isRunning) {
  90. Fillup fill (0,0,0);
  91. if (!fill.getInfo()) {
  92. break;
  93. }
  94. travel.addFillup(fill);
  95. travel.calculateSingle(fill);
  96. travel.printStats();
  97. }
  98. travel.calculateTotal();
  99. travel.printStats("Total ");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement