I have no idea how to pass this task.
Right now it's work, i have right output. But if i try to
@Override
public Book getBook() {
return agathaChristieBook;
}
in AgathaChrisiteBook, or similar code in MarkTwainBook i still get NullPointerException....package pl.codegym.task.task15.task1504;
import java.util.LinkedList;
import java.util.List;
/*
OOP: książki
*/
public class Solution {
public static void main(String[] args) {
List<Book> books = new LinkedList<Book>();
books.add(new MarkTwainBook("Tom Sawyer"));
books.add(new AgathaChristieBook("Hercule Poirot"));
System.out.println(books);
}
abstract static class Book {
private String author;
private String title;
public Book(String author) {
this.author = author;
}
public abstract Book getBook();
public abstract String getTitle();
private String getOutputByBookType() {
String agathaChristieOutput = author + ": " + getBook().getTitle() + " jest detektywem";
String markTwainOutput = getBook().getTitle() + " została napisana przez " + author;
if(author.equals("Mark Twain"))
return markTwainOutput;
else
return agathaChristieOutput;
}
public String toString() {
return getOutputByBookType();
}
}
public static class MarkTwainBook extends Book{
private String author = "Mark Twain";
private String title;
MarkTwainBook markTwainBook;
public MarkTwainBook(String title) {
super("Mark Twain");
this.title = title;
}
@Override
public Book getBook() {
return this;
}
@Override
public String getTitle() {
return title;
}
}
public static class AgathaChristieBook extends Book{
private String author = "Agatha Christie";
private String title;
AgathaChristieBook agathaChristieBook;
public AgathaChristieBook(String title) {
super("Agatha Christie");
this.title = title;
}
@Override
public Book getBook() {
return this;
}
@Override
public String getTitle() {
return title;
}
}
}