Source of BooleanMethods.java


  2: import java.util.Scanner;

  4: /**
  5:  * A simple order-taking app to demonstrate the "if" control and boolean 
  6:  * methods. With more methods than the GreesieBurger program.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */

 11: public class BooleanMethods {

 13:     public static void main(String[] args) {
 14:         // create variables
 15:         Scanner kbd = new Scanner(System.in);
 16:         String answer;
 17:         double amount = 0.00;

 19:         // welcome customer
 20:         System.out.print("\n\n"
 21:                 + "Welcome to GreesieBurger!\n"
 22:                 + "-------------------------\n\n");

 24:         // ask for sandwich order
 25:         System.out.print("What sandwich would you like? ");
 26:         addToOrder(kbd.nextLine());
 27:         amount += 8.95;

 29:         // ask if want fries
 30:         if (userConfirms("Would you like fries with that? ")) {
 31:             addToOrder("Fries");
 32:             amount += 2.99;
 33:         }

 35:         // ask for drink order
 36:         System.out.print("What can I get you to drink? ");
 37:         answer = kbd.nextLine();

 39:         // check if we carry that
 40:         while (!answer.toUpperCase().equals("PEPSI")) {
 41:             System.out.println("No " + answer + ".  Pepsi.");
 42:             System.out.print("What can I get you to drink? ");
 43:             answer = kbd.nextLine();
 44:         }
 45:         addToOrder(answer);
 46:         amount += 2.99;

 48:         // report back order and total amount owed
 49:         System.out.print("\n"
 50:                 + "That'll be $" + amount + ".\n\n");
 51:         System.out.println("Thank-you for eating at GreesieBurgers!\n");
 52:     }

 54:     /**
 55:      * Add an item to the order by "shouting" to the back, and waiting for the
 56:      * person on grill to confirm by repeating the item.
 57:      *
 58:      * @param item the item to add to the order
 59:      */
 60:     private static void addToOrder(String item) {
 61:         System.out.println(item.toUpperCase() + "!");
 62:         System.out.println("(" + item.toLowerCase() + ")");
 63:     }

 65:     /**
 66:      * Ask the user a yes-no question and return whether they say yes.
 67:      * Make sure that their answer is either a yes or a no before returning.
 68:      *
 69:      * @param prompt the question to ask
 70:      * @return whether the (final) answer counts as a "yes"
 71:      */
 72:     private static boolean userConfirms(String prompt) {
 73:         // create variables
 74:         Scanner kbd = new Scanner(System.in);
 75:         String answer;

 77:         // get answer
 78:         System.out.print(prompt + " ");
 79:         answer = kbd.next();
 80:         kbd.nextLine();

 82:         // make sure it's a yes or a no
 83:         while (!isYes(answer) && !isNo(answer)) {
 84:             System.out.println("Please answer YES or NO.");
 85:             System.out.print(prompt + " ");
 86:             answer = kbd.next();
 87:             kbd.nextLine();
 88:         }

 90:         // return whether it's a yes
 91:         return isYes(answer);
 92:     }

 94:     /**
 95:      * Check a word to see if it counts as a "yes".
 96:      *
 97:      * @param word the word to check
 98:      * @return whether the word counts as a "yes"
 99:      */
100:     private static boolean isYes(String word) {
101:         return word.equalsIgnoreCase("yes")
102:             || word.equalsIgnoreCase("yeah")
103:             || word.equalsIgnoreCase("yup")
104:             || word.equalsIgnoreCase("sure")
105:             || word.equalsIgnoreCase("ok")
106:             || word.equalsIgnoreCase("fine");
107:     }

109:     /**
110:      * Check a word to see if it counts as a "no".
111:      *
112:      * @param word the word to check
113:      * @return whether the word counts as a "no"
114:      */
115:     private static boolean isNo(String word) {
116:         return word.equalsIgnoreCase("no")
117:             || word.equalsIgnoreCase("nope")
118:             || word.equalsIgnoreCase("nah");
119:     }

121: }