// CSE 142, Autumn 2009, Marty Stepp // This program uses variables to compute the bill at a restaurant. // This third version uses good methods for structure // and also uses System.out.printf to format the numbers. import java.util.*; public class Receipt3 { public static void main(String[] args) { Scanner console = new Scanner(System.in); double subtotal = meals(console); calculateTotal(subtotal); } // Reads the data for the people's meals and returns // the subtotal for all meals. 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; } // Calculate/print total owed, assuming 8% tax / 15% tip public static void calculateTotal(double subtotal) { double tax = subtotal * .08; double tip = subtotal * .15; double total = subtotal + tax + tip; // print the various cash amounts in a proper format System.out.printf("Subtotal: $%6.2f\n", subtotal); System.out.printf("Tax: $%6.2f\n", tax); System.out.printf("Tip: $%6.2f\n", tip); System.out.printf("Total: $%6.2f\n", total); } }