Advertisement
Oslapas

Untitled

Nov 8th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. public class Library {
  2.     private HashMap<String, Book> collection;
  3.  
  4.     public Library() {
  5.         this.collection = new HashMap<String, Book>();
  6.     }
  7.  
  8.     public Book getBook(String bookName) {
  9.         bookName = stringCleaner(bookName);
  10.         return this.collection.get(bookName);
  11.     }
  12.  
  13.     public void addBook(Book kirja) {
  14.         String name = stringCleaner(book.getName());
  15.  
  16.         if(this.collection.containsKey(name)) {
  17.             System.out.println("The book is already in the library!");
  18.         } else {
  19.             this.collection.put(name, book);
  20.         }
  21.     }
  22.  
  23.     public void removeBook(String bookName) {
  24.         bookName = stringCleaner(bookName);
  25.  
  26.         if(this.collection.containsKey(bookName)) {
  27.             this.collection.remove(bookName);
  28.         } else {
  29.             System.out.println("The book was not found, you can't remove it!");
  30.         }
  31.     }
  32.  
  33.     public ArrayList<Book> bookList() {
  34.         return new ArrayList<Book>(this.collection.values());
  35.     }
  36.  
  37.     private String stringCleaner(String string) {
  38.         if (string == null) {
  39.             return "";
  40.         }
  41.  
  42.         string = string.toLowerCase();
  43.         return string.trim();
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement