First of all, I have already passed the task so I can't really post my whole code. So I'm just posting original code for your reference.
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Person person = null;
String key;
while (!(key = reader.readLine()).equals("exit")) {
if ("player".equals(key)) {
person = new Player();
} else if ("dancer".equals(key)) {
person = new Dancer();
}
haveFun(person);
}
}
public static void haveFun(Person person) {
//write your code here
}
interface Person {
}
static class Player implements Person {
void play() {
System.out.println("playing");
}
}
static class Dancer implements Person {
void dance() {
System.out.println("dancing");
}
}
}
The strange thing is that if I type other than player or dancer - like "hello", it always displays dancing. Could anyone explain why it's happening and how to prevent it?