fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.Scanner;
  8.  
  9. import java.text.DecimalFormat;
  10.  
  11. // This program will calculate the cost of someone's order at a coffee shop with applied possible discounts and tax
  12.  
  13. class CoffeeShopWithMethods
  14. {
  15.  
  16. static Scanner user_input = new Scanner(System.in);
  17. static double Discounted_cost = 0.0;
  18. static double Total_cost = 0.0;
  19. static double tax = 0.0;
  20. static double cost = 0.0;
  21. static double discount = 0.0;
  22. public static void main (String [] args)
  23. {
  24. String username;
  25. System.out.print("\nEnter your username: ");
  26. username = user_input.nextLine();
  27. System.out.print ("\nWelcome to Casey's Classic Coffee, " + username + "! \n");
  28.  
  29. //call methods
  30. displayMenu();
  31. displayOutput();
  32.  
  33. System.out.println("\nThank you " + username + "! Have a nice day!");
  34. }
  35.  
  36. //outputs the menu to the screen
  37. public static void displayMenu()
  38. {
  39. System.out.println ("\n\tItem\t\tCost\n\t1. Coffee\t$1.50\n\t2. Latte\t$3.50\n\t3. Cappuccino\t$3.25\n\t4. Espresso\t$2.00");
  40. }
  41.  
  42. //prompts the user to enter item number, returns user input
  43. public static int getItemNumber() //error: ; expected
  44. {
  45. System.out.print ("\nPlease enter the desired item number: ");
  46. return user_input.nextInt();
  47. }
  48.  
  49. //prompts user to enter quantity, returns user input
  50. public static int getQuantity() //error: ; expected
  51. {
  52. System.out.print ("\nPlease enter the quantity: ");
  53. return user_input.nextInt();
  54. }
  55.  
  56. //takes the item number and quantity and returns the subtotal
  57. public static double computeSubTotal () //error: ; expected
  58. {
  59. int number = getItemNumber();
  60. int quantity = getQuantity();
  61.  
  62. // Used final double in order to make coffee shop items constant
  63. final double COFFEE = 1.50;
  64. final double LATTE = 3.50;
  65. final double CAPPUCCINO = 3.25;
  66. final double ESPRESSO = 2.00;
  67.  
  68. //double cost = 0;
  69.  
  70. if (number == 1)
  71. cost = quantity * COFFEE;
  72. else if (number == 2)
  73. cost = quantity * LATTE;
  74. else if (number == 3)
  75. cost = quantity * CAPPUCCINO;
  76. else if (number == 4)
  77. cost = quantity * ESPRESSO;
  78. return cost;
  79. }
  80.  
  81. //takes the subtotal and returns true if the user earned a discount; otherwise, returns false
  82. public static boolean discountCheck () //error: ; expected
  83. {
  84. if (cost >= 10)
  85. return true;
  86. return false;
  87. }
  88.  
  89. //takes the subtotal and returns the dollar amount of the discount earned by the user
  90. public static double computeDiscount () //error: ; expected
  91. {
  92. if (discountCheck())
  93. {
  94. discount = cost * 0.10;
  95. }
  96. else
  97. {
  98. discount = 0;
  99. }
  100. return discount;
  101. }
  102.  
  103. //takes the subtotal and the discount amount and returns the price after the discount is applied
  104. public static double computePriceAfterDiscount () //error: ; expected
  105. {
  106. //double discount = 0;
  107. //double Discounted_cost = 0;
  108.  
  109. Discounted_cost = cost - discount;
  110. return Discounted_cost;
  111. }
  112.  
  113. //takes the prices after the discount is applied and tax rate and returns the tax amount
  114. public static double computeTax() //error: ; expected
  115. {
  116. tax = Discounted_cost * 0.07;
  117. return tax;
  118. }
  119.  
  120. //takes the price after the discount is applied and the tax amount and returns the final total
  121. public static double computeTotal() //says ; expected
  122. {
  123. Total_cost = Discounted_cost + tax;
  124. return Total_cost;
  125. }
  126.  
  127. //takes the subtotal, discount amount, price after discount, tax, and final total and displays all the lines of output to the user
  128. public static void displayOutput() //says ; expected at the end of method header
  129. {
  130. //call methods
  131. cost = computeSubTotal();
  132. discount = computeDiscount();
  133. Discounted_cost = computePriceAfterDiscount();
  134. tax = computeTax();
  135. Total_cost = computeTotal();
  136.  
  137. System.out.printf ("\nTotal before discount and tax: $%.2f\n ", cost);
  138.  
  139. System.out.printf("\nCalculated discount: $%.2f\n", discount);
  140. System.out.printf("\nTotal after special discount: $%.2f\n", Discounted_cost);
  141.  
  142. System.out.printf("\nTax: $%.2f\n", tax);
  143. System.out.printf ("\nTotal cost: $%.2f\n", Total_cost);
  144. }
  145. }
Success #stdin #stdout 0.11s 380736KB
stdin
u
3
2
stdout
Enter your username: 
Welcome to Casey's Classic Coffee, u! 

	Item		Cost
	1. Coffee	$1.50
	2. Latte	$3.50
	3. Cappuccino	$3.25
	4. Espresso	$2.00

Please enter the desired item number: 
Please enter the quantity: 
Total before discount and tax: $6.50
 
Calculated discount: $0.00

Total after special discount: $6.50

Tax: $0.46

Total cost: $6.96

Thank you u! Have a nice day!