Advertisement
PurePureMilk

Inventory Processing

Jul 14th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. // Inventory Processing.cpp : Defines the entry point 4 the console application.
  2. //
  3.  
  4. //1st I make sure I have the headers files I need
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <sstream>
  10. #include <iomanip>
  11. #include <vector>
  12. #include <stdlib.h>
  13.  
  14. using namespace std;
  15.  
  16. double calcProductsNonTax(string, double, string isTax, int);
  17. double calcProductsTax(string, double, string isTax, int);
  18. double calcTax(string, double, string isTax, int);
  19.  
  20. bool isValid(int);
  21.  
  22.  
  23. int main()
  24. {
  25.  
  26. //2nd I make sure I can open the file. I use 140-grades code as a reference
  27.  
  28. ifstream infile;
  29. ofstream outputfile;
  30. string line; //one line of text
  31. string token; //ehhh i will just try with this...
  32.  
  33. string receiptName;
  34.  
  35. int tokenCount = 0;
  36.  
  37. cout << "Enter the name of your receipt" << endl;
  38. getline(cin, receiptName);
  39. receiptName = receiptName + ".txt";
  40. cout << "your receipt name is " << receiptName << endl;
  41.  
  42. infile.open("Minimart-Inventory.txt"); //oops I 4got the .txt and it gave me an error message XD
  43. outputfile.open(receiptName);
  44.  
  45. int i = 0;
  46.  
  47. string priceTemp[26]; //to be converted
  48.  
  49. string item[26];
  50. double price[26];
  51. string tax[26];
  52.  
  53. //double helpMe;
  54.  
  55. //menu variables
  56. string enterAnother = "y";
  57. int userNum;
  58. double total = 0;
  59. double totalTax = 0;
  60. double grandTotalTax = 0;
  61. double grandTotal = 0;
  62.  
  63. string nonTaxItem[1000];
  64. int nonTaxNumItems[1000];
  65. double nonTaxPrice[1000];
  66. string nonTaxTax[1000];
  67.  
  68. string taxItem[1000];
  69. int taxNumItems[1000];
  70. double taxPrice[1000];
  71. string taxTax[1000];
  72.  
  73. int iNonTax = 0;
  74. int iTax = 0;
  75. int units;
  76. int totalUnits = 0;
  77. int totalOrdersTax = 0;
  78. int totalOrdersNonTax = 0;
  79.  
  80. bool isValidCode;
  81.  
  82. cout << "below are available items you can purchase" << endl;
  83. cout << endl;
  84.  
  85. if (!infile)
  86. {
  87. cout << "Could not open file to read. Ending Program" << endl;
  88. return -1;
  89. }
  90.  
  91. while (!infile.eof()) //will return falses if you aren't at the the end of a file. so, it needs to true by placing ! 4 actions to keep occuring
  92. {
  93. getline(infile, line); //would need #include string
  94. //debugging // cout << "line: " << line << endl; //would need #include string
  95. stringstream ss(line);
  96.  
  97. while (getline(ss, token, ' '))
  98. {
  99. switch (tokenCount)
  100. {
  101. tokenCount = 0;
  102. case 0:
  103. cout << "* * * * * * * * * * * * * * * * * " << endl;
  104. cout << i << ") ";
  105. cout << "Item: " << token;
  106. item[i] = (token);
  107. break;
  108. case 1:
  109. cout << "; Price: " << token;
  110. priceTemp[i] = (token);
  111. break;
  112. case 2:
  113. cout << "; Taxability: " << token << endl;
  114. tax[i] = (token);
  115. }
  116. tokenCount++;
  117. }
  118. i++;
  119. tokenCount = 0;
  120.  
  121. }
  122.  
  123. for (int y = 0; y < 26; y++)
  124. {
  125. price[y] = atof(priceTemp[y].c_str());
  126. //cout << price[y] << endl; //debug if it is stored
  127. }
  128.  
  129. //cout << "done with inputting data from file... time to convert!" << endl;
  130. cout << endl;
  131. cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
  132. cout << "above are available items you can purchase" << endl;
  133. cout << "input the corresponding number to the item to make your purchase" << endl;
  134. cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * " << endl;
  135.  
  136. //debug if the items are still there
  137. /*
  138. for (int e = 0; e < 26; e++)
  139. {
  140. cout << item[e] << endl;
  141. cout << price[e] << endl;
  142. cout << tax[e] << endl;
  143. }
  144. */
  145.  
  146. while (enterAnother == "y")
  147. {
  148. cout << "Enter your number (0-25): ";
  149. cin >> userNum;
  150.  
  151. isValidCode = isValid(userNum);
  152.  
  153. while (isValidCode == false)
  154. {
  155. cout << "re-enter please" << " ";
  156. cin >> userNum;
  157. isValidCode = isValid(userNum);
  158. }
  159.  
  160. if (tax[userNum] == "N")
  161. {
  162. nonTaxItem[iNonTax] = item[userNum];
  163. nonTaxPrice[iNonTax] = price[userNum];
  164. nonTaxTax[iNonTax] = tax[userNum];
  165.  
  166. cout << "You ordered this item: ";
  167. cout << nonTaxItem[iNonTax] << " ";
  168. cout << nonTaxPrice[iNonTax] << " ";
  169. cout << nonTaxTax[iNonTax] << " " << endl;
  170.  
  171.  
  172. cout << "how many units of this item do you want? ";
  173. cin >> units;
  174. while (units < 0)
  175. {
  176. cout << "Invalid, enter a number that is positive: ";
  177. cin >> units;
  178. }
  179. cout << endl;
  180. totalUnits = totalUnits + units;
  181. //cout << "you ordered this many units " << units << endl;
  182. nonTaxNumItems[iNonTax] = units;
  183.  
  184. iNonTax++;
  185.  
  186. total = calcProductsNonTax(item[userNum], price[userNum], tax[userNum], units);
  187. cout << "Total is (from main) " << "$" << total << endl;
  188.  
  189. totalOrdersNonTax = totalOrdersNonTax + 1;
  190. cout << "this is totalOrdersNonTax " << totalOrdersNonTax << endl;
  191.  
  192.  
  193. }
  194. else if (tax[userNum] == "T")
  195. {
  196. taxItem[iTax] = item[userNum];
  197. taxPrice[iTax] = price[userNum];
  198. taxTax[iTax] = tax[userNum];
  199.  
  200. cout << "You ordered this item: ";
  201. cout << taxItem[iTax] << " ";
  202. cout << taxPrice[iTax] << " ";
  203. cout << taxTax[iTax] << " " << endl;
  204.  
  205.  
  206. cout << "how many units of this item do you want? ";
  207. cin >> units;
  208. cout << endl;
  209. totalUnits = totalUnits + units;
  210. //cout << "you ordered this many units " << units << endl;
  211. taxNumItems[iTax] = units;
  212.  
  213. iTax++;
  214.  
  215. totalTax = calcTax(item[userNum], price[userNum], tax[userNum], units);
  216. total = calcProductsTax(item[userNum], price[userNum], tax[userNum], units);
  217. cout << "Total is (from main) " << setprecision(2) << fixed << "$" << total << endl;
  218.  
  219. totalOrdersTax = totalOrdersTax + 1;
  220. cout << "This is the totalOrdersTax " << totalOrdersTax << endl;
  221.  
  222. grandTotalTax = grandTotalTax + totalTax;
  223. }
  224.  
  225. cout << endl;
  226. grandTotal = grandTotal + total;
  227. cout << "Grand Total Tax is " << setprecision(2) << fixed << "$" << grandTotalTax << endl;
  228. cout << "Grand total is (w/o tax)" << setprecision(2) << fixed << "$" << grandTotal << endl;
  229. //cout << "The total number of items you bought are " << totalUnits << endl;
  230. cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * " << endl;
  231. cout << "do you want to continue another purchase?" << endl;
  232. cout << "(Press y to continue or press n to end then press enter)" << endl;
  233. cin >> enterAnother;
  234. cin.ignore();
  235. cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
  236.  
  237. if (!(enterAnother == "y"))
  238. {
  239. cout << "Program ending, Here's you receipt saved as " << receiptName << endl;
  240. cout << "These are your purchased items: " << endl;
  241. outputfile << "These are your purchased items: " << endl;
  242. grandTotal = grandTotal + grandTotalTax;
  243.  
  244. int u = 0;
  245. while (u < totalOrdersNonTax)
  246. {
  247. cout << nonTaxItem[u] << " ";
  248. outputfile << nonTaxItem[u] << " ";
  249.  
  250. cout << "x" << nonTaxNumItems[u] << " ";
  251. outputfile << "x" << nonTaxNumItems[u] << " ";
  252.  
  253. cout << setprecision(2) << fixed << "$" << nonTaxPrice[u] << " ";
  254. outputfile << setprecision(2) << fixed << "$" << nonTaxPrice[u] << " ";
  255.  
  256. cout << nonTaxTax[u] << " " << endl;
  257. outputfile << nonTaxTax[u] << " " << endl;
  258.  
  259. u++;
  260. }
  261.  
  262. int o = 0;
  263. while (o < totalOrdersTax)
  264. {
  265. cout << taxItem[o] << " ";
  266. outputfile << taxItem[o] << " ";
  267.  
  268. cout << "x" << taxNumItems[o] << " ";
  269. outputfile << "x" << taxNumItems[o] << " ";
  270.  
  271. cout << setprecision(2) << fixed << "$" << taxPrice[o] << " ";
  272. outputfile << setprecision(2) << fixed << "$" << taxPrice[o] << " ";
  273.  
  274. cout << taxTax[o] << " " << endl;
  275. outputfile << taxTax[o] << " " << endl;;
  276.  
  277. o++;
  278. }
  279. cout << endl;
  280. outputfile << endl;
  281. cout << "Here's the final tax cost " << setprecision(2) << fixed << "$" << grandTotalTax << endl;
  282. outputfile << "Here's the final tax cost " << setprecision(2) << fixed << "$" << grandTotalTax << endl;
  283.  
  284. cout << "Here's the final cost with tax added " << setprecision(2) << fixed << "$" << grandTotal << endl;
  285. outputfile << "Here's the final cost with tax added " << setprecision(2) << fixed << "$" << grandTotal << endl;
  286.  
  287. cout << "Thank you for your online order." << endl;
  288. outputfile << "Thank you for your online order." << endl;
  289.  
  290. }
  291.  
  292. }
  293.  
  294. infile.close();
  295. outputfile.close();
  296.  
  297. return 0;
  298. }
  299.  
  300. double calcProductsNonTax(string item, double priceInDollars, string isTax, int numOfItems)
  301. {
  302. double total;
  303.  
  304. total = priceInDollars * numOfItems;
  305. //cout << "The total is (from function)" << setprecision(2) << fixed << "$" << total << endl;
  306.  
  307. return total;
  308. }
  309.  
  310. double calcProductsTax(string item, double priceInDollars, string isTax, int numOfItems)
  311. {
  312. double total;
  313.  
  314. total = priceInDollars * numOfItems;
  315. //cout << "The total is (from function)" << setprecision(2) << fixed << "$" << total << endl;
  316.  
  317. return total;
  318. }
  319.  
  320. double calcTax(string item, double priceInDollars, string isTax, int numOfItems)
  321. {
  322. double tax;
  323. tax = priceInDollars * 0.06 * numOfItems;
  324. cout << "The tax for this item is " << setprecision(2) << fixed << "$" << tax << endl;
  325. return tax;
  326. }
  327.  
  328. bool isValid(int userNum)
  329. {
  330. while (userNum < 0 || userNum > 25)
  331. {
  332. cout << "Invalid, enter a number from 0-25: ";
  333. return false;
  334. }
  335. return true;
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement