Source of GreesieMethods.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 GreesieMethods {

 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:         Utilities.printTitle("Welcome to GreesieBurger!");
 21:         // System.out.print("\n\n"
 22:                 // + "Welcome to GreesieBurger!\n"
 23:                 // + "-------------------------\n\n");

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

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

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

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

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

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

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

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

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

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

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

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

122: }