Advertisement
koodeta

GrItem.cpp

May 8th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.75 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "StoreInfo.h"
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <iomanip>
  9. #include <map>
  10.  
  11. using namespace std;
  12.  
  13. // Tax rate catagories
  14. const double alcoholTax = 0.08;
  15. const double foodTax = 0.05;
  16. const double genMerchandiseTax = 0.07;
  17. const double medicineTax = 0.04;
  18.  
  19. struct cost {
  20.     double alcTax = 0.0, food = 0.0, genMerch = 0.0, meds = 0.0;// Total taxes collected for each tax bracket
  21.     double totalTax = alcTax + food + genMerch + meds;// Total Taxes to be accessed later
  22.  
  23.     // Variables used in the accessor functions at the bottom
  24.     double costBeforeTax = 0.0, costAfterTax = 0.0, custSaving = 0.0, totRegPrice = 0.0, totSalePrice = 0.0;
  25. };
  26.  
  27. // Constructor
  28. GrItem::GrItem(string name, int quantity, float regPrice, float salePrice, bool onSale, TaxCategory taxCategory){
  29.  
  30.     name = name;
  31.     quantity = quantity;
  32.     regPrice = regPrice;
  33.     salePrice = salePrice;
  34.     onSale = onSale;
  35.     taxCategory = taxCategory;
  36.  
  37. };
  38.  
  39. // Default constructor
  40. GrItem::GrItem() {
  41. }
  42.  
  43. // Get the total cost before the tax
  44. void getTotBeforeTax(double regPrice, double salePrice, bool onSale){
  45.     cost itemTotalCost;
  46.     if (onSale == 1){
  47.         itemTotalCost.costBeforeTax += salePrice;
  48.         itemTotalCost.totSalePrice += salePrice;
  49.     }
  50.     else{
  51.         itemTotalCost.costBeforeTax += regPrice;
  52.         itemTotalCost.totRegPrice += regPrice;
  53.     }
  54. }
  55.  
  56. // Get the total after tax
  57. void getTotAfterTax(float costBeforeTax, float totalTax){
  58.     cost itemTotalCost;
  59.     itemTotalCost.costAfterTax = costBeforeTax + totalTax;
  60. }
  61.  
  62. // Get the total amount of tax for each category
  63. void getTotTaxCategory(double regPrice, double salePrice, bool onSale, TaxCategory taxCategory){// These different values are determined by what enum tax category they are
  64.     cost itemTotalCost;
  65.     // Adds values to the total alcohol tax gathered
  66.     if (taxCategory = TaxCategory::alcohol){
  67.         if (onSale = true)
  68.             itemTotalCost.alcTax += salePrice * alcoholTax;
  69.         itemTotalCost.alcTax += regPrice * alcoholTax;
  70.     }
  71.  
  72.     if (taxCategory = TaxCategory::food){
  73.         if (onSale = true)
  74.             itemTotalCost.alcTax += salePrice * foodTax;
  75.         itemTotalCost.alcTax += regPrice * foodTax;
  76.     }
  77.  
  78.     if (taxCategory = TaxCategory::genMerchandise){
  79.         if (onSale = true)
  80.             itemTotalCost.alcTax += salePrice * genMerchandiseTax;
  81.         itemTotalCost.alcTax += regPrice * genMerchandiseTax;
  82.     }
  83.  
  84.     if (taxCategory = TaxCategory::medicine){
  85.         if (onSale = true)
  86.             itemTotalCost.alcTax += salePrice * medicineTax;
  87.         itemTotalCost.alcTax += regPrice * medicineTax;
  88.     }
  89. }
  90.  
  91. // Get customer savings (total of all differences between regular price and sale price for items that are currently on sale)
  92. void getCustSave(double totRegPrice, double totSalePrice, bool onSale){
  93.     cost itemTotalCost;
  94.     if (onSale == 1){
  95.         itemTotalCost.custSaving = totRegPrice - totSalePrice;
  96.     }
  97.  
  98. }
  99.  
  100. // Function that is called to sort by name
  101. bool sortByName(const GrListItem &lhs, const GrListItem &rhs){
  102.     return lhs.name < rhs.name;
  103. }
  104.  
  105. // Function that is called to sort by quantity
  106. bool sortByQuantity(const GrListItem &lhs, const GrListItem &rhs){
  107.     return lhs.quantity < rhs.quantity;
  108. }
  109.  
  110. // Function that is called to sort by regular price
  111. bool sortByRegPrice(const GrListItem &lhs, const GrListItem &rhs){
  112.     return lhs.regPrice < rhs.regPrice;
  113. }
  114.  
  115. // Function that is called to sort by sale price
  116. bool sortBySalePrice(const GrListItem &lhs, const GrListItem &rhs){
  117.     return lhs.salePrice < rhs.salePrice;
  118. }
  119.  
  120. const int listSize = 20;
  121. // Main method
  122. int main() {
  123.  
  124.     string input;// Holds each line from the imported textfile temporarily
  125.     string fileName;// Name of grocery list user wishes to use
  126.     fstream nameFile;// File stream object
  127.     GrListItem itemList[listSize];// Creates a list of objects. These objects are each item on the list and hold related information
  128.     std::vector<GrListItem> vectorList(itemList, itemList + listSize);
  129.     cost itemTotalCost;
  130.  
  131.     // Requests data from user
  132.     cout << "What is the name of the grocery list you wish to use? " << endl;
  133.     getline(cin, fileName);// Retrieves filename from user and applies string to grListName
  134.  
  135.     // Tests to see if file can be opened
  136.     fstream testFile(fileName, ios::out);
  137.     if (testFile.fail()){
  138.         cout << "ERROR: Cannot open indicated file.\n";
  139.         return 0;
  140.     }
  141.  
  142.     // Open data file
  143.     nameFile.open(fileName, ios::in);
  144.     // Read data and apply variables to an object
  145.     if (nameFile){
  146.         int count = 0;
  147.         while (nameFile && count < listSize){
  148.  
  149.             // Read the name
  150.             getline(nameFile, input, '#');
  151.             vectorList[count].name = input;// Assigns item name to the object inside itemList.name
  152.  
  153.             // Read quantity
  154.             getline(nameFile, input, '$');
  155.             vectorList[count].quantity = atoi(input.c_str());// Casts string to an int
  156.  
  157.             // Read regular price
  158.             getline(nameFile, input, '$');
  159.             vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
  160.  
  161.             // Read sale price
  162.             getline(nameFile, input, '#');
  163.             vectorList[count].salePrice = stof(input.c_str());
  164.  
  165.             // Read on sale bool
  166.             getline(nameFile, input, '#');
  167.             if (input == "Y")// If the item is on sale, the onSale var returns true
  168.                 vectorList[count].onSale = true;
  169.             else
  170.                 vectorList[count].onSale = false;
  171.  
  172.             // Read tax category
  173.             getline(nameFile, input, '#');
  174.             if (input == "alcohol") {
  175.                 vectorList[count].taxCategory = TaxCategory::alcohol;;
  176.             }
  177.             else if (input == "general merchanise") {
  178.                 vectorList[count].taxCategory = TaxCategory::genMerchandise;
  179.             }
  180.             else if (input == "food") {
  181.                 vectorList[count].taxCategory = TaxCategory::food;;
  182.             }
  183.             else if (input == "medicine") {
  184.                 vectorList[count].taxCategory = TaxCategory::medicine;;
  185.             }
  186.             else {
  187.                 std::cout << "BAD TAX CATEGORY" << std::endl;
  188.             }
  189.  
  190.             // These functions are called as many times as there are objects in the array.
  191.             getTotBeforeTax(vectorList[count].regPrice, vectorList[count].salePrice, vectorList[count].onSale);
  192.             getTotTaxCategory(vectorList[count].regPrice, vectorList[count].salePrice, vectorList[count].onSale, vectorList[count].taxCategory);
  193.             getTotAfterTax(itemTotalCost.costBeforeTax, itemTotalCost.totalTax);
  194.  
  195.             count++;
  196.         }
  197.         // Close file
  198.         nameFile.close();
  199.     }
  200.     else
  201.         cout << "ERROR: Cannot open file.\n";
  202.  
  203.     // Sort array
  204.     // OFFER USER TO CHOOSE HOW THEY WOULD LIKE TO SORT THEIR LIST!!! For extra points
  205.     // Maybe
  206.     std::sort(vectorList.begin(), vectorList.end(), sortByName);
  207.  
  208.     // For loop that creates a receipt on the screen
  209.     // Formatting may or may not be correct
  210.     for (int i = 0; i != listSize; ++i){
  211.         std::cout << vectorList[i].name << endl;// Print item name
  212.         std::cout << std::setfill(' ') << std::setw(20);
  213.         std::cout.fill(' ');
  214.  
  215.         std::cout << std::setfill(' ') << std::setw(5);// Print item quantity
  216.         std::cout << vectorList[i].quantity << endl;
  217.         std::cout.fill(' ');
  218.  
  219.         std::cout << std::setfill(' ') << std::setw(5);// Print regular price of item
  220.         std::cout << vectorList[i].regPrice << endl;// Adjust preci
  221.         std::cout.fill(' ');
  222.  
  223.         std::cout << std::setfill(' ') << std::setw(5);// Print sale price of item
  224.         std::cout << vectorList[i].salePrice << endl;
  225.         std::cout.fill(' ');
  226.  
  227.         if (vectorList[i].onSale == 1){
  228.             std::cout << 'Y' << endl;// Print 'Y' if vectorList[i] is on sale
  229.             std::cout.width(3);
  230.             std::cout.fill(' ');
  231.         }
  232.         else {
  233.             std::cout << 'N' << endl;// Print 'N' if vectorList[i] is not on sale
  234.             std::cout.width(3);
  235.             std::cout.fill(' ');
  236.         }
  237.  
  238.         std::cout << vectorList[i].taxCategory << endl;// Print tax category
  239.         std::cout.width(20);
  240.         std::cout.fill(' ');
  241.     }
  242.  
  243.     // Print details of purchase below list of bought items
  244.  
  245.     // NOTE: THESE VALUES ARE STORED IN THE STRUCT ABOVE main()
  246.     // Display total before tax
  247.     // Display total after tax
  248.     // Display customer Savings
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement