Advertisement
SmellyBadger

Untitled

Oct 30th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. // DO_2: Define the constant APPLE_PRICE, ORANGE_PRICE and WIDTH
  8. const float W_MELON_PRICE = 30.00f;
  9. const float MANGO_PRICE = 30.25f;
  10. const float GRAPES_PRICE = 35.25f;
  11. const float APPLE_PRICE = 20.00f;
  12. const float ORANGE_PRICE = 25.25f;
  13. const float WIDTH = 10.00f;
  14.  
  15. // state taxes
  16. const float IN_TAX = 0.04f;
  17. const float IL_TAX = 0.05f;
  18. const float WI_TAX = 0.06f;
  19.  
  20. // price discounts
  21. const float DISCOUNT_TWO = 0.12F;
  22. const float DISCOUNT_THREE = 0.13F;
  23. const float DISCOUNT = 0.1F;
  24.  
  25. // DO_3: Complete the prototype for Calculate_Total function and
  26. // Calculate_Discount function, See function definition and
  27. // functions' calls below.
  28. void Display_Price(int code);
  29. float Calculate_Tax(float total);
  30. float Calculate_Total(int code, float total);
  31. float Calculate_Discount(float total);
  32. // functions for getting and checking the variable code
  33. int Get_Code();
  34. bool Valid_Code(int code);
  35.  
  36. int main()
  37. {
  38. int code;
  39. float total = 0;
  40. cout << "=========Welcome to our Market========" << endl;
  41.  
  42. // DO_4: Provide the priming read for the Sentinel Control Loop
  43. // Look at the while statement below for details on what is
  44. // necessary. Hint, you need to call get_code() function before
  45. // entering the while loop.
  46.  
  47. code = Get_Code();
  48. while (code != -1)
  49. {
  50. // DO_5: Call function Display_Price
  51. Display_Price(code);
  52. total = Calculate_Total(code, total);
  53.  
  54. // DO_6: Call function Get_Code ().
  55. // Assign the returned value to code
  56. code = Get_Code();
  57. cout << endl;
  58.  
  59. }
  60.  
  61. cout << "Total: " << total << endl;
  62. total -= Calculate_Discount(total);
  63. cout << "Total with discount: " << total << endl;
  64. total += Calculate_Tax(total);
  65. cout << "Total with tax: " << total << endl;
  66. cout << endl;
  67. return 0;
  68.  
  69. }
  70.  
  71. //---------------------------------------------------------------------
  72. // Function to display the price for the selections
  73. // params: in
  74. //-------------------------------------------------------------------
  75.  
  76. void Display_Price(int code)
  77. {
  78. cout << setw(WIDTH) << code << setw(WIDTH);
  79.  
  80. if (code == 101)
  81. cout << "Apple" << setw(WIDTH) << '$' << APPLE_PRICE << endl;
  82. else if (code == 102)
  83. cout << "Orange" << setw(WIDTH) << '$' << ORANGE_PRICE << endl;
  84.  
  85. // DO_7: Complete the function body for display_price.
  86. // Check the other values of 103, 104 for the variable code.
  87. // Set the width for each output according to the
  88. // sample run in Lab6.txt
  89.  
  90. else if (code == 103)
  91. cout << "Grapes" << setw(WIDTH) << '$' << GRAPES_PRICE << endl;
  92. else if (code == 104)
  93. cout << "W.Melon" << setw(WIDTH) << '$' << W_MELON_PRICE << endl;
  94.  
  95.  
  96. else
  97. cout << "Mango" << setw(WIDTH) << '$' << MANGO_PRICE << endl;
  98. }
  99.  
  100. //---------------------------------------------------------------------
  101. // Function to calculate the total
  102. // parms: in, in
  103. //-------------------------------------------------------------------
  104. // DO_8: Complete the function header for Calculate_Total.
  105. // Check the function prototype for details on this function.
  106. float Calculate_Total(int code,float total )
  107. {
  108. if (code == 101)
  109. total += APPLE_PRICE;
  110. else if (code == 102)
  111. total += ORANGE_PRICE;
  112. else if (code == 103)
  113. total += GRAPES_PRICE;
  114. else if (code == 104)
  115. total += W_MELON_PRICE;
  116. else if (code == 105)
  117. total += MANGO_PRICE;
  118. else
  119. total += 0;
  120. return total;
  121. }
  122.  
  123. //---------------------------------------------------------------------
  124. // Function to calculate the tax for the total
  125. // also it asks for the state, applies new tax.
  126. // cin is used to get the "state" variable in this
  127. // function
  128. // params: in
  129. //---------------------------------------------------------------------
  130.  
  131. float Calculate_Tax(float total)
  132. {
  133. float tax = 0;
  134.  
  135. string state;
  136. bool state_recognized = false;
  137.  
  138. while (!state_recognized && total != 0)
  139. {
  140. cout << endl << "Please enter the state you are in : ";
  141. cin >> state;
  142. cout << endl;
  143.  
  144. if (state == "Indiana" || state == "indiana" || state == "IN")
  145. {
  146. tax = total * IN_TAX;
  147. state_recognized = true;
  148. }
  149. else if (state == "Illinois" || state == "illinois"
  150. || state == "IL")
  151. {
  152. tax = total * IL_TAX;
  153. state_recognized = true;
  154. }
  155. else if (state == "Wisconsin" || state == "wisconsin"
  156. || state == "WI")
  157. {
  158. tax = total * WI_TAX;
  159. state_recognized = true;
  160. }
  161. else
  162. {
  163. cout << endl << "State not recognized." ;
  164. state_recognized = false;
  165. }
  166.  
  167. }
  168.  
  169. return tax;
  170. }
  171.  
  172. //---------------------------------------------------------------------
  173. // Function to calculate the discount if the total >$100.
  174. // discount conditions for beween $100 and $200,
  175. // between $200 and $350, and greater than 350
  176. // params: in
  177. //---------------------------------------------------------------------
  178.  
  179. float Calculate_Discount(float total)
  180. {
  181. if (total < 100)
  182. return 0;
  183. else if (total >= 100 && total < 200)
  184. return total * DISCOUNT;
  185. else if (total >= 200 && total < 350)
  186. return total * DISCOUNT_TWO;
  187. else
  188. return total * DISCOUNT_THREE;
  189.  
  190. }
  191.  
  192. //---------------------------------------------------------------------
  193. // function that gets the code, and then validates the code by
  194. // using the Valid_Code() function. The code loops until a valid code
  195. // or the exit command is entered.
  196. // params: in
  197. //---------------------------------------------------------------------
  198.  
  199. int Get_Code()
  200. {
  201. int out_code;
  202. bool good_code = false;
  203. while (!good_code)
  204. {
  205. cout << endl << "Enter Code:";
  206. cin >> out_code;
  207. good_code = Valid_Code(out_code);
  208. }
  209.  
  210. return out_code;
  211.  
  212. }
  213.  
  214. //---------------------------------------------------------------------
  215. // function to check whether the code is valid, using the same
  216. // bounds as the checking in the other functions, and replaces the
  217. // redundant error output from the Display_Price() function
  218. // params: in
  219. //---------------------------------------------------------------------
  220.  
  221. bool Valid_Code(int code)
  222. {
  223. if (code >= 101 && code <= 105 || code == -1)
  224.  
  225. return true;
  226.  
  227. else
  228. {
  229. cout << "Incorrect code !!!! " << endl;
  230. return false;
  231. }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement