Advertisement
16120

Library

May 8th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1.  
  2. public class main {
  3.  
  4.     public static void main(String[] args) {
  5.         Lib lib = new Lib();
  6.  
  7.  
  8.         lib.newBook("Into the Wild","Jon Krakauer","Villard","1996", "9789544745349");
  9.         lib.newBook("My Biography","Roberto Vasilev","My Publishment","2019", "0-1111-1111-1");
  10.         lib.newBook("Harry Potter and the Philosopher Stone","J.K. Rowling","Bloomsbury","1997", "0-7475-3269-9");
  11.         lib.newBook("Darkmouth","Shane Hegarty","Ciela","2015", "978-954-28-1898-4");
  12.  
  13.  
  14.         lib.getAllBook();
  15.         System.out.println();
  16.         lib.getBookByAuthor("J.K. Rowling");
  17.         lib.deleteBookByAuthor("J.K. Rowling");
  18.         System.out.println();
  19.         lib.getAllBook();
  20.  
  21.  
  22.     }
  23.  
  24. }
  25. ------------
  26. import java.util.*;
  27.  
  28. public class Lib {
  29.     Map<authors, book> libraryBooks = new HashMap<>();
  30.  
  31.     public void newBook(String header, String author, String publisher, String dateOfPublishment, String ISBN) {
  32.         libraryBooks.put(new authors(author), new book(header, author, publisher, dateOfPublishment, ISBN));
  33.     }
  34.  
  35.     public void getBookByAuthor(String author) {
  36.         for (authors authors : libraryBooks.keySet()) {
  37.             if (authors.getAuthor().equals(author)) {
  38.                 System.out.println(libraryBooks.get(authors));
  39.             }
  40.         }
  41.     }
  42.  
  43.     public void deleteBookByAuthor(String author) {
  44.  
  45.         for(Iterator<Map.Entry<authors, book>> it = libraryBooks.entrySet().iterator(); it.hasNext(); ) {
  46.             Map.Entry<authors, book> entry = it.next();
  47.             if(entry.getKey().getAuthor().equals(author)) {
  48.                 it.remove();
  49.             }
  50.         }
  51.  
  52.  
  53.     }
  54.  
  55.  
  56.  
  57.     public void deleteBookByName(String name) {
  58.         for(Iterator<Map.Entry<authors, book>> it = libraryBooks.entrySet().iterator(); it.hasNext(); ) {
  59.             Map.Entry<authors, book> entry = it.next();
  60.             if(entry.getValue().getHeader().equals(name)) {
  61.                 it.remove();
  62.             }
  63.         }
  64.     }
  65.  
  66.     public void getAllBook() {
  67.         for (authors authors : libraryBooks.keySet()) {
  68.             System.out.println(libraryBooks.get(authors));
  69.  
  70.         }
  71.  
  72.     }
  73.  
  74.     public void getBookByName(String name) {
  75.         for (book book : libraryBooks.values()) {
  76.             if (book.getHeader().equals(name)) {
  77.                 for (authors authors : libraryBooks.keySet()) {
  78.                     if (authors.getAuthor().equals(book.getAuthor())) {
  79.                         System.out.println(libraryBooks.get(authors));
  80.                     }
  81.                 }
  82.  
  83.             }
  84.         }
  85.     }
  86.  
  87.  
  88.  
  89. }
  90. ------------
  91.  
  92. public class book extends Lib {
  93.       protected String header;
  94.         protected String author;
  95.         protected String publisher;
  96.         protected String dateOfPublishment;
  97.         protected String ISBN;
  98.  
  99.         public book(String header, String author, String publisher, String dateOfPublishment, String ISBN) {
  100.             this.header = header;
  101.             this.author = author;
  102.             this.publisher = publisher;
  103.             this.dateOfPublishment = dateOfPublishment;
  104.             this.ISBN = ISBN;
  105.         }
  106.  
  107.         public String getHeader() {
  108.             return header;
  109.         }
  110.  
  111.         public String getAuthor() {
  112.             return author;
  113.         }
  114.  
  115.         public String getPublisher() {
  116.             return publisher;
  117.         }
  118.  
  119.         public String getDateOfPublishment() {
  120.             return dateOfPublishment;
  121.         }
  122.  
  123.         public String getISBN() {
  124.             return ISBN;
  125.         }
  126.         @Override
  127.         public String toString() {
  128.             return String.format("Book: '%s' Author: '%s' Publisher: '%s' Published: '%s' Date of publishment: '%s'",this.getHeader(),this.getAuthor(),this.getPublisher(),this.getDateOfPublishment(),this.getISBN());
  129.         }
  130.     }
  131. -------------
  132.  
  133. public class authors {
  134.       private String author;
  135.  
  136.         public String getAuthor() {
  137.             return author;
  138.         }
  139.  
  140.         public authors(String author) {
  141.             this.author = author;
  142.         }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement