Library Management System Project in Java

The project consists of seven Java classes include one main class. The Reader can ask to barrow a book, return a book or view book or reader can recommend a book to the library.

 

Source Code

Book.Java
package LEP.uzair.lms.library;
public class Book {
 private String title;
 private String author;
 private String owner;
 private String holder;
 
 public Book(String temp_title,String temp_author,String temp_owner,String temp_holder){
  author=temp_author;
  title=temp_title;
  owner=temp_owner;
  holder=temp_holder;
 }
 Book(){
  author=null;
  title=null;
  owner=null;
  holder=null;
 }
 //getters
 public String get_title(){
  return title;
 }
 public String get_author(){
  return author;

 }
 public String get_owner(){
  return owner;
 }
 public String get_holder(){
  return holder;
 }
 //setters
 public void set_title(String temp_title){
  title=temp_title;
 }
 public void set_author(String temp_author){
  author=temp_author;
 }
 public void set_owner(String temp_owner){
  owner=temp_owner;
 }
 public void set_holder(String temp_holder){
  holder=temp_holder;
 }
 public void display(){
  System.out.println("Book Details :\n^^^^^^^^^^^^^ \n\tAuthor : "+author+"\n\tName: "+title+"\n\tOwner:"+ owner+"\n\tHolder "+holder+"\n===============");
 }
}
Library.Java
package LEP.uzair.lms.library;
import java.util.Scanner;

