Advertisement
SmellyBadger

Untitled

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