Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- class Library{
- class Books{
- public HashMap<String, Boolean> books = new HashMap<>(){{
- put("book one", false);
- put("book two", false);
- }};
- public Books(){
- }
- public void DisplayBooks(){
- Iterator<Map.Entry<String, Boolean>> iterator = books.entrySet().iterator();
- while (iterator.hasNext()) {
- Map.Entry<String, Boolean> entry = iterator.next();
- System.out.println(entry.getKey() + ":" + entry.getValue());
- }
- }
- }
- abstract class User{
- protected HashMap<String, Boolean> books = new HashMap<>();
- abstract void doAction(String book);
- }
- class RentingUser extends User{
- public
- RentingUser(Books obj){
- this.books = obj.books;
- }
- void doAction(String book){
- if(books.containsKey(book)){
- if(books.get(book) == true){
- System.out.println("User is returning the book");
- books.replace(book, false);
- } else {
- System.out.println("User is renting the book");
- books.replace(book, true);
- }
- }
- }
- }
- class AdminUser extends User{
- public
- AdminUser(Books obj){
- this.books = obj.books;
- }
- void doAction(String book){
- if(books.containsKey(book)){
- books.remove(book);
- System.out.println("User removed the book");
- } else {
- System.out.println("The book was not in the list");
- }
- }
- }
- public static void main(String[] args){
- Library lib = new Library();
- Books obj = lib.new Books();
- AdminUser admin = lib.new AdminUser(obj);
- RentingUser rentingUser = lib.new RentingUser(obj);
- rentingUser.doAction("book one");
- admin.doAction("book two");
- obj.DisplayBooks();
- }
- }
Add Comment
Please, Sign In to add comment