package CIS115; import java.util.Scanner; public class SentinelMenu { public static void main(String[] args) { int sum = 0; int count = 0; int x; String ans; ans = promptString("Have a number?"); while (ans.equals("y")) { x = promptInt("What number?"); count = count + 1; sum = sum + x; // annoying extra input each time ans = promptString("More numbers?"); } System.out.println("average: " + sum / count); // double input version sum = 0; // reset count = 0; final int STOP = -989; // only works if this is invalid as data value System.out.println("Enter " + STOP + " to quit"); // input before loop x = promptInt("What number?"); while (x != STOP) { count = count + 1; sum = sum + x; // input at bottom of loop x = promptInt("What number?"); } System.out.println("average: " + sum / count); // single input version sum = 0; // reset count = 0; // final int STOP = -989; // already did this in main System.out.println("Enter " + STOP + " to quit"); x = 0; // anything but STOP while (x != STOP) { // read at top of loop x = promptInt("What number?"); if (x != STOP) { count = count + 1; sum = sum + x; } } System.out.println("average: " + sum / count); double balance = 800.80; int menuOp = -1; while (menuOp != 0) { System.out.println("0 Quit"); System.out.println("1 Show Balance"); System.out.println("2 Deposit"); System.out.println("3 Withdraw"); menuOp = promptInt("Choice?"); switch (menuOp) { case 1: System.out.println("$" + balance); break; case 2: balance = balance + 5; break; case 3: balance = balance - 5; break; } } double checking = 189.99; double savings = 2068.77; menuOp = -1; // resetting from last example while (menuOp != 0) { System.out.println("0 Quit"); System.out.println("1 Show Balances"); System.out.println("2 Deposit"); System.out.println("3 Withdraw"); System.out.println("4 Transfer Menu"); menuOp = promptInt("Choice?"); if (menuOp == 1) { System.out.println("Checking: $" + checking); System.out.println("Savings: $" + savings); } else if (menuOp == 2) { checking = checking + 5; System.out.println("Deposit, new balance is $" + checking); } else if (menuOp == 3) { checking = checking - 5; System.out.println("Withdraw, new balance is $" + checking); } else if (menuOp == 4) { int tMenuOp = -1; while (tMenuOp != 0) { System.out.println("0 Return to main menu"); System.out.println("1 Transfer from Savings"); System.out.println("2 Transfer to Savings"); tMenuOp = promptInt("Choice?"); if (tMenuOp == 1) { savings = savings - 10; checking = checking + 10; System.out.println("Transfer, new checking balance is $" + checking); System.out.println("Transfer, new savings balance is $" + savings); } else if (tMenuOp == 2) { savings = savings + 10; checking = checking - 10; System.out.println("Transfer, new checking balance is $" + checking); System.out.println("Transfer, new savings balance is $" + savings); } } // done transfer menu } } // done main menu menuOp = -1; // resetting from last example final int MSTOP = 0; // constant value to stop choosing while (menuOp != 0) { System.out.println("0 Quit"); System.out.println("1 Sum"); System.out.println("2 Product"); menuOp = promptInt("Choice?"); if (menuOp == 1) { System.out.println("SUM calculator"); sum = 0; // reset int user = promptInt("enter value or 0 to quit"); while (user != MSTOP) { sum = sum + user; user = promptInt("enter value or 0 to quit"); } // done sum values System.out.println("sum was " + sum); } else if (menuOp == 2) { System.out.println("PRODUCT calculator"); int product = 1; int user = -1; while (user != MSTOP) { user = promptInt("enter value or 0 to quit"); if (user != MSTOP) { product = product * user; } }// done product values System.out.println("product was " + product); } } } /* Simple Utility methods that prompt the user and return input If you enter the wrong type of input, the program will end with an error! */ // utility for reading in integer input public static int promptInt(String promptText) { System.out.print(promptText + " "); Scanner scan = new Scanner(System.in); int response = scan.nextInt(); return response; } // utility for reading in double input public static double promptDouble(String promptText) { System.out.print(promptText + " "); Scanner scan = new Scanner(System.in); double response = scan.nextDouble(); return response; } // utility for reading in String input public static String promptString(String promptText) { System.out.print(promptText + " "); Scanner scan = new Scanner(System.in); String response = scan.nextLine(); return response; } // utility for reading in boolean input public static boolean promptBoolean(String promptText) { System.out.print(promptText + " (y/n) "); Scanner scan = new Scanner(System.in); String response = scan.nextLine(); return response.equalsIgnoreCase("y"); } }