Advertisement
Kimes

Untitled

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