bozhilov

Lora Java HW

Apr 28th, 2022 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5.  
  6. class Library{
  7.  
  8.     class Books{
  9.  
  10.         public HashMap<String, Boolean> books = new HashMap<>(){{
  11.             put("book one", false);
  12.             put("book two", false);
  13.         }};
  14.  
  15.        
  16.         public Books(){
  17.         }
  18.  
  19.         public void DisplayBooks(){
  20.             Iterator<Map.Entry<String, Boolean>> iterator = books.entrySet().iterator();
  21.          while (iterator.hasNext()) {
  22.         Map.Entry<String, Boolean> entry = iterator.next();
  23.         System.out.println(entry.getKey() + ":" + entry.getValue());
  24.         }
  25.         }
  26.     }
  27.  
  28.     abstract class User{
  29.         protected HashMap<String, Boolean> books = new HashMap<>();
  30.         abstract void doAction(String book);
  31.     }
  32.  
  33.     class RentingUser extends User{
  34.         public
  35.         RentingUser(Books obj){
  36.             this.books = obj.books;
  37.         }
  38.  
  39.         void doAction(String book){
  40.             if(books.containsKey(book)){
  41.                 if(books.get(book) == true){
  42.                     System.out.println("User is returning the book");
  43.                     books.replace(book, false);
  44.                 } else {
  45.                     System.out.println("User is renting the book");
  46.                     books.replace(book, true);
  47.                 }
  48.             }
  49.         }
  50.        
  51.      }
  52.  
  53.     class AdminUser extends User{
  54.         public
  55.         AdminUser(Books obj){
  56.             this.books = obj.books;
  57.         }
  58.  
  59.         void doAction(String book){
  60.             if(books.containsKey(book)){
  61.                 books.remove(book);
  62.                 System.out.println("User removed the book");
  63.             } else {
  64.                 System.out.println("The book was not in the list");
  65.             }
  66.         }
  67.     }
  68.     public static void main(String[] args){
  69.         Library lib = new Library();
  70.         Books obj = lib.new Books();
  71.         AdminUser admin = lib.new AdminUser(obj);
  72.         RentingUser rentingUser = lib.new RentingUser(obj);
  73.         rentingUser.doAction("book one");
  74.         admin.doAction("book two");
  75.         obj.DisplayBooks();
  76.        
  77.     }
  78. }
Add Comment
Please, Sign In to add comment