OUTPUT:
Cartoon
Thriller
SoapOpera
java.lang.NullPointerException
at com.codegym.task.task14.task1414.Solution$MovieFactory.getMovie(Solution.java:45)
at com.codegym.task.task14.task1414.Solution.main(Solution.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
INPUT
cartoon
thriller
soapOpera
movie
package com.codegym.task.task14.task1414;
/*
MovieFactory
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) throws Exception {
// Read several keys (strings) from the console. Item 7
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String key = null;
String[] keys = {"cartoon", "thriller", "soapOpera"};
Movie movie = null;
while(true) {
key = reader.readLine();
movie = MovieFactory.getMovie(key);
if(!Arrays.stream(keys).anyMatch(key::equals)) {
break;
}
System.out.println(movie.getClass().getSimpleName());
}
}
static class MovieFactory {
static Movie getMovie(String key) {
Movie movie = null;
// Create a SoapOpera object for the key "soapOpera"
if ("soapOpera".equals(key)) {
movie = new SoapOpera();
} else if("thriller".equals(key)) {
movie = new Thriller();
} else if("cartoon".equals(key)) {
movie = new Cartoon();
}
//write your code here. Items 5, 6
movie = movie.getMovie();
return movie;
}
}
static abstract class Movie {
abstract Movie getMovie();
}
static class SoapOpera extends Movie {
public Movie getMovie() {
return new SoapOpera();
}
}
static class Cartoon extends Movie {
public Movie getMovie() {
return new Cartoon();
}
}
static class Thriller extends Movie {
public Movie getMovie() {
return new Thriller();
}
}
}