Hi Guys, I cannot work it out, I already tried creating new instances of classes, casting them on person, creating new instances, and using them for method innitalization.
package pl.codegym.task.task14.task1411;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
User, loser, coder i programmer
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Person person = null;
String key = null;
while (true)
{
key = reader.readLine();
if (!(key.equals("user") || key.equals("loser") || key.equals("coder") || key.equals("programmer"))) {
break;
}
if ("user".equals(key)) {
User user = new User();
person = user;
}
else if ("loser".equals(key)) {
Loser loser = new Loser();
person = loser;
}
else if ("coder".equals(key)) {
Coder coder = new Coder();
person = coder;
}
else if ("programmer".equals(key)) {
Programmer programmer = new Programmer();
person = programmer;
}
doWork(person); // Wywołuje doWork
}
}
public static void doWork(Person person) {
if (person instanceof User) {
person.live();
}
else if (person instanceof Loser) {
person.doNothing();
}
else if (person instanceof Coder) {
person.writeCode();
}
else if (person instanceof Programmer) {
person.enjoy();
}
}
}