001package headfirst.templatemethod.barista;
002
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.InputStreamReader;
006
007public class CoffeeWithHook extends CaffeineBeverageWithHook {
008
009        public void brew() {
010                System.out.println("Dripping Coffee through filter");
011        }
012
013        public void addCondiments() {
014                System.out.println("Adding Sugar and Milk");
015        }
016
017        public boolean customerWantsCondiments() {
018
019                String answer = getUserInput();
020
021                if (answer.toLowerCase().startsWith("y")) {
022                        return true;
023                } else {
024                        return false;
025                }
026        }
027
028        private String getUserInput() {
029                String answer = null;
030
031                System.out.print("Would you like milk and sugar with your coffee (y/n)? ");
032
033                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
034                try {
035                        answer = in.readLine();
036                } catch (IOException ioe) {
037                        System.err.println("IO error trying to read your answer");
038                }
039                if (answer == null) {
040                        return "no";
041                }
042                return answer;
043        }
044}