It does print:
I'm a cat powerhouse named Big Boy
I'm a cute kitty named Missy
I'm a cute kitty named Smudgey
I'm Pirate, an alley cat
Which I presume is the correct output...… ._.
package com.codegym.task.task14.task1404;
/*
Cats
1. Read strings (parameters) from the console until the user enters an empty string (Enter).
2. Each parameter corresponds to the name of a cat.
For each parameter:
3. Create a Cat object equal to the cat from getCatByKey(String parameter).
4. Display the result of cat.toString().
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String string = "";
try{
while(true) {
string = bufferedReader.readLine();
if (string.equals(""))
break;
Cat cat = CatFactory.getCatByKey(string);
System.out.println(cat);
}
}
catch(NullPointerException e){
}
}
static class CatFactory {
static Cat getCatByKey(String key) {
Cat cat = null;
if ("Boss".equals(key)) {
cat = new MaleCat("Big Boy");
} else if ("Miss".equals(key)) {
cat = new FemaleCat("Missy");
} else if ("Smudge".equals(key)) {
cat = new FemaleCat("Smudgey");
} 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 "I'm " + getName() + ", an alley cat";
}
}
static class MaleCat extends Cat {
MaleCat(String name) {
super(name);
}
public String toString() {
return "I'm a cat powerhouse named " + getName();
}
}
static class FemaleCat extends Cat {
FemaleCat(String name) {
super(name);
}
public String toString() {
return "I'm a cute kitty named " + getName();
}
}
}