Advertisement
MrDoyle

OOP) Personal Library

Apr 9th, 2021
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. public class Library {
  4.  
  5.     //Library constructor
  6.     public Library () {
  7.     }
  8.  
  9.     //Method to get a list of finished books. Parameter of a Hashmap.
  10.     public void getFinishedBooks (HashMap<String,Boolean> library){
  11.         System.out.println();
  12.         if (library.size()<1){
  13.             System.out.println("Your book-list is empty.");
  14.         } else {
  15.             System.out.println("Your completed list of finished books:");
  16.             //for each key (book) in the library keyset..."
  17.             for(String book:library.keySet()){
  18.                 if ( library.get(book) == true){
  19.                     System.out.println(book);
  20.                 }
  21.  
  22.             }
  23.         }
  24.     }
  25.  
  26.     //Method to get a list of unfinished books.
  27.     public void getUnfinishedBooks (HashMap<String,Boolean> library) {
  28.         System.out.println();
  29.         if (library.size() < 1) {
  30.             System.out.println("Your book-list is empty.");
  31.         } else {
  32.             System.out.println("Your list of unfinished books:");
  33.             for (String book : library.keySet()) {
  34.                 if (library.get(book) == false) {
  35.                     System.out.println(book);
  36.                 }
  37.             }
  38.         }
  39.     }
  40.  
  41.     public static void main(String[] args) {
  42.         //Create a new HAshmap called myBooks, the key is a String and the value is a Boolean.
  43.         HashMap<String,Boolean> myBooks = new HashMap<>();
  44.  
  45.         //Add new keys and values to the Hashmap
  46.         myBooks.put("Road Down The Funnel", true);
  47.         myBooks.put("Rat: A Biology", false);
  48.         myBooks.put("TimeIn", true);
  49.         myBooks.put("3D Food Printing", false);
  50.  
  51.         //Create/instantiate a new Library object
  52.         Library myLibrary = new Library();
  53.  
  54.         //Call the getFinishedBooks method, with the HashMap as the parameter.
  55.         myLibrary.getFinishedBooks(myBooks);
  56.         myLibrary.getUnfinishedBooks(myBooks);
  57.  
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement