Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         Library library = new Library();
  5.  
  6.         library.addBook("Java For Begginers", "2017", "Jakub Niedzielski");
  7.         library.addBook("Java Podstawy Wydanie X", "2016", "Cay S. Horstmann");
  8.         library.addBook("Java. Przewodnik dla początkujących. Wydanie V", "2012", "Herbert Schildt");
  9.         library.addBook("Capitalism: The unknown ideal", "1966", "Ayn Rand");
  10.         library.addBook("Atlas shrugged", "1957", "Ayn Rand");
  11.         library.addBook("Java For Dumbasses", "2017", "Jakub Niedzielski");
  12.         library.addBook("Effective Java: Programming Language Guide", "2017", "Joshua Bloch");
  13.         library.addBook("Java Performance", "2016", "Charlie Hunt");
  14.  
  15.         library.lendBook(3);
  16.         library.removeBook(3);
  17.  
  18.  
  19.     }
  20.  
  21. }
  22.  
  23. import java.util.HashMap;
  24. import java.util.Map;
  25.  
  26. public class Library {
  27.  
  28.     private int uniqueID = 0;
  29.     private Map<Integer, Book> bookMap = new HashMap<>();
  30.  
  31.     public void addBook(String title, String year, String author) {
  32.         uniqueID++;
  33.         Book newBook = new Book(title, year, author, uniqueID);
  34.         bookMap.put(uniqueID, newBook);
  35.  
  36.     }
  37.  
  38.     public void getInfoByID(Integer bookID) {
  39.         Book book = bookMap.get(bookID);
  40.         if (book != null) {
  41.             book.print();
  42.         }
  43.     }
  44.  
  45.     public void getStatusInfo(Integer bookID) {
  46.         Book statusInfo = bookMap.get(bookID);
  47.         System.out.println("The book of ID: " + bookID + " is " + statusInfo.getBookStatus());
  48.     }
  49.  
  50.     public void lendBook(Integer bookID) {
  51.         Book bookStatus = bookMap.get(bookID);
  52.         bookStatus.lendBook();
  53.     }
  54.  
  55.     public void recoverBook(Integer bookID) {
  56.         Book bookStatus = bookMap.get(bookID);
  57.         bookStatus.recoverBook();
  58.     }
  59.  
  60.     public void removeBook(Integer bookID) {
  61.         Book value = bookMap.get(bookID);
  62.         if (value != null) {
  63.             if (value.getEnumBookStatus().equals(BookStatus.AVAILABLE)) {
  64.                 bookMap.remove(bookID);
  65.             } else {
  66.                 System.out.println("Can't remove that book right now");
  67.             }
  68.         } else {
  69.             System.out.println("Invalid ID number");
  70.  
  71.         }
  72.  
  73.     }
  74.  
  75.     public void getAll() {
  76.         for (Book book : bookMap.values()) {
  77.             book.print();
  78.         }
  79.     }
  80.  
  81.     public void getAllByAuthor(String author) {
  82.         for (Book book : bookMap.values()) {
  83.             if (author.equals(book.getAuthor())) {
  84.                 book.print();
  85.             }
  86.         }
  87.     }
  88.  
  89.     public void getAllByYear(String year) {
  90.         for (Book book : bookMap.values()) {
  91.             if (year.equals(book.getYear())) {
  92.                 book.print();
  93.             }
  94.         }
  95.     }
  96.  
  97.     // forbid to lent a book if its already lent
  98.     // should allow to pass the name of person who lent it
  99.  
  100.     public Library() {
  101.  
  102.     }
  103.  
  104. }
  105.  
  106.  
  107.  
  108. public class Book {
  109.  
  110.     private int id;
  111.     private String title;
  112.     private String year;
  113.     private String author;
  114.     private BookStatus bookStatus = BookStatus.AVAILABLE;
  115.  
  116.     public Book(String title, String year, String author, int uniqueID) {
  117.         this.id = uniqueID;
  118.         this.title = title;
  119.         this.year = year;
  120.         this.author = author;
  121.  
  122.     }
  123.  
  124.     public String getTitle() {
  125.         return title;
  126.     }
  127.  
  128.     public String getYear() {
  129.         return year;
  130.     }
  131.  
  132.     public String getAuthor() {
  133.         return author;
  134.     }
  135.  
  136.     public int getId() {
  137.         return id;
  138.     }
  139.  
  140.     public void lendBook() {
  141.         bookStatus = BookStatus.NON_AVAILABLE;
  142.     }
  143.  
  144.     public void recoverBook() {
  145.         bookStatus = BookStatus.AVAILABLE;
  146.     }
  147.  
  148.     public String getBookStatus() {
  149.         return bookStatus.toString();
  150.     }
  151.    
  152.     public BookStatus getEnumBookStatus() {
  153.         return bookStatus;
  154.     }
  155.  
  156.     public void print() {
  157.         System.out.println("ID number: " + this.getId());
  158.         System.out.println("Title: " + this.getTitle());
  159.         System.out.println("Year: " + this.getYear());
  160.         System.out.println("Author: " + this.getAuthor());
  161.         System.out.println("The book is " + this.getBookStatus());
  162.         System.out.println();
  163.     }
  164.  
  165. }
  166.  
  167.  
  168. enum BookStatus {
  169.     AVAILABLE {
  170.         @Override
  171.         public String toString() {
  172.             return "available";
  173.         }
  174.     },
  175.     NON_AVAILABLE {
  176.         @Override
  177.         public String toString() {
  178.             return "non available";
  179.         }
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement