Hey,
When I use:
System.out.println(CatFactory.getCatByKey(reader.readLine()));
Without the loop, I have good output for each String key because it's fit to switch.
But the situation is changing, when I try use do-while, or while loop to match the requirements.
While loop should break my loop automatically when I press "" empty enter, right?
But my output is like this:
I am typing "kitty" and confirm with Enter. It should display promptly, one of the switch. But the program want's to hit again!, Enter. And then it is display the result of switch.
But worst i that, if I type correct switch key, it is display default Cat.
do-while loop is looks like this.
I am typing "kafel" and it is true, match to key switch, my output is promptly. And after, the program wants new input. This is expected of me. Now I try to put next key switch, so I am typing "mańka" and program ends!
Difference is in the do-while will accept 4th goal of requirements.
package pl.codegym.task.task14.task1404;
/*
Koty
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws Exception {
//tutaj wpisz swój kod
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//test
//System.out.println(CatFactory.getCatByKey(reader.readLine()));
// loop do-while
/*
do {
System.out.println(CatFactory.getCatByKey(reader.readLine()));
} while (reader.readLine().equals(""));
*/
// loop while
while (reader.readLine()!="") {
System.out.println(CatFactory.getCatByKey(reader.readLine()));
}
}
static class CatFactory {
static Cat getCatByKey(String key) {
Cat cat;
switch (key) {
case "dziki":
cat = new MeanCat("Pazur");
break;
case "mańka":
cat = new NiceCat("Mania");
break;
case "kafel":
cat = new NiceCat("Kafelek");
break;
default:
cat = new Cat(key);
break;
}
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();
}
}
}