rohanaj60

Simple Library Management System ( Updated - Add Return Feature)

Jul 31st, 2021
1,660
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Simple Library Management System Made By Rohan Joshi
  2.  
  3. package com.company;
  4.  
  5. import java.util.*;
  6.  
  7. class librarymanagement{
  8.      ArrayList<String> addBk = new ArrayList< >();
  9.      HashMap<String,String> iBook  = new HashMap<>();
  10.   public void issueBook(String Name , String Book){
  11.  
  12.         iBook.put(Name,Book);
  13.         addBk.remove(Book);
  14.     }
  15.     public void preAddedBooks(){
  16.       addBk.add("Maths");
  17.         addBk.add("Science");
  18.         addBk.add("English");
  19.     }
  20.    public void addBook( String aBook){
  21.  
  22.         addBk.add(aBook);
  23.  
  24.     }
  25.     public void availableBook() {
  26.         for (String avlBook : addBk) {
  27.             System.out.println(avlBook);
  28.         }
  29.     }
  30.         public void issuedBooks(){
  31.             for(String issuedBook: iBook.keySet()){
  32.  
  33.                 System.out.println("   "+issuedBook+"                                  "+iBook.get(issuedBook)+"           ");
  34.             }
  35.     }
  36.     //
  37.  
  38.      private static Set<String> getKeys(HashMap<String, String> iBook, String value) {
  39.  
  40.          Set<String> result = new HashSet<>();
  41.          if (iBook.containsValue(value)) {
  42.              for (Map.Entry<String, String> entry : iBook.entrySet()) {
  43.                  if (Objects.equals(entry.getValue(), value)) {
  44.                      result.add(entry.getKey());
  45.                  }
  46.                  // we can't compare like this, null will throws exception
  47.               /*(if (entry.getValue().equals(value)) {
  48.                   result.add(entry.getKey());
  49.               }*/
  50.              }
  51.          }
  52.          return result;
  53.  
  54.      }
  55. //     public boolean isIssuedBookEmpty(){
  56. //         if (){
  57. //           return true;
  58. //         }
  59. //     }
  60.      public void returnBooks(String returnB){
  61. //      boolean isIssuedYet = iBook.containsValue(returnB);
  62.  
  63.        if (!iBook.containsValue(returnB) ){
  64.           System.out.println("Entered Book is not Issued in our Database");
  65.       }
  66.       else {
  67.           addBk.add(returnB);
  68. //      String getKeyFromValue = getKeys(iBook, returnB);
  69. //          iBook.remove(getKeyFromValue);
  70.            for (String getKeyFromValue : getKeys(iBook, returnB)) {
  71.                iBook.remove(getKeyFromValue);
  72.                System.out.println("Book Sucessfully Returned");
  73.            }
  74.       }
  75.      }
  76. }
  77. public class library {
  78.     public static void main(String[] args) {
  79.         int ch;
  80. librarymanagement lm = new librarymanagement();
  81.         Scanner sc = new Scanner(System.in);
  82.         System.out.println("Welcome To Library Management System");
  83.         lm.preAddedBooks();
  84. do {
  85.     System.out.println("--------------------------------------------------------------");
  86.     System.out.println("press 1 to Show Available Book");
  87.     System.out.println("press 2 to Add book");
  88.     System.out.println("press 3 to issue book");
  89.     System.out.println("press 4 to see issued book");
  90.     System.out.println("press 5 to return book");
  91.     System.out.println("press 6 for Exit the program");
  92.     System.out.println("--------------------------------------------------------------");
  93.     System.out.print("Choice --> ");
  94.     ch = sc.nextInt();
  95.     switch (ch) {
  96.         case 1:
  97.             System.out.println("Displaying Available Books");
  98.             System.out.println("--------------------------------------------------------------");
  99.             lm.availableBook();
  100.             System.out.println("--------------------------------------------------------------");
  101.             break;
  102.         case 2:
  103.             System.out.println("Name of book you want to Add");
  104.             String ind = sc.next();
  105.             lm.addBook(ind);
  106.             System.out.println("--------------------------------------------------------------");
  107.             break;
  108.         case 3:
  109.             System.out.println("Enter your name");
  110.             String name = sc.next();
  111.             System.out.println("Currently Available Books");
  112.             System.out.println("--------------------------------------------------------------");
  113.             lm.availableBook();
  114.             System.out.println("--------------------------------------------------------------");
  115.             System.out.println("Enter Book name you want to issue");
  116.  
  117.             String bookName = sc.next();
  118.             boolean ifavailable = lm.addBk.contains(bookName);
  119.             if (ifavailable) {
  120.                 lm.issueBook(name, bookName);
  121.                 System.out.println("Sucessfully Book is issued");
  122.             }
  123.             else{
  124.                 System.out.println("Currently this book is not available");
  125.             }
  126.             break;
  127.         case 4:
  128.             System.out.println("Showing ISSUED Books By Person and the book nme");
  129.             System.out.println("--------------------------------------------------------------");
  130.             System.out.println("---Person---------------------------------BookName------------");
  131.             if(lm.iBook.isEmpty()){
  132.                 System.out.println("-----------------Not Issued Any Books---------------------");
  133.             }
  134.             else lm.issuedBooks();
  135.             System.out.println("--------------------------------------------------------------");
  136.  
  137.             break;
  138.         case 5:
  139.             if(lm.iBook.isEmpty()){
  140.                 System.out.println("Sorry Not any Book Issued to get return");
  141.             }
  142.         else{
  143.             System.out.println("Book you want to return");
  144.             String returnbook= sc.next();
  145.             lm.returnBooks(returnbook);}
  146.             break;
  147.         case 6:
  148.             System.out.println("Thanks for exiting the program.....");
  149.             break;
  150.         default:
  151.             System.out.println("Please Select the correct option");
  152.     }
  153. }while(ch!=6);
  154.     }
  155. }
  156.  
RAW Paste Data