// Whitaker Brand // CSE142 // Section BB // TA: Althea Poteet // 1/24/2018 // // This program is an example of processing user input using a Scanner, // and cumulative algorithms. // // (implementation details for class:) // This program is a good example of method decomposition, using 3 methods // to add structure and reduce redundancy. // // It still has some weaknesses: // - redundant printlns in main regarding total cost // - class constant for number of friends is suboptimal: // -- better would be to prompt for the number of friends, // and have the dinner cost message be based on an average // cost per person import java.util.*; public class Restaurant { public static final int FRIEND_COUNT = 3; public static void main(String[] args) { // NOTE: when using a System.in Scanner, it is better to declare only one // of these in main, and pass it as a parameter (instead of declaring // a new one in each method where it is needed) Scanner console = new Scanner(System.in); double aTotal = dinnerCost(console); System.out.println("Oh, it looks like group A spent $" + aTotal + " on dinner."); double bTotal = dinnerCost(console); System.out.println("Oh, it looks like group B spent $" + bTotal + " on dinner."); System.out.println("A's dinner was " + priceRange(aTotal)); System.out.println("B's dinner was " + priceRange(bTotal)); } // prints out an explanation of what the program does public static void intro() { System.out.println("This program prompts the user for input"); System.out.println("regarding the total money spent by two"); System.out.println("different groups of friends eating at a"); System.out.println("restaurant"); } // given a dollar amount representing a dinner for FRIEND_COUNT people, // returns a String representing whether that dinner was inexpensive, // moderately priced, or expensive. public static String priceRange(double totalCost) { if (totalCost < 20.0) { return "inexpensive"; } else if (totalCost < 60.0) { return "moderately priced"; } else { return "expensive"; } } // Accepts a user input Scanner as a parameter, prompts the user // for how much each friend spent on dinner, and returns the total // cost of the dinners public static double dinnerCost(Scanner console) { double total = 0.0; for (int i = 1; i <= FRIEND_COUNT; i++) { System.out.print("Friend #" + i + ", how much did your dinner cost? "); total = total + console.nextDouble(); // these lines are equivalent to the above line, except // that they store an extra variable, so they are less // preferable. // double thisFriendCost = console.nextDouble(); // total += thisFriendCost; } return total; } }