// CSE 142, Autumn 2009, Marty Stepp // This program uses variables to compute the bill at a restaurant. // This second version is improved by prompting for how many people ate // and using a cumulative sum to add up the cost of their meals. import java.util.*; public class Receipt2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); 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; } // int subtotal = 38 + 40 + 30; System.out.println("Subtotal: " + subtotal); // Calculate total owed, assuming 8% tax / 15% tip 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); } }