Advertisement
koodeta

Gritem.cpp

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