Hello.
I have spend around 30 minutes to prepare many different solutions but neither compiles.
In line 17 I have tried:
if (name.isEmpty() || name == null) break;
if (name.equals("") || name.isEmpty() || name == null)
I have tried it with try-catch, without and always same issue. NullPointer or Program should display description of each cat (cat.toString).
I have also tried in line number 20: System.out.println(cat.toString());
On top of that Polish translation was very poor. I had to adjust code myself because it was written FemaleCat instead of MeanCat which cause non compiling problem
package pl.codegym.task.task14.task1404;
/*
Koty
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String name = reader.readLine();
try {
if (name.equals("")) break;
Cat cat = CatFactory.getCatByKey(name);
System.out.println(cat);
}
catch (Exception e) {
break;
}
}
}
static class CatFactory {
static Cat getCatByKey(String key) {
Cat cat = null;
if ("dziki".equals(key)) {
cat = new MeanCat("Pazur");
} else if ("mańka".equals(key)) {
cat = new MeanCat("Mania");
} else if ("kafel".equals(key)) {
cat = new NiceCat("Kafelek");
} else {
cat = new Cat(key);
}
return cat;
}
}
static class Cat {
private String name;
protected Cat(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String toString() {
return "Jestem " + getName() + ", podwórkowy kocur";
}
}
static class MeanCat extends Cat {
MeanCat(String name) {
super(name);
}
public String toString() {
return "Jestem " + getName() + ", zaraz wydrapię Ci oczy";
}
}
static class NiceCat extends Cat {
NiceCat(String name) {
super(name);
}
public String toString() {
return "Jestem miłą kotką o imieniu " + getName();
}
}
}