public class Library {
 Book[] obj_book=new Book[100];
 Reader[] obj_reader=new Reader[100];
 PAuthor[] obj_author=new PAuthor[100];
 int R_count=0;// R = reader
 int A_count=0;// A = author 
 int B_count=2;// B = book
 public Library(){
  // for first time there will be two books in library
  obj_book[0]=new Book();
  obj_book[0].set_author("Uzair");
  obj_book[0].set_title("Recommende System");
  obj_book[0].set_holder("ali");
  obj_book[1]=new Book();
  obj_book[1].set_author("Asad");
  obj_book[1].set_title("Web Programing");
 }
 public void start() {
  @SuppressWarnings("resource")
  Scanner input = new Scanner(System.in);
  char YesOrNo = 'Y';
  while (YesOrNo  =='Y'){
   System.out.print("\tQ:What do you want to do?\n\n");
   System.out.print("\t\t1 : Rread a Book \n\n");
   System.out.print("\t\t2 : Write a Book\n\n");
   System.out.print("\t\t3 : View Books\n\n");
   System.out.print("\t\t4 : Edit  a Book\n\n");
   System.out.println("\t\t\tYou Select : ");
   String switchChoice=input.next();
   switch (Integer.parseInt(switchChoice)) 
   {
    case 1: {
     read();
     break;
    }
    case 2: {
     write();
     break;
    }
    case 3: {
     show_books(B_count);
     break;
    }
    case 4: {
     edit_book();
     break;
    }
    default: {
     System.out.println("Invalid Selection");
      break;
    }
   }
   System.out.println("\n\tDo u want to Do some thing Else \n\t\tY = yes\n\t\tN = No\n");
   System.out.println("You Select : ");
   YesOrNo =(input.next()).charAt(0);
   if(Character.isLowerCase(YesOrNo )){
    YesOrNo =Character.toUpperCase(YesOrNo );
   }
  }//End While
 }// End Start()
 private void edit_book() {
  obj_author[A_count]=new PAuthor();
  show_books(B_count);
  System.out.print("\nTotal Books:"+B_count+"\nWhich book do you want to edit");
  System.out.print("\nEnter above given Book Id");
  @SuppressWarnings("resource")
  Scanner input = new  Scanner(System.in);
  String temp_id=input.next();
  int i=Integer.parseInt(temp_id);
  i=i-1;
  System.out.println(obj_book[i].get_author());
  System.out.println("\nBook Editor:"+obj_author[A_count].get_name()+" Book author:"+obj_book[i].get_author() + "\n");
  if((obj_author[A_count].get_name()).equals(obj_book[i].get_author())){
   show_count();
   obj_author[A_count].set_name(obj_book[i].get_author());
   obj_book[i]=obj_author[A_count].new_book(obj_book[i]);
  }else{
   System.out.println("Author name Error");
  }
 }
 private void write() {
  if(A_count>=0 && B_count>=0){
   //show_count();
   obj_author[A_count]=new PAuthor();
   obj_book[B_count]=new Book();
   obj_book[B_count]=obj_author[A_count].new_book(obj_book[B_count]);
   A_count=A_count+1;
   B_count=B_count+1;
  }else{
   System.out.println("Books Or Author count has an Error");
  }
 }//End write()
 private void read() {
   obj_reader[R_count]=new Reader();
   show_books(B_count);
   System.out.print("\nTotal Books:"+B_count+"\nWhich book do you want to read");
   System.out.print("\nEnter above given Book Id");
   @SuppressWarnings("resource")
   Scanner input = new  Scanner(System.in);
   String temp_id=input.next();
   char YesOrNo = 'Y';
   while (YesOrNo  =='Y'){
    System.out.print("\tQ:What do you want to do for reading?\n\n");
    System.out.print("\t\t1 : Purchase a Book \n\n");
    System.out.print("\t\t2 : Borrow a Book\n\n");
    System.out.print("\t\t3 : Return a Book\n\n");
    System.out.println("\t\t\tYou Select : "); 
    String switchChoice=input.next();
    int i=Integer.parseInt(temp_id);
    i=i-1;
    switch (Integer.parseInt(switchChoice)) 
    {
     case 1: {
      Purchase(obj_book[i],obj_reader[R_count]);
      break;
     }
     case 2: {
      Borrow(obj_book[i],obj_reader[R_count]);
      break;
     }
     case 3: {
      Return(obj_book[i],obj_reader[R_count]);
      break;
     }
     default: {
      System.out.println("Invalid Selection");
       break;
     }
    }
    System.out.println("\n\tDo u want to Read again\n\t\tY = yes\n\t\tN = No\n");
    System.out.println("You Select : ");
    YesOrNo =(input.next()).charAt(0);
    if(Character.isLowerCase(YesOrNo )){
     YesOrNo =Character.toUpperCase(YesOrNo );
    }
   }
   R_count=R_count++;
 }//End read()
 private void Return(Book book, Reader reader) {
  System.out.println("\nRequester:"+reader.get_name()+" Book Holder:"+book.get_holder() + "\n");
  if((reader.get_name()).equals(book.get_holder())){
   show_count();
   reader.set_name(book.get_holder());
   reader.return_book(book);
   System.out.println("Return Book Successful");
  }else if((book.get_holder()).equals(null)){
   System.out.println("(!)-> Not Borrowed Yet ");
  }else {
   System.out.println("\tReturn Book Error\nRequester should be equal to Book Holder");
  }
 }
 private void Borrow(Book book, Reader reader) {
  if(book.get_holder()==null){
   System.out.println(reader.get_name()+" now you have this book \""+ book.get_title()+"\"");
   book.set_holder(reader.get_name());
   System.out.println("\nBorrowing Book success\n");
  }else if((reader.get_name()).equals(book.get_holder())){
   System.out.println(reader.get_name()+" this book has been borrowed by you\n");
  }else{
   System.out.println(reader.get_name()+" this book has been borrowed by some one\n");
  }
 }//End Borrow()
 private void Purchase(Book book, Reader reader) {
//  System.out.println("\n "+reader.get_name()+" "+book.get_owner()+"");
  if(book.get_owner()==null){
   System.out.println("Your Selected Book: ");
   book.display();
   obj_reader[R_count].show_reader();
   book=reader.purchase_book(book);
   System.out.println(reader.get_name()+" now you have this book \""+ book.get_title()+"\"");
  }else if((reader.get_name()).equals(book.get_owner())){
   System.out.println(reader.get_name()+" this book has been purchased by you\n");
  }else{
   System.out.println(reader.get_name()+" this book has been purchased\n");
  }
 }//End Purchase()
 public void show_books(int num){
  for(int i=0;i<num;i++){
   int temp=i+1;
   System.out.println("Book Id:"+temp);
   obj_book[i].display(); 
  }
 }//End show_books()
 
 //////////////////////////////////////////* Test Functions *//////////////////////////////////////////////////
 
