// CSE 142, Autumn 2009, Marty Stepp // This program uses variables to compute the bill at a restaurant. // This third and final version is improved by adding static methods. import java.util.*; public class Receipt3 { public static void main(String[] args) { Scanner console = new Scanner(System.in); double subtotal = meals(console); results(subtotal); } // Prompts for each person's meal cost and returns the subtotal. public static double meals(Scanner console) { System.out.print("How many people ate? "); int howMany = console.nextInt(); double subtotal = 0.0; // read each meal's cost and add them using a cumulative sum for (int i = 1; i <= howMany; i++) { System.out.print("Person #" + i + ": How much did your dinner cost? "); double cost = console.nextDouble(); subtotal = subtotal + cost; // subtotal += cost; } return subtotal; } // Calculates total owed for the given subtotal, // assuming 8% tax and 15% tip public static void results(double subtotal) { double tax = subtotal * .08; double tip = subtotal * .15; double total = subtotal + tax + tip; System.out.println("Subtotal: $" + subtotal); System.out.println("Tax: $" + tax); System.out.println("Tip: $" + tip); System.out.println("Total: $" + total); } }