Advertisement
Kimes

Library

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