 public void show_count(){
  System.out.println("Count Author:"+A_count);
  System.out.println("Count Book:"+B_count);
  System.out.println("Count Reader:"+R_count);
 }//End show_books()
}

/* public void get_books(){
  
 }
 public void new_book(){
  
 }
 public void sell_book(){
  
 }
 public void lend_book(){
  
 }
 public void tack_back_books(){
  
 }
 public void show_books(){
  
 }
 public void get_readers(){
  
 }
 public void show_readers(){
  
 }*/

Person.Java
package LEP.uzair.lms.library;
import java.util.Scanner;

public class Person {
 protected static String name;
 Person(){
  @SuppressWarnings("resource")
  Scanner input= new Scanner(System.in);
  
  System.out.println("Enter Your Name:");
  String temp_name=input.next();
  name=temp_name;
 }
 public void set_name(String temp_name){
  //Scanner input= new Scanner(System.in);
  //System.out.println("Enter Your Name:");
  //String temp_name=input.next();
  name=temp_name;
 }
 public String get_name(){
   return name;
 }
 public String who_are_you(){
  String temp="I am :" +name;
  return temp;
 }
}
PAuthor.Java
package LEP.uzair.lms.library;
import java.util.Scanner;

public class PAuthor extends  Person{
 PAuthor(){
  super();
  System.out.println("Author name is : "+name);
 }
 public Book new_book(Book obj_book){
  @SuppressWarnings("resource")
  Scanner input= new Scanner(System.in);
  System.out.println("Enter Book Title:");
  String temp_title=input.next();
  obj_book.set_title(temp_title);
  
  obj_book.set_author(name);
  return obj_book;
 }
 public String who_are_you(Book obj_book){
  String temp="I am Author of this book"+obj_book.get_title()+"my nmae is :" +name;
  return temp;
 }
}
Reader.Java
package LEP.uzair.lms.library;
public class Reader extends Person {
 Reader(){
  super();
  System.out.println("Reader name is : "+name);
 }
 public Book purchase_book(Book obj_book){
  obj_book.set_owner(name);
  System.out.println("\n Purchase Book action successfull for : "+name);
  return obj_book;
 }
 public void borrow_book(Book obj_book){
  obj_book.set_holder(name);
  System.out.println("\nBorrow Book action successfull for : "+name);
 }
 public void return_book(Book obj_book){
  obj_book.set_holder("null");
  System.out.println("\nReturn Book action successfull for : "+name);
 }
 public void show_reader(){
  System.out.println("Reader name is : "+name);
 }
 
 public String who_are_you(Book obj_book){
  String temp="I am Reader of this book"+obj_book.get_title()+"my nmae is :" +name;
  return temp;
 }
}
Start.Java
package LEP.uzair.lms.start;
import java.util.Scanner;
import LEP.uzair.lms.library.Library;

public class Start {
 public static void StartHms() {
  // TODO Auto-generated method stub
  int c=0;
  @SuppressWarnings("resource")
  Scanner input = new Scanner(System.in);
  char YesOrNo = 'Y';
  while (YesOrNo  =='Y'){
   
   System.out.print("\t\t     ============================\n");
   System.out.print("\t\t     Welcome To Library Managment \n");
   System.out.print("\t\t               Software           \n"); 
   if(c==0){
    c++;
   }else{
    System.out.print("\t\t        Again\n");
   }
   System.out.print("\t\t     ============================\n");
   Library obj_lib=new Library();
   obj_lib.start();
   
   /*
   Book obj_book=new Book("Numarical Analysis","Albert","Bruce","uzair");
   obj_book.display();
   */
   
   System.out.println("\n\tDo u want to Continue \n\t\tY = Continue Again\n\t\tN = Exit\n");
   System.out.println("You Select : ");
   
   
   YesOrNo =(input.next()).charAt(0);
   if(Character.isLowerCase(YesOrNo )){
    YesOrNo =Character.toUpperCase(YesOrNo );
   }
  }//End While
 }
}
Mymain.Java
package LEP.uzair.lms.main;
import LEP.uzair.lms.start.Start;

public class MyMain {

 public static void main(String[] args) {
  //Static Function Demonstration
  Start.StartHms();
 }

}

Output of the Program

Library Management System Project in Java
Library Management System Project in Java 



Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment