Advertisement
noobdoob

Untitled

Apr 28th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip> // input/output manipulation
  3. #include <string>
  4. #include <fstream>
  5. #include "Product.h"
  6.  
  7. using namespace std;
  8.  
  9.  
  10. Product **products;
  11.  
  12. int numProducts = 0;
  13.  
  14. //complete setup done here!
  15. void setup()
  16. {
  17. ifstream finput("products.txt");
  18. //get # of products first.
  19. finput >> numProducts;
  20. products = new Product* [numProducts];
  21.  
  22. //get product codes, names & prices.
  23. for(int i=0; i<numProducts; i++) {
  24. //finput >> codes[i] >> products[i] >> prices[i];
  25. products[i] = new Product;
  26. finput >> products[i]->code >> products[i]->name >> products[i]->price;
  27. }
  28. }
  29.  
  30. //list all the products available.
  31. void listProducts()
  32. {
  33. //list all the available products.
  34. cout << "Available products:\n";
  35. for(int i=0; i<numProducts; i++)
  36. // cout << codes[i] << ": " << products[i] << " @ " << prices[i] << "/pound.\n";
  37. cout << products[i]->code << ": " << products[i]->name << " @ "
  38. << products[i]->price << "/pound.\n";
  39. }
  40.  
  41. //return index of product with specified PLU
  42. //return -1 if not found
  43. int findIndex(int inputCode)
  44. {
  45. //look for inputCode in codes[] array
  46. //return corresponding index!
  47. for(int i=0; i<numProducts; i++)
  48. if (inputCode == products[i]->code)
  49. return i;
  50.  
  51. return -1;
  52. }
  53.  
  54. int getUserSelection()
  55. {
  56. int plu, selection = 0;
  57.  
  58. while (true)
  59. {
  60. listProducts();
  61. cout << "Enter PLU (0 to end): ";
  62. cin >> plu;
  63. if (plu == 0)
  64. return -1;
  65. selection = findIndex(plu);
  66. if (selection >= 0)
  67. return selection;
  68.  
  69. cout << "Invalid selection.\n";
  70. }
  71. }
  72.  
  73. //checkout functionality for one customer
  74. //returns the total amount
  75. double checkout()
  76. {
  77. double total = 0, weight = 0;
  78.  
  79. cout <<fixed << setprecision(2);
  80. while (true)
  81. {
  82. int selection = getUserSelection();
  83.  
  84. if (selection < 0)
  85. break;
  86.  
  87. cout << "Enter weight for " << products[selection]->name <<": ";
  88. cin >> weight;
  89.  
  90. total += weight * products[selection]->price;
  91.  
  92. cout << "Total so far: $ " << total << endl;
  93. }
  94.  
  95. cout << "Final total: $ " << total << endl;
  96. return total;
  97. }
  98.  
  99. int main()
  100. {
  101. double grandTotal = 0;
  102. char response = 'n';
  103.  
  104. setup();
  105.  
  106. while (response == 'n')
  107. {
  108. grandTotal += checkout();
  109. cout << "(n)ext customer OR (c)lose the register? ";
  110. cin >> response;
  111. }
  112.  
  113. cout << "Grand total: $" << grandTotal << endl;
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement