Advertisement
koodeta

GrItem.cpp

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