Library Management System in Java
Library Management System in Java
Submitted By:
Akash Mudgal (BITS ID: 202017b2001)
Sachin Shikarwar (BITS ID: 202017b2005)
Riya Tripathi (BITS ID: 202017b2025)
Shivansh Pandey (BITS ID: 202017b2024)
Abstract:
Actors:
The actors include the following:
❏ Librarian
❏ Checkout Clerk
❏ Borrower
❏ Administrator
Use Cases:
Below are the use cases covered, in the context of each of the Actors:
Borrower:
❏ Search for items by title.
❏ ... by author.
❏ ... by subject.
❏ Place a book on hold if it is on loan to somebody else.
❏ Check the borrower’s personal information and list of books currently
borrowed.
Checkout Clerk:
❏ All the Borrower use cases, plus
❏ Check out an item for a borrower.
❏ Check in an item that has been returned.
❏ Renew an item.
❏ Record that a fine has been paid.
❏ Add a new borrower.
❏ Update a borrower’s personal information (address, telephone number etc.).
Librarian:
❏ All of the Borrower and Checkout Clerk use cases, plus
❏ Add a new item to the collection.
❏ Delete an item from the collection.
❏ Change the information the system has recorded about an item.
Administrator:
❏ Add Clerk.
❏ Add Librarian.
❏ View Issued Books History.
❏ View All Books in Library.
Code in Execution:
To run the code, go to the java-library-management-system folder.
Compile using below commands:
When trying to issue the same book to a user, the system does not allow so with a
message:
3. Check personal info of borrower (and the books issued to them):
11. Update a book’s information. This lets you update any attribute of a book, such as title, author
and subject.
12. Check personal info of a clerk
The Clerk Interface
Login with a Clerk’s ID:
Below is the Clerk’s User interface:
A Clerk has limited access compared to a Librarian; He can assist in Checkin/Check out of books, check
borrower’s information, update Borrower’s information, search for books, and extend deadlines.
Borrower’s Portal
1. Checking his own information: A borrower can check his personal information, as well as te
records of book he is currently issued:
2. Borrower can also check if any of his books are past deadline, and if there’s a fine
package LMS;
import java.io.BufferedReader;
import java.io.*;
import java.util.*;
/* */
public Book(int id,String t, String s, String a, boolean issued) //
Parameterise cons.
{
currentIdNumber++;
if(id==-1)
{
bookID = currentIdNumber;
}
else
bookID=id;
title = t;
subject = s;
author = a;
isIssued = issued;
if(input.equals("y"))
{
System.out.println("\nEnter new Author: ");
author = reader.readLine();
}
if(input.equals("y"))
{
System.out.println("\nEnter new Title: ");
title = reader.readLine();
}
// Issuing a Book
public void issueBook(Borrower borrower, Staff staff)
{
if (isIssued)
{
System.out.println("\nThe book " + title + " is already issued.");
}else{
//If there are no hold requests for this book, then simply issue the
book.
setIssuedStatus(true);
Library.getInstance().addLoan(iHistory);
borrower.addBorrowedBook(iHistory);
// Returning a Book
public void returnBook(Borrower borrower, Loan l, Staff staff)
{
l.getBook().setIssuedStatus(false);
l.setReturnedDate(new Date());
l.setReceiver(staff);
borrower.removeBorrowedBook(l);
l.payFine();
Class Person:
package LMS;
if(idNum==-1)
{
id = currentIdNumber;
}
else
id = idNum;
password = Integer.toString(id);
this.name = name;
this.address = address;
phoneNo = phoneNum;
}
/*---------Setter FUNCs.---------*/
public void setAddress(String a)
{
address = a;
}
/*-------Getter FUNCs.--------*/
public String getName()
{
return name;
}
public String getPassword()
{
return password;
}
Class Staff:
package LMS;
@Override
public void printInfo()
{
super.printInfo();
System.out.println("Salary: " + salary + "\n");
}
Class Clerk:
package LMS;
public Clerk(int id, String n, String a,long ph, double s,int dk) // para
cons.
{
super(id,n,a,ph,s);
if(dk == -1)
{
deskNo = currentdeskNumber;
}
else
{
deskNo=dk;
}
currentdeskNumber++;
}
Class Librarian:
package LMS;
if (of == -1)
officeNo = currentOfficeNumber;
else
officeNo = of;
currentOfficeNumber++;
}
Class Borrower:
package LMS;
import java.io.*;
import java.util.*;
printBorrowedBooks();
}
System.out.println("-------------------------------------------------
-----------------------------");
System.out.println("No.\t\tTitle\t\t\tAuthor\t\t\tSubject");
System.out.println("-------------------------------------------------
-----------------------------");
updateBorrowerName(choice, reader);
updateBorrowerAddress(choice, reader);
updateBorrowerPhoneNumber(choice, sc);
/*-------------------------------------------*/
/*-----------Getter FUNCs. ------------------*/
public ArrayList<Loan> getBorrowedBooks()
{
return borrowedBooks;
}
/*-------------------------------------------*/
}
Class Loan:
package LMS;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Scanner;
public Loan(Borrower bor, Book b, Staff i, Staff r, Date iDate, Date rDate,
boolean fPaid) // Para cons.
{
borrower = bor;
book = b;
issuer = i;
receiver = r;
issuedDate = iDate;
dateReturned = rDate;
finePaid = fPaid;
}
public Staff getIssuer() //Returns the Staff Member who issued the book
{
return issuer;
}
public Borrower getBorrower() //Returns the Borrower to whom the book was
issued
{
return borrower;
}
/*----------Setter FUNCs.---------------------*/
public void setReturnedDate(Date dReturned)
{
dateReturned = dReturned;
}
//-----------Computing Fine-----------
double totalFine = 0;
if (!finePaid)
{
Date iDate = issuedDate;
Date rDate = new Date();
if(days>0)
totalFine = days * Library.getInstance().per_day_fine;
else
totalFine=0;
}
return totalFine;
}
if (totalFine > 0)
{
System.out.println("\nTotal Fine generated: Rs " + totalFine);
if(choice.equals("y") || choice.equals("Y"))
finePaid = true;
if(choice.equals("n") || choice.equals("N"))
finePaid = false;
}
else
{
System.out.println("\nNo fine is generated.");
finePaid = true;
}
}
package LMS;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
return obj;
}
/*---------------------------------------------------------------------*/
/*--------------------------------------*/
/*-----------Getter FUNCs.------------*/
/*---------------------------------------*/
// Setter Func.
public void setName(String n)
{
name = n;
}
/*----------------------------------------------*/
//When this function is called, only the pointer of the book placed in
booksInLibrary is removed. But the real object of book
//is still there in memory because pointers of that book placed in
IssuedBooks and ReturnedBooks are still pointing to that book. And we
//are maintaining those pointers so that we can maintain history.
public void removeBookfromLibrary(Book b)
{
boolean delete = true;
if (delete)
{
System.out.println("\nCurrently this book is not borrowed by
anyone.");
booksInLibrary.remove(b);
System.out.println("The book is successfully removed.");
}
else
System.out.println("\nDelete Unsuccessful.");
}
while (true)
{
System.out.println("\nEnter either '1' or '2' or '3' for search by
Title, Subject or Author of Book respectively: ");
choice = sc.next();
if (choice.equals("1"))
{
System.out.println("\nEnter the Title of the Book: ");
title = reader.readLine();
}
else if (choice.equals("2"))
{
System.out.println("\nEnter the Subject of the Book:
");
subject = reader.readLine();
}
else
{
System.out.println("\nEnter the Author of the Book: ");
author = reader.readLine();
}
//Retrieving all the books which matched the user's search query
for(int i = 0; i < booksInLibrary.size(); i++)
{
Book b = booksInLibrary.get(i);
if (choice.equals("1"))
{
if (b.getTitle().equals(title))
matchedBooks.add(b);
}
else if (choice.equals("2"))
{
if (b.getSubject().equals(subject))
matchedBooks.add(b);
}
else
{
if (b.getAuthor().equals(author))
matchedBooks.add(b);
}
}
//Printing all the matched Books
if (!matchedBooks.isEmpty())
{
System.out.println("\nThese books are found: \n");
System.out.println("-------------------------------------------------
-----------------------------");
System.out.println("No.\t\tTitle\t\t\tAuthor\t\t\tSubject");
System.out.println("-------------------------------------------------
-----------------------------");
return matchedBooks;
}
else
{
System.out.println("\nSorry. No Books were found related to your
query.");
return null;
}
System.out.println("-------------------------------------------------
-----------------------------");
System.out.println("No.\t\tTitle\t\t\tAuthor\t\t\tSubject");
System.out.println("-------------------------------------------------
-----------------------------");
for (int i = 0; i < booksInLibrary.size(); i++)
{
System.out.print(i + "-" + "\t\t");
booksInLibrary.get(i).printInfo();
System.out.print("\n");
}
}
else
System.out.println("\nCurrently, Library has no
books.");
}
/*--------------------------------------------- */
int id = 0;
try{
id = scanner.nextInt();
}
catch (java.util.InputMismatchException e)
{
System.out.println("\nInvalid Input");
}
try{
id = scanner.nextInt();
}
catch (java.util.InputMismatchException e)
{
System.out.println("\nInvalid Input");
}
/*-------------------------------------------------------------------------
*/
//Computes total fine for all loans of a borrower
public double computeFine2(Borrower borrower)
{
System.out.println("-----------------------------------------------------
---------------------------------------------------------------------------------
-------------------------------");
System.out.println("No.\t\tBook's Title\t\tBorrower's Name\t\t\tIssued
Date\t\t\tReturned Date\t\t\t\tFine(Rs)");
System.out.println("-----------------------------------------------------
---------------------------------------------------------------------------------
-----------------------------");
double totalFine = 0;
double per_loan_fine = 0;
totalFine += per_loan_fine;
}
}
return totalFine;
}
long phone = 0;
try{
System.out.println("Enter Phone Number: ");
phone = sc.nextLong();
}
catch (java.util.InputMismatchException e)
{
System.out.println("\nInvalid Input.");
}
try{
System.out.println("Enter Salary: ");
salary = sc.nextDouble();
}
catch (java.util.InputMismatchException e)
{
System.out.println("\nInvalid Input.");
}
addBookinLibrary(b);
int id = 0;
String password = "";
System.out.println("\nEnter ID: ");
try{
id = input.nextInt();
}
catch (java.util.InputMismatchException e)
{
System.out.println("\nInvalid Input");
}
if(librarian!=null)
{
if (librarian.getID() == id &&
librarian.getPassword().equals(password))
{
System.out.println("\nLogin Successful");
return librarian;
}
}
if (loans.get(i).getReceiver() != null)
{
System.out.print("\t" + loans.get(i).getReceiver().getName()
+ "\t\t" + loans.get(i).getReturnDate() +"\t " + loans.get(i).getFineStatus() +
"\n");
}
else
System.out.print("\t\t" + "--" + "\t\t\t" + "--" + "\t\t" +
"--" + "\n");
}
}
else
System.out.println("\nNo issued books.");
}
Class CSVReader.java
package LMS;
//Class to read the csv files, whic represent the database tables for each of the
entities in the project
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.nio.charset.StandardCharsets;
import LMS.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.*;
while(true)
{
System.out.println("\nEnter Choice: ");
choice = input.next();
else
System.out.println("\nInvalid Input.");
}
}
//Search Book
if (choice == 1)
{
lib.searchForBooks();
}
if(bor!=null)
bor.printInfo();
}
else
person.printInfo();
}
if(bor!=null)
{
double totalFine = lib.computeFine2(bor);
System.out.println("\nYour Total Fine is : Rs " + totalFine
);
}
}
else
{
double totalFine = lib.computeFine2((Borrower)person);
System.out.println("\nYour Total Fine is : Rs " + totalFine
);
}
}
//Issue a Book
else if (choice == 4)
{
ArrayList<Book> books = lib.searchForBooks();
if (books != null)
{
input = takeInput(-1,books.size());
Book b = books.get(input);
System.out.println(" Enter 1 to assign this book to existing
borrower in system, or 2 to add register a new borrower: ");
choice=takeInput(0, 3);
if(choice == 1){
Borrower bor = lib.findBorrower();
if(bor!=null)
{
b.issueBook(bor, (Staff)person);
}
}else if(choice==2){
Borrower bor=(Borrower)lib.createPerson('b');
if(bor!=null)
{
b.issueBook(bor, (Staff)person);
}
}
}
}
//Return a Book
else if (choice == 5)
{
Borrower bor = lib.findBorrower();
if(bor!=null)
{
bor.printBorrowedBooks();
ArrayList<Loan> loans = bor.getBorrowedBooks();
if (!loans.isEmpty())
{
input = takeInput(-1,loans.size());
Loan l = loans.get(input);
l.getBook().returnBook(bor, l, (Staff)person);
}
else
System.out.println("\nThis borrower " + bor.getName() + " has
no book to return.");
}
}
//Renew a Book
else if (choice == 6)
{
Borrower bor = lib.findBorrower();
if(bor!=null)
{
bor.printBorrowedBooks();
ArrayList<Loan> loans = bor.getBorrowedBooks();
if (!loans.isEmpty())
{
input = takeInput(-1,loans.size());
loans.get(input).renewIssuedBook(new java.util.Date());
}
else
System.out.println("\nThis borrower " + bor.getName() + " has
no issued book which can be renewed.");
}
}
if(bor != null)
bor.updateBorrowerInfo();
}
System.out.println("\nEnter Title:");
String title = reader.readLine();
System.out.println("\nEnter Subject:");
String subject = reader.readLine();
System.out.println("\nEnter Author:");
String author = reader.readLine();
//Remove a Book
else if (choice == 10)
{
ArrayList<Book> books = lib.searchForBooks();
if (books != null)
{
input = takeInput(-1,books.size());
lib.removeBookfromLibrary(books.get(input));
}
}
if (books!=null)
{
input = takeInput(-1,books.size());
books.get(input).changeBookInfo();
}
}
if(clerk!=null)
clerk.printInfo();
}
// Functionality Performed.
System.out.println("\nPress any key to continue..\n");
scanner.next();
}
// FRONT END //
System.out.println("-------------------------------------------------
-------");
System.out.println("\tWelcome to Library Management System");
System.out.println("-------------------------------------------------
-------");
int choice = 0;
choice = takeInput(0,4);
if (choice == 3)
{
System.out.println("\nEnter Password: ");
Scanner admin= new Scanner(System.in);
String aPass = admin.next();
if(aPass.equals("lib"))
{
while (true) // Way to Admin Portal
{
clearScreen();
System.out.println("-------------------------------------
-------------------");
System.out.println("\tWelcome to Admin's Portal");
System.out.println("-------------------------------------
-------------------");
System.out.println("Following Functionalities are
available: \n");
System.out.println("-------------------------------------
--------");
choice = takeInput(0,6);
if (choice == 5)
break;
if (choice == 1)
lib.createPerson('c');
else if (choice == 2)
lib.createPerson('l');
else if (choice == 3)
lib.viewHistory();
else if (choice == 4)
lib.viewAllBooks();
else if (choice == 1)
{
Person person = lib.login();
if (person == null){}
else if (person.getClass().getSimpleName().equals("Borrower"))
{
while (true) // Way to Borrower's Portal
{
clearScreen();
System.out.println("-------------------------------------
-------------------");
System.out.println("\tWelcome to Borrower's Portal");
System.out.println("-------------------------------------
-------------------");
System.out.println("Following Functionalities are
available: \n");
System.out.println("1- Search a Book");
System.out.println("2- Check Personal Info of Borrower");
System.out.println("3- Check Total Fine of Borrower");
System.out.println("4- Logout");
System.out.println("-------------------------------------
-------------------");
choice = takeInput(0,5);
if (choice == 4)
break;
allFunctionalities(person,choice);
}
}
else if (person.getClass().getSimpleName().equals("Clerk"))
{
while(true) // Way to Clerk's Portal
{
clearScreen();
System.out.println("-------------------------------------
-------------------");
System.out.println("\tWelcome to Clerk's Portal");
System.out.println("-------------------------------------
-------------------");
System.out.println("Following Functionalities are
available: \n");
System.out.println("1- Search a Book");
System.out.println("2- Check Personal Info of Borrower");
System.out.println("3- Check Total Fine of
Borrower");
System.out.println("4- Check out a Book");
System.out.println("5- Check in a
Book");
System.out.println("6- Renew a Book");
System.out.println("7- Add a new Borrower");
System.out.println("8- Update a Borrower's Info");
System.out.println("9- Logout");
System.out.println("-------------------------------------
-------------------");
choice = takeInput(0,10);
if (choice == 9)
break;
allFunctionalities(person,choice);
}
}
else if (person.getClass().getSimpleName().equals("Librarian"))
{
while(true) // Way to Librarian Portal
{
clearScreen();
System.out.println("-------------------------------------
-------------------");
System.out.println("\tWelcome to Librarian's Portal");
System.out.println("-------------------------------------
-------------------");
System.out.println("Following Functionalities are
available: \n");
System.out.println("1- Search a Book");
System.out.println("2- Check Personal Info of Borrower");
System.out.println("3- Check Total Fine of
Borrower");
System.out.println("4- Check out a Book");
System.out.println("5- Check in a
Book");
System.out.println("6- Renew a Book");
System.out.println("7- Add a new Borrower");
System.out.println("8- Update a Borrower's Info");
System.out.println("9- Add new Book");
System.out.println("10- Remove a Book");
System.out.println("11- Change a Book's Info");
System.out.println("12- Check Personal Info of
Clerk");
System.out.println("13- Logout");
System.out.println("-------------------------------------
-------------------");
choice = takeInput(0,14);
if (choice == 13)
break;
allFunctionalities(person,choice);
}
}
else
stop = true;