Advertisement
K_Wall_24

Project2

Nov 24th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.86 KB | None | 0 0
  1. /*
  2. * Program: Project2.java
  3. * Author: K. Kowalsky
  4. * Date: Nov 21, 2012
  5. * Description: This program will emulate a self-serve checkout, with the user entering prices for items, and the program calculating prices and change. A breakdown of the program:
  6. *
  7. * Declare the variables and scanner input;
  8. * print the initial blurb to the screen and ask for the user's name;
  9. * create a new array list for all the prices entered;
  10. * create a while statement so that as long as the price entered is not equal to -1, everything will continue to function;
  11. * in the while statement, ask for the item price, and store the input, and create an if-else-if statement where:
  12. * if the price entered is valid, we add it to the array of prices, copy the value to the previousPrice variable, calculate the current total, and add one to itemNumber to display when asking for the price;
  13. * if the price entered is not valid, we say so and return to the beginning of the while statement, and do not update anything;
  14. * if the price entered is zero, we know the customer wants to remove the last item, so we remove it from the total price, and we remove it from the array. We then print the results of these actions;
  15. * as soon as the price entered is -1, we exit the while loop and continue the program;
  16. * we print the array of prices;
  17. * create the method calculateHST to calculate the amount of tax on the purchase and use it;
  18. * create the method roundToTwoPlaces and use it to round the calculated tax and final price;
  19. * print the final values to screen, and ask the user the total amount the will be paying with;
  20. * calculate the amount of change needed, and how much change of each type is to be given using many while statements(I'm sure this is not the most efficient design, but it works nonetheless);
  21. * finally, display the amount of each type of change to be given, and display the final lines of text
  22. */
  23.  
  24. import java.util.Scanner;
  25. import java.util.ArrayList;
  26.  
  27. public class Kyle_Project2
  28. {
  29. public static void main(String[] args)
  30. {
  31. //New Scanner input, declare most variables
  32. Scanner input = new Scanner(System.in);
  33. double previousPrice = 0;
  34. double priceEntered = 0;
  35. double currentTotal = 0;
  36. double taxOnly = 0;
  37. double returnChange = 0;
  38. double newChange = 0;
  39. double previousTotal = 0;
  40. int pennies = 0;
  41. int nickels = 0;
  42. int dimes = 0;
  43. int quarters = 0;
  44. int loonies = 0;
  45. int toonies = 0;
  46. int fives = 0;
  47. int tens = 0;
  48. int twenties = 0;
  49. int itemNumber = 1;
  50. int previousItemNumber = 0;
  51.  
  52. //Print the initial blurb, including asking for the users name, and using that during the program
  53. System.out.println("+-------------------------------------------+\n"
  54. + "| BigBox Depot Self-Serve Checkout |\n"
  55. + "+-------------------------------------------+\n\n"
  56. + "Hello. Welcome to the checkout. What's your name? ");
  57. String name = input.next();
  58. System.out.println("\nOkay, " + name + ", enter the price of each purchase in dollars and cents, and then push the ENTER key.\n"
  59. + "For example, if your item costs $5.99, enter 5.99.\n\n"
  60. + "If you make a mistake when you enter a price, enter a zero for the next entry,\nand the last price you entered will be subtracted from your running total.\n\n"
  61. + "When you've entered all of your prices, enter -1 to indicate that you've finished your entries.\nI'll then calculate what your total owing is.");
  62.  
  63. //Create a new array list for all the data entries
  64. ArrayList<Double> priceArray = new ArrayList<Double>();
  65.  
  66. //As long as the price entered is not equal to -1, everything will continue to function
  67. while (priceEntered != -1)
  68. {
  69. //Ask for the item price, and store the input
  70. System.out.println("\nEnter a price for item #" + itemNumber + ": $");
  71. priceEntered = input.nextDouble();
  72. if (priceEntered > 0 && priceEntered <= 2999.99)
  73. {
  74. //If the price entered is valid, we add it to the array, copy the value to the previousPrice variable, calculate the current total, and add one to itemNumber to display when asking for the price.
  75. priceArray.add(priceEntered);
  76. previousPrice = priceEntered;
  77. currentTotal = currentTotal + priceEntered;
  78. System.out.println("That was $" + priceEntered + ". Your current total is $" + currentTotal + ".");
  79. itemNumber ++;
  80. previousItemNumber = itemNumber - 1;
  81. }
  82. else if (priceEntered >= 3000)
  83. {
  84. //If the price entered is not valid, we say so and return to the beginning of the while statement, and do not update anything.
  85. System.out.println("INVALID PRICE: Please Re-Enter...");
  86. }
  87. else if (priceEntered == 0)
  88. {
  89. //If the price entered is zero, we know the customer wants to remove the last item, so we remove it from the total price, and we remove it from the array. We then print the results of these actions.
  90. previousTotal = currentTotal - previousPrice;
  91. currentTotal = previousTotal;
  92. priceArray.remove(previousPrice);
  93. System.out.println("Zero entered: Removing last item #" + previousItemNumber +", $"
  94. + previousPrice + ". Your total is now $" + currentTotal + ".");
  95.  
  96. }
  97. }
  98.  
  99. //As soon as the price entered is -1, we exit the while loop and continue the program
  100. System.out.println("-1 Entered. Now calculating your total owing...\n\n"
  101. + name + ", your purchases are:");
  102.  
  103. //Print all items in the array
  104. for(int i = 0; i < priceArray.size(); i++)
  105. {
  106. System.out.println(priceArray.get(i)); // Gets the element at index i
  107. }
  108.  
  109. //Calculate the tax using calculateHST
  110. taxOnly = calculateHST(currentTotal);
  111.  
  112. //Use roundToTwoPlaces method to get final values
  113. double taxFinal = roundToTwoPlaces(taxOnly);
  114. double priceWithTax = currentTotal + taxFinal;
  115. double finalPriceWithTax = roundToTwoPlaces(priceWithTax);
  116.  
  117. //Print the final values to screen, and ask/receive how much they are paying
  118. System.out.println("SUB-TOTAL: $" + currentTotal
  119. + "\n\nHarmonized Sales Tax on your purchases:"
  120. + "\nHST: $" + taxFinal
  121. + "\nGRAND TOTAL: $" + finalPriceWithTax
  122. + "\n\nHow much money are you presenting for your payment?");
  123. double paymentTotal = input.nextDouble();
  124.  
  125. //As long as the paid amount is greater than the purchase price, the program won't complain
  126. while (paymentTotal < finalPriceWithTax)
  127. {
  128. System.out.println("You're payment amount is lower than your total price."
  129. + "Please enter another amount.");
  130. paymentTotal = input.nextDouble();
  131. }
  132.  
  133. //Calculate the amount of change, and print it to screen
  134. returnChange = paymentTotal - finalPriceWithTax;
  135.  
  136. System.out.println("Your change is $" + returnChange + "\n");
  137.  
  138. //Figure out how much of each type of change are needed, using the largest denominations possible. While I am sure this is not the most efficient code, it works nonetheless
  139. while ((returnChange - 20) >= 0)
  140. {
  141. newChange = returnChange - 20;
  142. returnChange = newChange;
  143. twenties ++;
  144. }
  145. while ((returnChange - 10) >= 0)
  146. {
  147. newChange = returnChange - 10;
  148. returnChange = newChange;
  149. tens ++;
  150. }
  151. while ((returnChange - 5) >= 0)
  152. {
  153. newChange = returnChange - 5;
  154. returnChange = newChange;
  155. fives ++;
  156. }
  157. while ((returnChange - 2) >= 0)
  158. {
  159. newChange = returnChange - 2;
  160. returnChange = newChange;
  161. toonies ++;
  162. }
  163. while ((returnChange - 1) >= 0)
  164. {
  165. newChange = returnChange - 1;
  166. returnChange = newChange;
  167. loonies ++;
  168. }
  169. while ((returnChange - 0.25) >= 0)
  170. {
  171. newChange = returnChange - 0.25;
  172. returnChange = newChange;
  173. quarters ++;
  174. }
  175. while ((returnChange - 0.10) >= 0)
  176. {
  177. newChange = returnChange - 0.10;
  178. returnChange = newChange;
  179. dimes ++;
  180. }
  181. while ((returnChange - 0.05) >= 0)
  182. {
  183. newChange = returnChange - 0.05;
  184. returnChange = newChange;
  185. nickels ++;
  186. }
  187. while ((returnChange - 0.01) >= 0)
  188. {
  189. newChange = returnChange - 0.01;
  190. returnChange = newChange;
  191. pennies ++;
  192. }
  193.  
  194.  
  195. //Display the appropriate count of the different types of change. Again, not the most efficient, but it works just fine.
  196. while (twenties >= 1)
  197. {
  198. System.out.println(twenties + " $20 bills");
  199. twenties = 0;
  200. }
  201. while (tens >= 1)
  202. {
  203. System.out.println(tens + " $10 bills");
  204. tens = 0;
  205. }
  206. while (fives >= 1)
  207. {
  208. System.out.println(fives + " $5 bills");
  209. fives = 0;
  210. }
  211. while (toonies >= 1)
  212. {
  213. System.out.println(toonies + " Toonies");
  214. toonies = 0;
  215. }
  216. while (loonies >= 1)
  217. {
  218. System.out.println(loonies + " Loonies");
  219. loonies = 0;
  220. }
  221. while (quarters >= 1)
  222. {
  223. System.out.println(quarters + " Quarters");
  224. quarters = 0;
  225. }
  226. while (dimes >= 1)
  227. {
  228. System.out.println(dimes + " Dimes");
  229. dimes = 0;
  230. }
  231. while (nickels >= 1)
  232. {
  233. System.out.println(nickels + " Nickels");
  234. nickels = 0;
  235. }
  236. while (pennies >= 1)
  237. {
  238. System.out.println(pennies + " Pennies");
  239. pennies = 0;
  240. }
  241.  
  242.  
  243. //Print the final lines to screen.
  244. System.out.println("\nChange will be sent to the tray at the lower right."
  245. + "\n\nThank you for shopping at the BigBox Depot, " + name + ".");
  246.  
  247. input.close();
  248. }
  249.  
  250. /*
  251. * Method: roundToTwoPlace
  252. * Purpose: Round doubles to two decimal places
  253. * Accepts: One double - Number to round
  254. * Returns: One double - Rounded Number
  255. */
  256. public static double roundToTwoPlaces(double toRound)
  257. {
  258. int tempPrice = (int) (toRound * 100.0);
  259. toRound = (tempPrice / 100.0);
  260. return toRound;
  261. }
  262.  
  263. /*
  264. * Method: calculateHST
  265. * Purpose: Calculate the HST total
  266. * Accepts: One double - the current total to be taxed
  267. * Returns: One double - the amount of tax
  268. */
  269.  
  270. public static double calculateHST(double applyTax)
  271. {
  272. applyTax = applyTax * 0.13;
  273. return applyTax;
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement