another version of this code that doesn't work to :
public static void haveFun(Person person) {
if(person instanceof Player){
person.play();
}
else if(person instanceof Dancer){
person.dance();
}
here I have another version of method haveFun:
public static void haveFun(Person person) {
try {
if(person instanceof Player){
person.play();
}
}catch (Exception e){
person.dance();
}
}
package pl.codegym.task.task14.task1407;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Graj i tańcz
*/
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("wyjdz")) {
if ("player".equals(key)) {
person = new Player();
} else if ("dancer".equals(key)) {
person = new Dancer();
}
haveFun(person);
}
}
public static void haveFun(Person person) {
if(person instanceof Player){
Player a = new Player();
a.play();
}
else if(person instanceof Dancer){
Dancer a = new Dancer();
a.dance();
}
}
interface Person {
}
static class Player implements Person {
void play() {
System.out.println("gramy");
}
}
static class Dancer implements Person {
void dance() {
System.out.println("tańczymy");
}
}
}