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 = "";
while(true) {
string = bufferedReader.readLine();
if (string.equals(""))
break;
Cat cat = CatFactory.getCatByKey(string);
System.out.println(cat.toString());
}
}
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() + " the alley cat";
}
}
static class MaleCat extends Cat {
MaleCat(String name) {
super(name);
}
public String toString() {
return "I'm a solid cat named " + getName();
}
}
static class FemaleCat extends Cat {
FemaleCat(String name) {
super(name);
}
public String toString() {
return "I'm a cute kitty named " + getName();
}
}
}
What's the problem here ?
Resolved
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
22 March 2019, 16:09solution
Did you change the original output lines? When I look up the original task code I see:
but your code shows:
You will automatically fail tasks for more likely than not for making changes to the starting code.
+8
Baljinder Singh
24 March 2019, 16:37
Thanks for the reply. Yes, you were right, the output lines were changed.
+1
// Java Poser
12 April 2019, 01:21
Man Guadalupe, good eye!
+4
Ramilya
22 March 2019, 16:02
Also you can use ArrayList, for example.
Because without this kind of solution String "" rewrites string with word (boss/miss etc.).
+1
Abhishek Rangari
7 November 2018, 18:55
just use cat not the cat.toString() in print statement. As Class is already having toString() we dont need to call it explicitely
0
Azhar Iqbal
29 September 2018, 15:59
I am Also facing the same problem a little hint is appreciated.
0