Kimes

Library

Mar 29th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.15 KB | None | 0 0
  1. package pl.kimes.core;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Library library = new Library();
  7.  
  8.         Book b = new Book("Java For Begginers", "2017", "Jakub Niedzielski");
  9.         Book b2 = new Book("Java For Begginers", "2017", "Jakub Niedzielski");
  10.         Book b3 = new Book("Java for dummies", "2010", "Unknown");
  11.  
  12.         library.addBook(b);
  13.         library.addBook(b2);
  14.         library.addBook(b3);
  15.         library.removeBook(1);
  16.  
  17.         library.getAllBooks();
  18.  
  19.     }
  20.  
  21. }
  22. package pl.kimes.core;
  23.  
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28.  
  29. public class Library {
  30.  
  31.     private int uniqueID;
  32.     private Map<Integer, Book> bookMap = new HashMap<>();
  33.     private Map<Book, Integer> copiesMap = new HashMap<>();
  34.     private Map<Book, List<String>> borrowersMap = new HashMap<>();
  35.  
  36.     public void addBook(Book newAddbook) {
  37.         Integer id = ++uniqueID;
  38.         for (Book book : bookMap.values()) {
  39.             if (book.equals(newAddbook)) {
  40.                 int numberOfCopies = getNumberOfCopies(newAddbook);
  41.                 newAddbook.setId(id);
  42.                 numberOfCopies++;
  43.                 bookMap.put(id, newAddbook);
  44.                 copiesMap.put(newAddbook, numberOfCopies);
  45.                 return;
  46.             }
  47.         }
  48.         int numberOfCopies = 1;
  49.         newAddbook.setId(id);
  50.         bookMap.put(id, newAddbook);
  51.         copiesMap.put(newAddbook, numberOfCopies);
  52.     }
  53.  
  54.  
  55.  
  56.     public void lendBook(Integer bookID, String borrowersName) {
  57.         Book bookValues = bookMap.get(bookID);
  58.         int numberOfCopies = copiesMap.get(bookValues);
  59.         if (numberOfCopies == 0) {
  60.             System.out.println("Can't lend this book, it's been alredy lent by "
  61.                     + borrowersMap.get(bookValues).toString().replace("[", "").replace("]", ""));
  62.         } else {
  63.             List<String> borrowersList = borrowersMap.get(bookValues);
  64.             if (borrowersList == null) {
  65.                 borrowersList = new ArrayList<>();
  66.                 borrowersMap.put(bookValues, borrowersList);
  67.             }
  68.             borrowersList.add(borrowersName);
  69.  
  70.             copiesMap.put(bookValues, copiesMap.get(bookValues) - 1);
  71.  
  72.         }
  73.     }
  74.  
  75.     public void returnBook(Integer bookID, String returnersName) {
  76.         Book bookValues = bookMap.get(bookID);
  77.         List<String> borrowersList = borrowersMap.get(bookValues);
  78.         if (borrowersList == null) {
  79.             System.out.println("This book wasn't lent by you, you can't return it");
  80.             return;
  81.         }
  82.         if (!borrowersList.contains(returnersName)) {
  83.             System.out.println("Invalid name");
  84.         }
  85.  
  86.         borrowersList.remove(returnersName);
  87.         copiesMap.put(bookValues, copiesMap.get(bookValues) + 1);
  88.     }
  89.  
  90.     public void removeBook(Integer bookID) {
  91.         Book bookValues = bookMap.get(bookID);
  92.         if (bookValues != null) {
  93.             if (borrowersMap.get(bookValues) == null) {
  94.                 bookMap.remove(bookID);
  95.                 copiesMap.put(bookValues, copiesMap.get(bookValues) - 1);
  96.             } else {
  97.                 System.out.println("Can't remove that book right now, it's lent");
  98.             }
  99.         } else {
  100.             System.out.println("Invalid ID number");
  101.  
  102.         }
  103.     }
  104.    
  105.     public void getInfoByID(Integer bookID) {
  106.         Book book = bookMap.get(bookID);
  107.         if (book != null) {
  108.             book.print();
  109.             printCopies(book);
  110.         }
  111.     }
  112.  
  113.     public void getAllBooks() {
  114.         for (Book book : bookMap.values()) {
  115.             book.print();
  116.             printCopies(book);
  117.         }
  118.     }
  119.  
  120.     public void getAllByTitle(String title) {
  121.         for (Book book : bookMap.values()) {
  122.             if (title.equals(book.getTitle())) {
  123.                 book.print();
  124.                 printCopies(book);
  125.             }
  126.         }
  127.  
  128.     }
  129.  
  130.     public void getAllByAuthor(String author) {
  131.         for (Book book : bookMap.values()) {
  132.             if (author.equals(book.getAuthor())) {
  133.                 book.print();
  134.                 printCopies(book);
  135.             }
  136.         }
  137.     }
  138.  
  139.     public void getAllByYear(String year) {
  140.         for (Book book : bookMap.values()) {
  141.             if (year.equals(book.getYear())) {
  142.                 book.print();
  143.                 printCopies(book);
  144.             }
  145.         }
  146.     }
  147.  
  148.     public void getAllByAuthorAndYear(String author, String year) {
  149.         for (Book book : bookMap.values()) {
  150.             if (author.equals(book.getAuthor()) && (year.equals(book.getYear()))) {
  151.                 book.print();
  152.                 printCopies(book);
  153.             }
  154.         }
  155.     }
  156.  
  157.     private void printCopies(Book book) {
  158.         copiesMap.get(book);
  159.         System.out.println("Number of copies: " + getNumberOfCopies(book));
  160.  
  161.     }
  162.     private int getNumberOfCopies(Book book) {
  163.         return copiesMap.get(book);
  164.        
  165.     }
  166.  
  167. }
  168. package pl.kimes.core;
  169.  
  170.  
  171. public class Book {
  172.  
  173.     private int id;
  174.     private String title;
  175.     private String year;
  176.     private String author;
  177.  
  178.     public Book(String title, String year, String author) {
  179.         this.title = title;
  180.         this.year = year;
  181.         this.author = author;
  182.     }
  183.  
  184.     public String getTitle() {
  185.         return title;
  186.     }
  187.  
  188.     public String getYear() {
  189.         return year;
  190.     }
  191.  
  192.     public String getAuthor() {
  193.         return author;
  194.     }
  195.  
  196.     public int getId() {
  197.         return id;
  198.     }
  199.    
  200.     public void setId(int id) {
  201.         this.id = id;
  202.     }
  203.  
  204.  
  205.     public void print() {
  206.         System.out.println("-----");
  207.         System.out.println("ID number: " + this.getId());
  208.         System.out.println("Title: " + this.getTitle());
  209.         System.out.println("Year: " + this.getYear());
  210.         System.out.println("Author: " + this.getAuthor());
  211.     }
  212.  
  213.     @Override
  214.     public int hashCode() {
  215.         final int prime = 31;
  216.         int result = 1;
  217.         result = prime * result + ((author == null) ? 0 : author.hashCode());
  218.         result = prime * result + ((title == null) ? 0 : title.hashCode());
  219.         result = prime * result + ((year == null) ? 0 : year.hashCode());
  220.         return result;
  221.     }
  222.  
  223.     @Override
  224.     public boolean equals(Object obj) {
  225.         if (this == obj)
  226.             return true;
  227.         if (obj == null)
  228.             return false;
  229.         if (getClass() != obj.getClass())
  230.             return false;
  231.         Book other = (Book) obj;
  232.         if (author == null) {
  233.             if (other.author != null)
  234.                 return false;
  235.         } else if (!author.equals(other.author))
  236.             return false;
  237.         if (title == null) {
  238.             if (other.title != null)
  239.                 return false;
  240.         } else if (!title.equals(other.title))
  241.             return false;
  242.         if (year == null) {
  243.             if (other.year != null)
  244.                 return false;
  245.         } else if (!year.equals(other.year))
  246.             return false;
  247.         return true;
  248.     }
  249.  
  250.  
  251. // old implementation of equals method
  252. //  @Override
  253. //  public boolean equals(Object arg0) {
  254. //      if (Book.class.isInstance(arg0)) {
  255. //     
  256. //      return this.author.equals(((Book) arg0).author) &&
  257. //              this.year.equals(((Book) arg0).year) &&
  258. //              this.title.equals(((Book) arg0).title);
  259. //      }
  260. //      return false;
  261. //  }
  262.  
  263.    
  264. }
Advertisement
Add Comment
Please, Sign In to add comment