Advertisement
noobdoob

Untitled

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