// CSE 142, Spring 2010, Marty Stepp // This is an improved version of the "Receipt" program from week 2. // Now it is more flexible because it uses the Scanner and a cumulative sum. // This version also uses static methods with parameters and returns. import java.util.*; // for Scanner public class Receipt3 { public static final int TAX_PERCENT = 8; public static final int TIP_PERCENT = 15; public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("How many people ate? "); int people = console.nextInt(); double subtotal = readMeals(console, people); results(subtotal); } public static double readMeals(Scanner console, int people) { // cumulative sum of each person's meal cost double subtotal = 0.00; for (int i = 1; i <= people; i++) { System.out.print("Person #" + i + ": How much did your dinner cost? "); double cost = console.nextDouble(); subtotal = subtotal + cost; } return subtotal; } public static void results(double subtotal) { double tax = TAX_PERCENT / 100.0 * subtotal; double tip = TIP_PERCENT / 100.0 * subtotal; double total = subtotal + tax + tip; System.out.printf("Subtotal = $%6.2f\n", subtotal); System.out.printf("Tax %3d%% = $%6.2f\n", TAX_PERCENT, tax); System.out.printf("Tip %3d%% = $%6.2f\n", TIP_PERCENT, tip); System.out.printf("Total = $%6.2f\n", total); } }