OutPut:
java.lang.NullPointerException
at com.codegym.task.task14.task1404.Solution.main(Solution.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAcce
It run very well on my JDK..but it creates an exception here...why lol
package com.codegym.task.task14.task1404;
/*
Cats
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Cat> list = new ArrayList<Cat>();
while (true){
String s = read.readLine();
if (s.isEmpty() || s == null) break;
s = s.toLowerCase();
Cat yo = CatFactory.getCatByKey(s);
list.add(yo);
}
for (Cat cat: list) System.out.println(cat);
}
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();
}
}
}