Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level15.lesson02.task04;
- /* ООП - книги
- 1. Создайте public static класс MarkTwainBook, который наследуется от Book. Имя автора [Mark Twain]. Параметр конструктора - имя книги.
- 2. В классе MarkTwainBook реализуйте все абстрактные методы.
- 3. Для метода getBook расширьте тип возвращаемого результата.
- 4. Создайте по аналогии AgathaChristieBook. Имя автора [Agatha Christie].
- 5. В классе Book реализуйте тело метода getOutputByBookType так, чтобы он возвращал:
- 5.1. agathaChristieOutput для книг Агаты Кристи;
- 5.2. markTwainOutput для книг Марка Твена.
- */
- import java.util.LinkedList;
- import java.util.List;
- 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 abstract static class Book {
- private String author;
- public Book(String author) {
- this.author = author;
- }
- public abstract Book getBook();
- public abstract String getName();
- private String getOutputByBookType() {
- String agathaChristieOutput = author + ", " + getBook().getName() + " is a detective";
- String markTwainOutput = getBook().getName() + " book was written by " + author;
- String output = "";
- //Add your code here
- if (this instanceof MarkTwainBook)
- output = markTwainOutput;
- if (this instanceof AgathaChristieBook)
- output = agathaChristieOutput;
- return output;
- }
- public String toString() {
- return getOutputByBookType();
- }
- }
- /*Book class ends here*/
- /*public static класс MarkTwainBook*/
- public static class MarkTwainBook extends Book {
- String name = "";
- public MarkTwainBook(String name) {
- super(name);
- this.name = name;
- super.author = "Mark Twain";
- }
- public String getName() {
- return this.name;
- }
- public MarkTwainBook getBook() {
- return this;
- }
- }
- /*----------------------------------------------------*/
- /*public static класс AgathaChristieBook*/
- public static class AgathaChristieBook extends Book {
- String name = "";
- public AgathaChristieBook(String name) {
- super(name);
- this.name = name;
- super.author = "Agatha Christie";
- }
- public String getName() {
- return this.name;
- }
- public AgathaChristieBook getBook() {
- return this;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement