Advertisement
Lawnknome

carLot.cpp

Nov 23rd, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. //date specified for user input
  7. struct Date
  8. {                      
  9.     int day;                                       
  10.     int month;
  11.     int year;
  12. };
  13.  
  14. struct Car
  15. {  
  16.     string make;                                        //car make
  17.     string model;                                       //car model
  18.     int year;                                           //year car made
  19.     Date datePurchased;                                 //date car purchased by lot
  20.     double purchasePrice;                               //price car purchased for by lot
  21.     bool isSold;                                        //was car sold
  22.     Date dateSold;                                      //date car was sold
  23.     double salePrice;                                   //price car sold for
  24. };
  25.  
  26. void carInfo(vector<Car>& carVect, int count);          //function to add car details to list (vector)
  27. void lotInv(vector<Car>& carVect, int count);           //lists the lots current inventory(all non-sold)
  28. void profitMonth(vector<Car>& carVect, int count);      //calculates a profit based on month and year selected
  29.  
  30. int main()
  31. {
  32.     vector<Car> carVect;                                //vector of structs of Car, lot inventory tracker
  33.     int menu;                                           //menu selection input
  34.     int count;                                          //positional tracker
  35.  
  36.     do
  37.     {
  38.         cout << "Please view the allowable menu options.\n";
  39.         cout << "1. Add Entry - Input of car info.\n";
  40.         cout << "2. List - Lists current invetory on site.\n";
  41.         cout << "3. Profit - Displays profit for a given month/year.\n";
  42.         cout << "4. Exit - Terminates Program.\n";
  43.         cout << "Please enter a menu choice 1 - 4: " << endl;
  44.         cin >> menu;
  45.         cin.ignore();
  46.  
  47.         //verify that valid menu choice is selected.
  48.         while ((menu > 4) || (menu < 1))
  49.         {
  50.             cin.clear();
  51.             cout << "ERROR" << endl;
  52.             cout << "Please enter a choice between 1 and 4: ";
  53.             cin >> menu;
  54.             cin.ignore();
  55.         }
  56.  
  57.         switch (menu)
  58.         {
  59.             case 1:
  60.                 carInfo(carVect, count);
  61.                 count++;
  62.                 break;
  63.  
  64.             case 2:
  65.                 lotInv(carVect, count);
  66.                 break;
  67.        
  68.             case 3:
  69.                 profitMonth(carVect, count);
  70.                 break;
  71.  
  72.             case 4:
  73.                 return(0);
  74.        
  75.             }
  76.         }while(1); 
  77.  
  78.     return 0;
  79. }
  80.  
  81. void carInfo(vector<Car>& carVect, int count)
  82. {
  83.     char sold;                      //was car sold
  84.  
  85.     //create element in vector for next inventory piece
  86.     carVect.push_back(Car());
  87.  
  88.     cout << "Make: ";
  89.     getline(cin, carVect[count].make);
  90.    
  91.     cout << "Model: ";
  92.     getline(cin, carVect[count].model);
  93.  
  94.     cout << "Year: ";
  95.     cin >> carVect[count].year;
  96.     cin.ignore();
  97.  
  98.    
  99.     cout << "Month of purchase (1 - 12): ";
  100.     cin >> carVect[count].datePurchased.month;
  101.  
  102.     //validates that a correct number for month is entered
  103.     while(carVect[count].datePurchased.month > 12 || carVect[count].datePurchased.month < 1)
  104.     {
  105.         cin.clear();
  106.         cout << "ERROR" << endl;
  107.         cout << "Please be sure to enter a valid numeric month.\n";
  108.         cout << "Month: ";
  109.         cin >> carVect[count].datePurchased.month;
  110.         cin.ignore();
  111.     }  
  112.  
  113.     //enter the day for February
  114.     if(carVect[count].datePurchased.month == 2)
  115.     {
  116.         cout << "Day (1-28): ";
  117.         cin >> carVect[count].datePurchased.day;
  118.         cin.ignore();
  119.  
  120.         while(carVect[count].datePurchased.day > 28 || carVect[count].datePurchased.day < 1)
  121.         {
  122.             cin.clear();
  123.             cout << "ERROR" << endl;
  124.             cout << "Please be sure to enter a valid date for February (1-28).\n";
  125.             cout << "Day: ";
  126.             cin >> carVect[count].datePurchased.day;
  127.             cin.ignore();
  128.         }  
  129.     }
  130.  
  131.     //enter the day for any month with 30 days
  132.     else if(carVect[count].datePurchased.month == 4 || carVect[count].datePurchased.month == 6
  133.             || carVect[count].datePurchased.month == 9 || carVect[count].datePurchased.month == 11)
  134.     {
  135.         cout << "Day (1-30): ";
  136.         cin >> carVect[count].datePurchased.day;
  137.         cin.ignore();  
  138.  
  139.         while(carVect[count].datePurchased.day > 30 || carVect[count].datePurchased.day < 1)
  140.         {
  141.             cin.clear();
  142.                 cout << "ERROR" << endl;
  143.                 cout << "Please be sure to enter a valid date for the given month (1-30).\n";
  144.                 cout << "Day: ";
  145.                 cin >> carVect[count].datePurchased.day;
  146.                 cin.ignore();
  147.         }  
  148.     }
  149.  
  150.     //enter the day for any month with 31 days
  151.     else
  152.     {
  153.         cout << "Day (1-31): ";
  154.         cin >> carVect[count].datePurchased.day;
  155.         cin.ignore();
  156.  
  157.         while(carVect[count].datePurchased.day > 31 || carVect[count].datePurchased.day < 1)
  158.         {
  159.             cin.clear();
  160.             cout << "ERROR" << endl;
  161.             cout << "Please be sure to enter a valid date for the given month (1-31).\n";
  162.             cout << "Day: ";
  163.             cin >> carVect[count].datePurchased.day;
  164.             cin.ignore();
  165.         }  
  166.     }
  167.  
  168.     //year of purchase
  169.     cout << "Year: ";
  170.     cin >> carVect[count].datePurchased.year;
  171.     cin.ignore();      
  172.    
  173.  
  174.     cout << "Price purchased: ";
  175.     cin >> carVect[count].purchasePrice;
  176.     cin.ignore();  
  177.  
  178.     cout << "Has this vehicle been sold? (Y/N): ";
  179.     cin >> sold;
  180.  
  181.     if(sold == 'Y' || sold == 'y')
  182.     {
  183.         carVect[count].isSold = true;
  184.     }  
  185.  
  186.     if(!(sold == 'Y' || sold == 'y'))
  187.     {
  188.         carVect[count].isSold = false;
  189.     }  
  190.  
  191.     cout << carVect[count].isSold << endl;
  192.  
  193.     if(carVect[count].isSold == true)
  194.     {  
  195.         cout << "Month of sale (1 - 12): ";
  196.         cin >> carVect[count].dateSold.month;
  197.  
  198.         //validates that a correct number for month is entered
  199.         while(carVect[count].dateSold.month > 12 || carVect[count].dateSold.month < 1)
  200.         {
  201.             cin.clear();
  202.             cout << "ERROR" << endl;
  203.             cout << "Please be sure to enter a valid numeric month.\n";
  204.             cout << "Month: ";
  205.             cin >> carVect[count].dateSold.month;
  206.             cin.ignore();
  207.         }  
  208.  
  209.         //enter the day for February
  210.         if(carVect[count].dateSold.month == 2)
  211.         {
  212.             cout << "Day (1-28): ";
  213.             cin >> carVect[count].dateSold.day;
  214.             cin.ignore();
  215.  
  216.             while(carVect[count].dateSold.day > 28 || carVect[count].dateSold.day < 1)
  217.             {
  218.                 cin.clear();
  219.                 cout << "ERROR" << endl;
  220.                 cout << "Please be sure to enter a valid date for February (1-28).\n";
  221.                 cout << "Day: ";
  222.                 cin >> carVect[count].dateSold.day;
  223.                 cin.ignore();
  224.             }  
  225.         }
  226.  
  227.         //enter the day for any month with 30 days
  228.         else if(carVect[count].dateSold.month == 4 || carVect[count].dateSold.month == 6
  229.                 || carVect[count].dateSold.month == 9 || carVect[count].dateSold.month == 11)
  230.         {
  231.             cout << "Day (1-30): ";
  232.             cin >> carVect[count].dateSold.day;
  233.             cin.ignore();
  234.  
  235.             while(carVect[count].dateSold.day > 30 || carVect[count].dateSold.day < 1)
  236.             {
  237.                 cin.clear();
  238.                 cout << "ERROR" << endl;
  239.                 cout << "Please be sure to enter a valid date for the given month (1-30).\n";
  240.                 cout << "Day: ";
  241.                 cin >> carVect[count].dateSold.day;
  242.                 cin.ignore();
  243.             }  
  244.         }
  245.  
  246.         //enter the day for any month with 31 days
  247.         else
  248.         {
  249.             cout << "Day (1-31): ";
  250.             cin >> carVect[count].dateSold.day;
  251.             cin.ignore();
  252.  
  253.             while(carVect[count].dateSold.day > 31 || carVect[count].dateSold.day < 1)
  254.             {
  255.                 cin.clear();
  256.                 cout << "ERROR" << endl;
  257.                 cout << "Please be sure to enter a valid date for the given month (1-31).\n";
  258.                 cout << "Day: ";
  259.                 cin >> carVect[count].dateSold.day;
  260.                 cin.ignore();
  261.             }  
  262.         }
  263.  
  264.         //year of sale
  265.         cout << "Year: ";
  266.         cin >> carVect[count].dateSold.year;
  267.         cin.ignore();      
  268.    
  269.         cout << "Sale price: ";
  270.         cin >> carVect[count].salePrice;
  271.         cin.ignore();  
  272.     }  
  273.  
  274.     cout << "\n\n";
  275.  
  276. }
  277.  
  278.  
  279.  
  280. void lotInv(vector<Car>& carVect, int count)
  281. {
  282.     cout << "Current lot inventory" << endl;
  283.     cout << setw(15) << left << "Make" << "\t\t" <<"Model" << "\t\t" << "Year" << "\t\t" << "Purchase Date"
  284.          << "\t\t" << "Purchase Price" <<   endl;
  285.  
  286.     for(int i = 0; i < carVect.size(); i++)
  287.     {
  288.         //only display cars that were NOT sold
  289.         if(carVect[i].isSold == false)
  290.         {
  291.             cout << setw(15) << left << carVect[i].make << "\t\t" << carVect[i].model << "\t\t"
  292.                  << carVect[i].year << "\t\t" << carVect[i].datePurchased.month << "/"
  293.                  << carVect[i].datePurchased.day << "/" << carVect[i].datePurchased.year
  294.                  << "\t\t" << "$" << showpoint << setprecision(2) << fixed << carVect[i].purchasePrice << endl;
  295.  
  296.             cout << endl;    
  297.         }  
  298.     }    
  299. }
  300.  
  301. void profitMonth(vector<Car>& carVect, int count)
  302. {
  303.     int Pmonth;                     //month selected to chart profits
  304.     int Pyear;                      //year selected to chart profits
  305.     double Ppurchase;               //adds up total purchase amounts for that month/year
  306.     double Psold;                   //adds up all sold prices for month/year
  307.     double profits;                 //calculated profits
  308.  
  309.     cout << "Generate a profit report based on date.\n";
  310.     cout << "Month: ";
  311.     cin >> Pmonth;
  312.     cin.ignore();
  313.     cout << "Year: ";
  314.     cin >> Pyear;
  315.     cin.ignore();
  316.  
  317.     for(int i = 0; i < carVect.size(); i++)
  318.     {
  319.         Ppurchase += carVect[i].purchasePrice;
  320.  
  321.         //parse vector based on month
  322.         if(carVect[i].dateSold.month == Pmonth)
  323.         {
  324.             //parse vector based on year
  325.             if(carVect[i].dateSold.year == Pyear)
  326.             {
  327.                 Psold += carVect[i].salePrice;
  328.             }  
  329.         }  
  330.  
  331.     }
  332.  
  333.     profits = Psold - Ppurchase;    //calculated profits (sales - overhead)
  334.  
  335.     cout << "Total Profits for given month/year: " << endl;
  336.     cout << "$" << showpoint << setprecision(2) << fixed << profits << endl;
  337.     cout "\n\n";   
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement