Advertisement
koodeta

Untitled

May 5th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 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. const int listSize = 20;
  27. // Main method
  28. int main(){
  29.  
  30. double costBeforeTax = 0.0, costAfterTax = 0.0, custSaving = 0.0;
  31. double alcTax = 0.0, food = 0.0, genMerch = 0.0, meds = 0.0;
  32. typedef enum taxAmount{ alcTax, food, genMerch, meds } taxAmount;// Enum to hold values of total taxes collected for each tax bracket
  33.  
  34. string input;// Holds each line from the imported textfile temporarily
  35. string fileName;// Name of grocery list user wishes to use
  36. fstream nameFile;// File stream object
  37. GrListItem itemList[listSize];// Creates a list of objects. These objects are each item on the list and hold related information
  38. std::vector<GrListItem> vectorList(itemList, itemList + listSize);
  39.  
  40. // Requests data from user
  41. cout << "What is the name of the grocery list you wish to use? " << endl;
  42. getline(cin, fileName);// Retrieves filename from user and applies string to grListName
  43.  
  44. // Tests to see if file can be opened
  45. fstream testFile(fileName, ios::out);
  46. if (testFile.fail()){
  47. cout << "ERROR: Cannot open indicated file.\n";
  48. return 0;
  49. }
  50.  
  51. // Open data file
  52. nameFile.open(fileName, ios::in);
  53. if (nameFile){
  54. int count = 0;
  55. while (nameFile && count < listSize){
  56.  
  57. // Read the name
  58. getline(nameFile, input, '#');
  59. vectorList[count].name = input;// Assigns item name to the object inside itemList.name
  60.  
  61. // Read quantity
  62. getline(nameFile, input, '$');
  63. vectorList[count].quantity = atoi(input.c_str());// Casts string to an int
  64.  
  65. // Read regular price
  66. getline(nameFile, input, '$');
  67. vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
  68.  
  69. // Read sale price
  70. getline(nameFile, input, '#');
  71. vectorList[count].salePrice = stof(input.c_str());
  72.  
  73. // Read on sale bool
  74. getline(nameFile, input, '#');
  75. if (input == "Y");
  76. vectorList[count].onSale = atoi(input.c_str());
  77.  
  78. // Read tax category
  79. getline(nameFile, input, '#');
  80. vectorList[count].taxCategory = atof(input.c_str());// Casts string to a double
  81.  
  82. count++;
  83. }
  84. // Close file
  85. nameFile.close();
  86. }
  87. else
  88. cout << "ERROR: Cannot open file.\n";
  89.  
  90. // Sort array
  91. // OFFER USER TO CHOOSE HOW THEY WOULD LIKE TO SORT THEIR LIST!!!
  92. std::sort(vectorList.begin(), vectorList.end(), sortByName);
  93.  
  94. // For loop that creates a receipt on the screen
  95. for (int i = 0; i != listSize; ++i){
  96. std::cout << vectorList[i].name << endl;// Print item name
  97. std::cout << std::setfill(' ') << std::setw(20);
  98. std::cout.fill(' ');
  99.  
  100. std::cout << std::setfill(' ') << std::setw(5);// Print item quantity
  101. std::cout << vectorList[i].quantity << endl;
  102. std::cout.fill(' ');
  103.  
  104. std::cout << std::setfill(' ') << std::setw(5);// Print regular price of item
  105. std::cout << vectorList[i].regPrice << endl;
  106. std::cout.fill(' ');
  107.  
  108. std::cout << std::setfill (' ') << std::setw(5);// Print sale price of item
  109. std::cout << vectorList[i].salePrice << endl;
  110. std::cout.fill(' ');
  111.  
  112. std::cout << vectorList[i].onSale << endl;// Print whether or not item is on sale
  113. std::cout.width(3);
  114. std::cout.fill(' ');
  115.  
  116. std::cout << vectorList[i].GrListItem::taxCategory << endl;// Print tax category
  117. std::cout.width(20);
  118. std::cout.fill(' ');
  119. }
  120.  
  121. }
  122.  
  123. // Constructor
  124. GrItem::GrItem(string name, int quantity, float regPrice, float salePrice, bool onSale, enum GrListItem::taxCategory taxCategory){
  125.  
  126. name = name;
  127. quantity = quantity;
  128. regPrice = regPrice;
  129. salePrice = salePrice;
  130. onSale = onSale;
  131. enum GrListItem::taxCategory tax = taxCategory;
  132.  
  133. };
  134.  
  135. // Default constructor
  136. GrItem::GrItem() {
  137. }
  138.  
  139. // Get the total cost before the tax
  140. void getTotBeforeTax(double itemPrice){
  141. double costBeforeTax = 0.0;
  142. costBeforeTax += itemPrice;
  143.  
  144. }
  145.  
  146. // Get the total amount of tax for each category
  147. void getTotTaxCategory(){
  148.  
  149.  
  150. }
  151.  
  152. // Get the total after tax
  153. void getTotAfterTax(){
  154.  
  155.  
  156. }
  157.  
  158. // Get customer savings (total of all differences between regular price and sale price for items that are currently on sale)
  159. void getCustSave(){
  160.  
  161.  
  162. }
  163.  
  164. // Function that is called to sort by name
  165. bool sortByName(const GrListItem &lhs, const GrListItem &rhs){
  166. return lhs.name < rhs.name;
  167. }
  168.  
  169. // Function that is called to sort by quantity
  170. bool sortByQuantity(const GrListItem &lhs, const GrListItem &rhs){
  171. return lhs.quantity < rhs.quantity;
  172. }
  173.  
  174. // Function that is called to sort by regular price
  175. bool sortByRegPrice(const GrListItem &lhs, const GrListItem &rhs){
  176. return lhs.regPrice < rhs.regPrice;
  177. }
  178.  
  179. // Function that is called to sort by sale price
  180. bool sortBySalePrice(const GrListItem &lhs, const GrListItem &rhs){
  181. return lhs.salePrice < rhs.salePrice;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement