Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Simple Library Management System Made By Rohan Joshi
- package com.company;
- import java.util.*;
- class librarymanagement{
- ArrayList<String> addBk = new ArrayList< >();
- HashMap<String,String> iBook = new HashMap<>();
- public void issueBook(String Name , String Book){
- iBook.put(Name,Book);
- addBk.remove(Book);
- }
- public void preAddedBooks(){
- addBk.add("Maths");
- addBk.add("Science");
- addBk.add("English");
- }
- public void addBook( String aBook){
- addBk.add(aBook);
- }
- public void availableBook() {
- for (String avlBook : addBk) {
- System.out.println(avlBook);
- }
- }
- public void issuedBooks(){
- for(String issuedBook: iBook.keySet()){
- System.out.println(" "+issuedBook+" "+iBook.get(issuedBook)+" ");
- }
- }
- //
- private static Set<String> getKeys(HashMap<String, String> iBook, String value) {
- Set<String> result = new HashSet<>();
- if (iBook.containsValue(value)) {
- for (Map.Entry<String, String> entry : iBook.entrySet()) {
- if (Objects.equals(entry.getValue(), value)) {
- result.add(entry.getKey());
- }
- // we can't compare like this, null will throws exception
- /*(if (entry.getValue().equals(value)) {
- result.add(entry.getKey());
- }*/
- }
- }
- return result;
- }
- // public boolean isIssuedBookEmpty(){
- // if (){
- // return true;
- // }
- // }
- public void returnBooks(String returnB){
- // boolean isIssuedYet = iBook.containsValue(returnB);
- if (!iBook.containsValue(returnB) ){
- System.out.println("Entered Book is not Issued in our Database");
- }
- else {
- addBk.add(returnB);
- // String getKeyFromValue = getKeys(iBook, returnB);
- // iBook.remove(getKeyFromValue);
- for (String getKeyFromValue : getKeys(iBook, returnB)) {
- iBook.remove(getKeyFromValue);
- System.out.println("Book Sucessfully Returned");
- }
- }
- }
- }
- public class library {
- public static void main(String[] args) {
- int ch;
- librarymanagement lm = new librarymanagement();
- Scanner sc = new Scanner(System.in);
- System.out.println("Welcome To Library Management System");
- lm.preAddedBooks();
- do {
- System.out.println("--------------------------------------------------------------");
- System.out.println("press 1 to Show Available Book");
- System.out.println("press 2 to Add book");
- System.out.println("press 3 to issue book");
- System.out.println("press 4 to see issued book");
- System.out.println("press 5 to return book");
- System.out.println("press 6 for Exit the program");
- System.out.println("--------------------------------------------------------------");
- System.out.print("Choice --> ");
- ch = sc.nextInt();
- switch (ch) {
- case 1:
- System.out.println("Displaying Available Books");
- System.out.println("--------------------------------------------------------------");
- lm.availableBook();
- System.out.println("--------------------------------------------------------------");
- break;
- case 2:
- System.out.println("Name of book you want to Add");
- String ind = sc.next();
- lm.addBook(ind);
- System.out.println("--------------------------------------------------------------");
- break;
- case 3:
- System.out.println("Enter your name");
- String name = sc.next();
- System.out.println("Currently Available Books");
- System.out.println("--------------------------------------------------------------");
- lm.availableBook();
- System.out.println("--------------------------------------------------------------");
- System.out.println("Enter Book name you want to issue");
- String bookName = sc.next();
- boolean ifavailable = lm.addBk.contains(bookName);
- if (ifavailable) {
- lm.issueBook(name, bookName);
- System.out.println("Sucessfully Book is issued");
- }
- else{
- System.out.println("Currently this book is not available");
- }
- break;
- case 4:
- System.out.println("Showing ISSUED Books By Person and the book nme");
- System.out.println("--------------------------------------------------------------");
- System.out.println("---Person---------------------------------BookName------------");
- if(lm.iBook.isEmpty()){
- System.out.println("-----------------Not Issued Any Books---------------------");
- }
- else lm.issuedBooks();
- System.out.println("--------------------------------------------------------------");
- break;
- case 5:
- if(lm.iBook.isEmpty()){
- System.out.println("Sorry Not any Book Issued to get return");
- }
- else{
- System.out.println("Book you want to return");
- String returnbook= sc.next();
- lm.returnBooks(returnbook);}
- break;
- case 6:
- System.out.println("Thanks for exiting the program.....");
- break;
- default:
- System.out.println("Please Select the correct option");
- }
- }while(ch!=6);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement