Advertisement
Kimes

Library app

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