Advertisement
koodeta

GrItem.cpp

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