Kimes

Library

Mar 22nd, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 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.         library.addBook("Biografia", "2010", "Jakub Niedzielski");
  15.         library.addBook("Java For Begginers", "2016", "Jakub Niedzielski");
  16.         library.addBook("Java For Begginers", "2016", "Jakub Niedzielski");
  17.  
  18.         library.getAllByTitle("Java For Begginers");
  19.        
  20.     }
  21.  
  22. }
  23.  
  24.  
  25. import java.util.HashMap;
  26. import java.util.Map;
  27.  
  28. public class Library {
  29.  
  30.     private int uniqueID = 0;
  31.     int valueCount;
  32.     private Map<Integer, Book> bookMap = new HashMap<>();
  33.  
  34.     public void addBook(String title, String year, String author) {
  35.         uniqueID++;
  36.         Book newBook = new Book(title, year, author, uniqueID);
  37.         bookMap.put(uniqueID, newBook);
  38.  
  39.     }
  40.  
  41.     public void getInfoByID(Integer bookID) {
  42.         Book book = bookMap.get(bookID);
  43.         if (book != null) {
  44.             book.print();
  45.         }
  46.     }
  47.  
  48.     public void getStatusInfo(Integer bookID) {
  49.         Book statusInfo = bookMap.get(bookID);
  50.         System.out.println("The book of ID: " + bookID + " is " + statusInfo.getBookStatus());
  51.     }
  52.  
  53.     public void lendBook(Integer bookID, String borrowersName) {
  54.         Book bookStatus = bookMap.get(bookID);
  55.         if (bookStatus.getEnumBookStatus().equals(BookStatus.AVAILABLE)) {
  56.             bookStatus.lendBook(borrowersName);
  57.         } else {
  58.             System.out.println("This book is already lent by " + bookStatus.getBorrowersName());
  59.         }
  60.  
  61.     }
  62.  
  63.     public void returnBook(Integer bookID) {
  64.         Book bookStatus = bookMap.get(bookID);
  65.         bookStatus.returnBook();
  66.     }
  67.  
  68.     public void removeBook(Integer bookID) {
  69.         Book value = bookMap.get(bookID);
  70.         if (value != null) {
  71.             if (value.getEnumBookStatus().equals(BookStatus.AVAILABLE)) {
  72.                 bookMap.remove(bookID);
  73.             } else {
  74.                 System.out.println("Can't remove that book right now, it's lent by " + value.getBorrowersName());
  75.             }
  76.         } else {
  77.             System.out.println("Invalid ID number");
  78.  
  79.         }
  80.  
  81.     }
  82.  
  83.     public void getAllBooks() {
  84.         for (Book book : bookMap.values()) {
  85.             book.print();
  86.  
  87.         }
  88.     }
  89.  
  90.     public void getAllByTitle(String title) {
  91.         for (Book book : bookMap.values()) {
  92.             if (title.equals(book.getTitle())) {
  93.                 book.print();
  94.                 System.out.println("Number of copies: " + valueCount);
  95.             }
  96.         }
  97.  
  98.     }
  99.  
  100.     public void getAllByAuthor(String author) {
  101.         for (Book book : bookMap.values()) {
  102.             if (author.equals(book.getAuthor())) {
  103.                 book.print();
  104.             }
  105.         }
  106.     }
  107.  
  108.     public void getAllByYear(String year) {
  109.         for (Book book : bookMap.values()) {
  110.             if (year.equals(book.getYear())) {
  111.                 book.print();
  112.             }
  113.         }
  114.     }
  115.  
  116.     public void getAllByAuthorAndYear(String author, String year) {
  117.         for (Book book : bookMap.values()) {
  118.             if (author.equals(book.getAuthor()) && (year.equals(book.getYear()))) {
  119.                 book.print();
  120.             }
  121.         }
  122.     }
  123.  
  124.     public int getNumberOfCopies(String title) {
  125.         for (Book book : bookMap.values()) {
  126.             if (title.equals(book.getTitle())) {
  127.                 valueCount++;
  128.             }
  129.  
  130.         }
  131.         return valueCount;
  132.  
  133.     }
  134.  
  135.     public Library() {
  136.  
  137.     }
  138.  
  139. }
  140.  
  141.  
  142.  
  143. public class Book {
  144.  
  145.     private int id;
  146.     private String title;
  147.     private String year;
  148.     private String author;
  149.     private BookStatus bookStatus = BookStatus.AVAILABLE;
  150.     private String borrowersName;
  151.     private int numberOfCopies;
  152.  
  153.     public Book(String title, String year, String author, int uniqueID) {
  154.         this.id = uniqueID;
  155.         this.title = title;
  156.         this.year = year;
  157.         this.author = author;
  158.  
  159.     }
  160.  
  161.     public String getTitle() {
  162.         return title;
  163.     }
  164.  
  165.     public String getYear() {
  166.         return year;
  167.     }
  168.  
  169.     public String getAuthor() {
  170.         return author;
  171.     }
  172.  
  173.     public int getId() {
  174.         return id;
  175.     }
  176.  
  177.     public void lendBook(String borrowersName) {
  178.         bookStatus = BookStatus.NON_AVAILABLE;
  179.         this.borrowersName = borrowersName;
  180.     }
  181.  
  182.     public void returnBook() {
  183.         bookStatus = BookStatus.AVAILABLE;
  184.     }
  185.  
  186.     public String getBookStatus() {
  187.         return bookStatus.toString();
  188.     }
  189.  
  190.     public BookStatus getEnumBookStatus() {
  191.         return bookStatus;
  192.     }
  193.  
  194.     public String getBorrowersName() {
  195.         return borrowersName;
  196.     }
  197.  
  198.     public int getNumberOfCopies() {
  199.         return numberOfCopies;
  200.     }
  201.    
  202.     public void printNumberOfCopies() {
  203.         System.out.println(numberOfCopies);
  204.        
  205.     }
  206.  
  207.     public void print() {
  208.         System.out.println("-----");
  209.         System.out.println("ID number: " + this.getId());
  210.         System.out.println("Title: " + this.getTitle());
  211.         System.out.println("Year: " + this.getYear());
  212.         System.out.println("Author: " + this.getAuthor());
  213.         if (this.bookStatus.equals(BookStatus.NON_AVAILABLE)) {
  214.             System.out.println("This book is " + this.getBookStatus() + ", " + borrowersName + " borrowed it");
  215.         } else {
  216.             System.out.println("This book is " + this.getBookStatus());
  217.         }
  218.        
  219.     }
  220.  
  221. }
  222.  
  223.  
  224.  
  225. enum BookStatus {
  226.     AVAILABLE {
  227.         @Override
  228.         public String toString() {
  229.             return "available";
  230.         }
  231.     },
  232.     NON_AVAILABLE {
  233.         @Override
  234.         public String toString() {
  235.             return "non available";
  236.         }
  237.     }
  238.  
  239. }
Add Comment
Please, Sign In to add comment