Created
March 19, 2017 12:53
-
-
Save fermopili/91282ceb8cd046bcb8dcad7319f85119 to your computer and use it in GitHub Desktop.
com.javarush.task.task15.task1504
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ООП - книги | |
*/ | |
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); | |
} | |
public static class MarkTwainBook extends Book | |
{ | |
private String title; | |
// private String name; | |
public MarkTwainBook(String title) | |
{ | |
super("Mark Twain"); | |
this.title = title; | |
} | |
@Override | |
public MarkTwainBook getBook() | |
{ | |
return this; | |
} | |
@Override | |
public String getTitle() | |
{ | |
return title; | |
} | |
} | |
public static class AgathaChristieBook extends Book | |
{ | |
private String title; | |
public AgathaChristieBook(String title) | |
{ | |
super("Agatha Christie"); | |
this.title = title; | |
} | |
@Override | |
public AgathaChristieBook getBook() | |
{ | |
return this; | |
} | |
@Override | |
public String getTitle() | |
{ | |
return title; | |
} | |
} | |
abstract static class Book | |
{ | |
private String author; | |
public Book(String author) | |
{ | |
this.author = author; | |
} | |
public abstract Book getBook(); | |
public abstract String getTitle(); | |
private String getOutputByBookType() | |
{ | |
String agathaChristieOutput = author + ", " + getBook().getTitle() + " is a detective"; | |
String markTwainOutput = getBook().getTitle() + " book was written by " + author; | |
String output = "output"; | |
//Add your code here | |
if (this instanceof AgathaChristieBook) | |
return agathaChristieOutput; | |
if (this instanceof MarkTwainBook) | |
return markTwainOutput; | |
return output; | |
} | |
public String toString() | |
{ | |
return getOutputByBookType(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment