Advertisement
jaVer404

level15.lesson02.task04

Jul 2nd, 2015
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. package com.javarush.test.level15.lesson02.task04;
  2.  
  3. /* ООП - книги
  4. 1. Создайте public static класс MarkTwainBook, который наследуется от Book. Имя автора [Mark Twain]. Параметр конструктора - имя книги.
  5. 2. В классе MarkTwainBook реализуйте все абстрактные методы.
  6. 3. Для метода getBook расширьте тип возвращаемого результата.
  7. 4. Создайте по аналогии AgathaChristieBook. Имя автора [Agatha Christie].
  8. 5. В классе Book реализуйте тело метода getOutputByBookType так, чтобы он возвращал:
  9. 5.1. agathaChristieOutput для книг Агаты Кристи;
  10. 5.2. markTwainOutput для книг Марка Твена.
  11. */
  12.  
  13. import java.util.LinkedList;
  14. import java.util.List;
  15.  
  16. public class Solution {
  17.     public static void main(String[] args) {
  18.         List<Book> books = new LinkedList<Book>();
  19.         books.add(new MarkTwainBook("Tom Sawyer"));
  20.         books.add(new AgathaChristieBook("Hercule Poirot"));
  21.         System.out.println(books);
  22.     }
  23.  
  24.     public abstract static class Book {
  25.         private String author;
  26.  
  27.         public Book(String author) {
  28.             this.author = author;
  29.         }
  30.  
  31.         public abstract Book getBook();
  32.  
  33.         public abstract String getName();
  34.  
  35.         private String getOutputByBookType() {
  36.             String agathaChristieOutput = author + ", " + getBook().getName() + " is a detective";
  37.             String markTwainOutput = getBook().getName() + " book was written by " + author;
  38.  
  39.             String output = "";
  40.             //Add your code here
  41.             if (this instanceof MarkTwainBook)
  42.                 output = markTwainOutput;
  43.             if (this instanceof AgathaChristieBook)
  44.                 output = agathaChristieOutput;
  45.  
  46.             return output;
  47.         }
  48.  
  49.         public String toString() {
  50.             return getOutputByBookType();
  51.         }
  52.     }
  53.     /*Book class ends here*/
  54.  
  55.     /*public static класс MarkTwainBook*/
  56.     public static class MarkTwainBook extends Book {
  57.         String name = "";
  58.         public MarkTwainBook(String name) {
  59.             super(name);
  60.             this.name = name;
  61.             super.author = "Mark Twain";
  62.         }
  63.  
  64.  
  65.         public String getName() {
  66.             return this.name;
  67.         }
  68.  
  69.         public MarkTwainBook getBook() {
  70.             return this;
  71.         }
  72.     }
  73.     /*----------------------------------------------------*/
  74.  
  75.     /*public static класс AgathaChristieBook*/
  76.     public static class AgathaChristieBook extends Book {
  77.         String name = "";
  78.         public AgathaChristieBook(String name) {
  79.             super(name);
  80.             this.name = name;
  81.             super.author = "Agatha Christie";
  82.         }
  83.  
  84.  
  85.         public String getName() {
  86.             return this.name;
  87.         }
  88.  
  89.         public AgathaChristieBook getBook() {
  90.             return this;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement