Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <climits>
  4. using namespace std;
  5.  
  6. struct Product
  7. {
  8. string naam;
  9. double prijs;
  10. string beschrijving;
  11. };
  12.  
  13. char menu();
  14. Product leesProductIn();
  15. Product kiesProduct(Product[]);
  16. double berekenTotaal(Product[]);
  17. bool verwijderProduct(Product[]);
  18.  
  19. int main()
  20. {
  21. int teller = 0, teller2 = 0;
  22. Product plijst[5];
  23. Product mand[10];
  24. char keuze;
  25. keuze = menu();
  26. while (keuze != 'Q')
  27. {
  28. cout << endl;
  29. switch (keuze)
  30. {
  31. case 'A':
  32. plijst[teller] = leesProductIn(); teller++;
  33. break;
  34. case 'B':
  35. if (teller2<10)
  36. {
  37. mand[teller2] = kiesProduct(plijst);
  38. teller2++;
  39. }
  40. else {
  41. cout << "Je kan maar max. 10 producten toevoegen aan je winkelmand." << endl;
  42. }
  43. break;
  44. case 'C':
  45. if (teller2>0) {
  46. if (verwijderProduct(mand)) {
  47. teller2--;
  48. }
  49. }
  50. else {
  51. cout << "Je winkelmand is leeg!" << endl << endl;
  52. }
  53. break;
  54. case 'D':
  55. cout << "Totaal prijs: " << berekenTotaal(mand) << endl << endl;
  56. break;
  57. }
  58. keuze = menu();
  59. }
  60. return 0;
  61. }
  62.  
  63. char menu()
  64. {
  65. char keuze;
  66. cout << "A: Product toevoegen aan DB" << endl;
  67. cout << "B: Product toevoegen aan winkelmand" << endl;
  68. cout << "C: Product verwijderen uit winkelmand" << endl;
  69. cout << "D: Totaalprijs berekenen" << endl;
  70. cout << "Q: Quit" << endl;
  71. cin >> keuze;
  72. keuze = toupper(keuze);
  73. while (keuze<'A' || (keuze > 'D' && keuze != 'Q')) {
  74. cout << endl << "Foutieve invoer. Probeer opnieuw";
  75. cin >> keuze;
  76. }
  77. return keuze;
  78. }
  79.  
  80. Product leesProductIn()
  81. {
  82. Product p;
  83. cout << "Naam product: ";
  84. getline(cin, p.naam);
  85.  
  86. cout << "Prijs product: ";
  87. cin >> p.prijs;
  88.  
  89. cout << "Beschrijving product: ";
  90. getline(cin, p.beschrijving);
  91.  
  92. return p;
  93. }
  94.  
  95. Product kiesProduct(Product lijst[])
  96. {
  97. int keuze = 0;
  98. int teller = 0;
  99.  
  100. for (int i = 0; i<5; i++) {
  101. if (lijst[i].naam != "") {
  102. teller++;
  103. cout << "Product " << teller << ": " << lijst[i].naam << endl;
  104. cout << "Prijs: " << lijst[i].prijs << endl;
  105. cout << "Beschrijving: " << lijst[i].beschrijving << endl << endl;
  106. }
  107. }
  108. cout << "Welk product wilt u toevoegen?" << endl;
  109. cin >> keuze;
  110.  
  111.  
  112.  
  113. while (keuze<1 || keuze > teller) {
  114. cout << "Ongeldige keuze, probeer opnieuw: ";
  115. cin >> keuze;
  116. }
  117.  
  118.  
  119. return lijst[keuze - 1];
  120. }
  121.  
  122. bool verwijderProduct(Product mand[])
  123. {
  124. int keuze = 0;
  125. int teller = 0;
  126. for (int i = 0; i<10; i++) {
  127. if (mand[i].naam != "") {
  128. teller++;
  129. cout << "Product " << teller << ": " << mand[i].naam << endl;
  130. cout << "Prijs: " << mand[i].prijs << endl;
  131. cout << "Beschrijving: " << mand[i].beschrijving << endl << endl;
  132. }
  133. }
  134. cout << "Welk product wilt u verwijderen (0 om te annuleren)?" << endl;
  135. cin >> keuze;
  136. while (keuze<0 || keuze > teller) {
  137. cout << "Ongeldige keuze, probeer opnieuw: ";
  138. cin >> keuze;
  139. }
  140. cout << endl;
  141. if (keuze == 0) {
  142. return false;
  143. }
  144. for (int i = keuze - 1; i<8; i++) {
  145. mand[i] = mand[i + 1];
  146. mand[9] = Product{};
  147. }
  148. return true;
  149. }
  150.  
  151. double berekenTotaal(Product lijst[])
  152. {
  153. double prijs = 0;;
  154.  
  155. for (int i = 0; i<10; i++) {
  156. if (lijst[i].naam != "") {
  157. prijs += lijst[i].prijs;
  158. }
  159. }
  160. return prijs;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement