Advertisement
Kimes

Untitled

Mar 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package pl.kimes.core;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. public class Library {
  8.  
  9.     private int uniqueID;
  10.     private Map<Integer, Book> bookMap = new HashMap<>();
  11.     private Map<Book, Integer> copiesMap = new HashMap<>();
  12.     private Map<Book, List<String>> borrowersMap = new HashMap<>();
  13.    
  14.     public void addBook(Book newAddbook) {
  15.         Integer id = ++uniqueID;
  16.         for (Book book : bookMap.values()) {
  17.             if (book.equals(newAddbook)) {
  18.                 int numberOfCopies = getNumberOfCopies(newAddbook);
  19.                 newAddbook.setId(id);
  20.                 numberOfCopies++;
  21.                 bookMap.put(id, newAddbook);
  22.                 copiesMap.put(newAddbook, numberOfCopies);
  23.                 return;
  24.             }
  25.         }
  26.         int numberOfCopies = 1;
  27.         newAddbook.setId(id);
  28.         bookMap.put(id, newAddbook);
  29.         copiesMap.put(newAddbook, numberOfCopies);
  30.     }
  31.    
  32.     public int getNumberOfCopies(Book book){
  33.         return copiesMap.get(book);
  34.        
  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.  
  46.     public void lendBook(Integer bookID, String borrowersName) {
  47.         Book bookStatus = bookMap.get(bookID);
  48.        
  49.  
  50.     }
  51.  
  52.     public void returnBook(Integer bookID, String returnersName) {
  53.         Book bookStatus = bookMap.get(bookID);
  54.        
  55.     }
  56.  
  57.     public void removeBook(Integer bookID) {
  58.         Book value = bookMap.get(bookID);
  59.         if (value != null) {
  60.             if (value.getEnumBookStatus().equals(BookStatus.AVAILABLE)) {
  61.                 bookMap.remove(bookID);
  62.             } else {
  63.                 System.out.println("Can't remove that book right now, it's lent by " + value.getBorrowersName());
  64.             }
  65.         } else {
  66.             System.out.println("Invalid ID number");
  67.  
  68.         }
  69.  
  70.     }
  71.  
  72.     public void getAllBooks() {
  73.         for (Book book : bookMap.values()) {
  74.             book.print();
  75.             System.out.println("Number of copies: " + getNumberOfCopies(book));
  76.  
  77.         }
  78.     }
  79.  
  80.     public void getAllByTitle(String title) {
  81.         for (Book book : bookMap.values()) {
  82.             if (title.equals(book.getTitle())) {
  83.                 book.print();
  84.             }
  85.         }
  86.  
  87.     }
  88.  
  89.     public void getAllByAuthor(String author) {
  90.         for (Book book : bookMap.values()) {
  91.             if (author.equals(book.getAuthor())) {
  92.                 book.print();
  93.             }
  94.         }
  95.     }
  96.  
  97.     public void getAllByYear(String year) {
  98.         for (Book book : bookMap.values()) {
  99.             if (year.equals(book.getYear())) {
  100.                 book.print();
  101.             }
  102.         }
  103.     }
  104.  
  105.     public void getAllByAuthorAndYear(String author, String year) {
  106.         for (Book book : bookMap.values()) {
  107.             if (author.equals(book.getAuthor()) && (year.equals(book.getYear()))) {
  108.                 book.print();
  109.             }
  110.         }
  111.     }
  112.  
  113.     public Library() {
  114.  
  115.     }
  116.  
  117. }
  118. package pl.kimes.core;
  119.  
  120.  
  121. public class Book {
  122.  
  123.     private int id;
  124.     private String title;
  125.     private String year;
  126.     private String author;
  127.     private BookStatus bookStatus = BookStatus.AVAILABLE;
  128.  
  129.     public Book(String title, String year, String author) {
  130.         this.title = title;
  131.         this.year = year;
  132.         this.author = author;
  133.     }
  134.  
  135.     public String getTitle() {
  136.         return title;
  137.     }
  138.  
  139.     public String getYear() {
  140.         return year;
  141.     }
  142.  
  143.     public String getAuthor() {
  144.         return author;
  145.     }
  146.  
  147.     public int getId() {
  148.         return id;
  149.     }
  150.    
  151.     public void setId(int id) {
  152.         this.id = id;
  153.     }
  154.  
  155.     public void setBookStatusToNonAvailable() {
  156.             bookStatus = BookStatus.NON_AVAILABLE;
  157.     }
  158.  
  159.     public void setBookStatusToAvailable() {
  160.             bookStatus = BookStatus.AVAILABLE;
  161.     }
  162.  
  163.     public String getBookStatus() {
  164.         return bookStatus.toString();
  165.     }
  166.  
  167.     public BookStatus getEnumBookStatus() {
  168.         return bookStatus;
  169.     }
  170.  
  171.     public void print() {
  172.         System.out.println("-----");
  173.         System.out.println("ID number: " + this.getId());
  174.         System.out.println("Title: " + this.getTitle());
  175.         System.out.println("Year: " + this.getYear());
  176.         System.out.println("Author: " + this.getAuthor());
  177.     }
  178.  
  179.     @Override
  180.     public int hashCode() {
  181.         final int prime = 31;
  182.         int result = 1;
  183.         result = prime * result + ((author == null) ? 0 : author.hashCode());
  184.         result = prime * result + ((title == null) ? 0 : title.hashCode());
  185.         result = prime * result + ((year == null) ? 0 : year.hashCode());
  186.         return result;
  187.     }
  188.  
  189.     @Override
  190.     public boolean equals(Object obj) {
  191.         if (this == obj)
  192.             return true;
  193.         if (obj == null)
  194.             return false;
  195.         if (getClass() != obj.getClass())
  196.             return false;
  197.         Book other = (Book) obj;
  198.         if (author == null) {
  199.             if (other.author != null)
  200.                 return false;
  201.         } else if (!author.equals(other.author))
  202.             return false;
  203.         if (title == null) {
  204.             if (other.title != null)
  205.                 return false;
  206.         } else if (!title.equals(other.title))
  207.             return false;
  208.         if (year == null) {
  209.             if (other.year != null)
  210.                 return false;
  211.         } else if (!year.equals(other.year))
  212.             return false;
  213.         return true;
  214.     }
  215.  
  216. //  @Override
  217. //  public boolean equals(Object arg0) {
  218. //      if (Book.class.isInstance(arg0)) {
  219. //     
  220. //      return this.author.equals(((Book) arg0).author) &&
  221. //              this.year.equals(((Book) arg0).year) &&
  222. //              this.title.equals(((Book) arg0).title);
  223. //      }
  224. //      return false;
  225. //  }
  226.  
  227.    
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement