Advertisement
koodeta

Untitled

May 7th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.01 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 alcohol = 0.08;
  22. const double food = 0.05;
  23. const double genMerchandise = 0.07;
  24. const double medicine = 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.     double costBeforeTax = 0.0, costAfterTax = 0.0, custSaving = 0.0, totRegPrice = 0.0, totSalePrice = 0.0;// Variables used in the accessor functions at the bottom
  31. };
  32.  
  33. const int listSize = 20;
  34. // Main method
  35. int main(){
  36.  
  37.     string input;// Holds each line from the imported textfile temporarily
  38.     string fileName;// Name of grocery list user wishes to use
  39.     fstream nameFile;// File stream object
  40.     GrListItem itemList[listSize];// Creates a list of objects. These objects are each item on the list and hold related information
  41.     std::vector<GrListItem> vectorList(itemList, itemList + listSize);
  42.     cost itemTotalCost;
  43.  
  44.     // Requests data from user
  45.     cout << "What is the name of the grocery list you wish to use? " << endl;
  46.     getline(cin, fileName);// Retrieves filename from user and applies string to grListName
  47.  
  48.     // Tests to see if file can be opened
  49.     fstream testFile(fileName, ios::out);
  50.     if (testFile.fail()){
  51.         cout << "ERROR: Cannot open indicated file.\n";
  52.         return 0;
  53.     }
  54.  
  55.     // Open data file
  56.     nameFile.open(fileName, ios::in);
  57.     // Read data and apply variables to an object
  58.     if (nameFile){
  59.         int count = 0;
  60.         while (nameFile && count < listSize){
  61.  
  62.             // Read the name
  63.             getline(nameFile, input, '#');
  64.             vectorList[count].name = input;// Assigns item name to the object inside itemList.name
  65.  
  66.             // Read quantity
  67.             getline(nameFile, input, '$');
  68.             vectorList[count].quantity = atoi(input.c_str());// Casts string to an int
  69.  
  70.             // Read regular price
  71.             getline(nameFile, input, '$');
  72.             vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
  73.            
  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 = atof(input.c_str());// Casts string to a double
  90.  
  91.  
  92.             getTotAfterTax(itemTotalCost.costBeforeTax, );// Needs enum value parameters. Don't know how to do
  93.             //getTotTaxCategory() Still not sure how to do
  94.             getTotBeforeTax(vectorList[count].regPrice, vectorList[count].salePrice, vectorList[count].isOnSale);
  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 goes to each object and removes the line delimiters
  110.     for (int i = 0; i != listSize; ++i){
  111.  
  112.     }
  113.  
  114.     // For loop that creates a receipt on the screen
  115.     // Formatting may or may not be correct
  116.     for (int i = 0; i != listSize; ++i){
  117.         std::cout << vectorList[i].name << endl;// Print item name
  118.         std::cout << std::setfill(' ') << std::setw(20);
  119.         std::cout.fill(' ');
  120.  
  121.         std::cout << std::setfill(' ') << std::setw(5);// Print item quantity
  122.         std::cout << vectorList[i].quantity << endl;
  123.         std::cout.fill(' ');
  124.  
  125.         std::cout << std::setfill(' ') << std::setw(5);// Print regular price of item
  126.         std::cout << vectorList[i].regPrice << endl;
  127.         std::cout.fill(' ');
  128.  
  129.         std::cout << std::setfill(' ') << std::setw(5);// Print sale price of item
  130.         std::cout << vectorList[i].salePrice << endl;
  131.         std::cout.fill(' ');
  132.  
  133.         if (vectorList[i].onSale == 1){
  134.             std::cout << 'Y' << endl;// Print 'Y' if vectorList[i] is on sale
  135.             std::cout.width(3);
  136.             std::cout.fill(' ');
  137.         }
  138.         else {
  139.             std::cout << 'N' << endl;// Print 'N' if vectorList[i] is on sale
  140.             std::cout.width(3);
  141.             std::cout.fill(' ');
  142.         }
  143.  
  144.         std::cout << vectorList[i].GrListItem::taxCategory << endl;// Print tax category
  145.         std::cout.width(20);
  146.         std::cout.fill(' ');
  147.     }
  148.  
  149.     // Print details of purchase below list of bought items
  150.  
  151.     // NOTE: THESE VALUES ARE STORED IN THE STRUCT ABOVE main()
  152.     // Display total before tax
  153.     // Display total after tax
  154.     // Display customer Savings
  155.    
  156. }
  157.  
  158. // Constructor
  159. GrItem::GrItem(string name, int quantity, float regPrice, float salePrice, bool onSale, enum GrListItem::taxCategory taxCategory){
  160.  
  161.     name = name;
  162.     quantity = quantity;
  163.     regPrice = regPrice;
  164.     salePrice = salePrice;
  165.     onSale = onSale;
  166.     enum GrListItem::taxCategory tax = taxCategory;
  167.  
  168. };
  169.  
  170. // Default constructor
  171. GrItem::GrItem() {
  172. }
  173.  
  174. // Get the total cost before the tax
  175. void getTotBeforeTax(double regPrice, double salePrice, bool onSale){
  176.     cost itemTotalCost;
  177.     if (onSale == 1){
  178.         itemTotalCost.costBeforeTax += salePrice;
  179.         itemTotalCost.totSalePrice += salePrice;
  180.     }
  181.     else{
  182.         itemTotalCost.costBeforeTax += regPrice;
  183.         itemTotalCost.totRegPrice += regPrice;
  184.     }
  185. }
  186.  
  187. // Get the total amount of tax for each category
  188. void getTotTaxCategory(){// Need parameters. They must incorporate the cost struct values. These different values are determined by what enum tax category they are. Not sure how to implement
  189.     cost itemTotalCost;
  190.    
  191.  
  192. }
  193.  
  194. // Get the total after tax
  195. void getTotAfterTax(float costBeforeTax, float totalTax){// Enum value as third parameter to determine tax
  196.     cost itemTotalCost;
  197.     itemTotalCost.costAfterTax = itemTotalCost.costBeforeTax + itemTotalCost.totalTax;
  198.  
  199. }
  200.  
  201. // Get customer savings (total of all differences between regular price and sale price for items that are currently on sale)
  202. void getCustSave(double totRegPrice, double totSalePrice, bool onSale){
  203.     cost itemTotalCost;
  204.     if (onSale == 1){
  205.         itemTotalCost.custSaving = totRegPrice - totSalePrice;
  206.     }
  207.  
  208. }
  209.  
  210. // Function that is called to sort by name
  211. bool sortByName(const GrListItem &lhs, const GrListItem &rhs){
  212.     return lhs.name < rhs.name;
  213. }
  214.  
  215. // Function that is called to sort by quantity
  216. bool sortByQuantity(const GrListItem &lhs, const GrListItem &rhs){
  217.     return lhs.quantity < rhs.quantity;
  218. }
  219.  
  220. // Function that is called to sort by regular price
  221. bool sortByRegPrice(const GrListItem &lhs, const GrListItem &rhs){
  222.     return lhs.regPrice < rhs.regPrice;
  223. }
  224.  
  225. // Function that is called to sort by sale price
  226. bool sortBySalePrice(const GrListItem &lhs, const GrListItem &rhs){
  227.     return lhs.salePrice < rhs.salePrice;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement