From
6. For each string entered (including the invalid string), you must call the MovieFactory.getMovie method.
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));
/*
8. Create a variable movie in the Movie class, and for each entered string (key):
8.1. Get an object using MovieFactory.getMovie and assign it to the variable movie.
8.2. Display the result of calling movie.getClass().getSimpleName().
*/
//String textInput = null;
//Movie movie;
//label:
while(true){
String textInput = reader.readLine();
Movie movie = MovieFactory.getMovie(textInput);
if(textInput.equalsIgnoreCase("cartoon")){
//Movie movie = MovieFactory.getMovie(textInput);
System.out.println(movie.getClass().getSimpleName());
}else if(textInput.equalsIgnoreCase("thriller")) {
//Movie movie = MovieFactory.getMovie(textInput);
System.out.println(movie.getClass().getSimpleName());
}else if(textInput.equalsIgnoreCase("soapOpera")){
//Movie movie = MovieFactory.getMovie(textInput);
System.out.println(movie.getClass().getSimpleName());
}else{
break;
}
}
}
static class MovieFactory {
static Movie getMovie(String key) {
Movie movie = null;
// Create a SoapOpera object for the key "soapOpera"
if ("soapOpera".equalsIgnoreCase(key)) {
movie = new SoapOpera();
}
//write your code here. Items 5, 6
else if("Cartoon".equalsIgnoreCase(key)){
movie = new Cartoon();
}else if("Thriller".equalsIgnoreCase(key)){
movie = new Thriller();
}
return movie;
}
}
static abstract class Movie {
}
static class SoapOpera extends Movie {
}
// Write your classes here. Item 3
static class Cartoon extends Movie{
}
static class Thriller extends Movie{
}
}
The first try, I wrote "Movie movie = MovieFactory.getMovie(textInput);" at line 18,22,26 but It not pass requirement 6. I think I call "MovieFactory.getMovie()" but not pass. Also, the second try I move "Movie movie = MovieFactory.getMovie(textInput);" form line 18,22,26 to line 16 , the result is pass.
I don't understand why I passed at the second try and why I didn't pass at the first try?