package animal;


abstract class State {
    abstract public void process(Node node);
}

class QuestionState extends State {
    public QuestionState(String q, Node yes, Node no) {
        question = q;
        yesNode = yes;
        noNode = no;
    }

    public void process(Node node) {
        Input in = Input.getInstance();
        System.out.println(question + "  ");
        String answer = in.getLine();
        if(answer.equals("yes") || answer.equals("Yes")) {
            yesNode.process();
        } else {
            noNode.process();
        }
    }

    protected String question;
    protected Node yesNode;
    protected Node noNode;
}

class AnswerState extends State {
    public AnswerState(String answer) {
        animal = answer;
    }

    public void process(Node node) {
        Input in = Input.getInstance();
        System.out.println("Are you thinking of a(n) " + animal + "? ");
        String answer = in.getLine();
        if(answer.equals("yes") || answer.equals("Yes")) {
            System.out.print("I knew it!\n");
        } else {
            String msg = "";
            msg += "Gee, you stumped me, what were you thinking of? ";
            System.out.print(msg);
            String newAnimal = in.getLine();
            msg = "What is a question to distinguish a " + newAnimal;
            msg += " from a " + animal + "? ";
            System.out.print(msg);
            String newQuestion = in.getLine();
            State ns = new QuestionState(newQuestion,
                                         new Node(new AnswerState(newAnimal)),
                                         new Node(node.getState()));
            node.setState(ns);
        }
    }

    protected String animal;
}
