The code is not all written yet, i only covered so far the following parts:
1- add a book to the library.
2- display the books.
3- remove a book.
4- repeat the previous functions as many times as needed.
5- exit the program.

Need help in constructing Classes for my Library system
package library;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Book {

    public String title;
    public String author;
    public String publisher;
    public String publicationYear;
    public String status;
    public String borrower;
    public String borrowDate;
    public String returnDate;

    public String status1 = "Available";
    public String status2 = "Borrowed";
    public int BookChoice;


    static ArrayList<String> UserList = new ArrayList<String>();
    static ArrayList<Book> BookList = new ArrayList<Book>();

    static int choice ;

    static Scanner userInput = new Scanner(System.in);
    static Scanner choiceInput = new Scanner(System.in);
    /*
     * Book Constructor:
     */

    /**
     * ===================================================================================================
     * Class Methods here:
     * ===================================================================================================
     */

    public static void displayFirstMenu(){
        System.out.println(">########################################################################");
        System.out.println("> Choose one of the options below by typing the corresponding number: ");
        System.out.println(">====================================================================");
        System.out.println("2- Add a book to the Library.");
        System.out.println("6- Blow up library.");
        System.out.println("7- Back to main menu.");
        System.out.println("0- Exit.");
        System.out.println(">########################################################################");
        System.out.println("> Enter your option here: ");
        choice = choiceInput.nextInt();//User inputs a choice (integer).

    }

    public static void displaySecondMenu(){
        System.out.println(">########################################################################");
        System.out.println("> Choose one of the options below by typing the corresponding number: ");
        System.out.println(">====================================================================");
        System.out.println("1- Check library list.");
        System.out.println("2- Add a book to the Library.");
        System.out.println("3- Borrow a book.");
        System.out.println("4- Return a book.");
        System.out.println("5- Delete a book.");
        System.out.println("6- Blow up library.");
        System.out.println("7- Back to main menu.");
        System.out.println("0- Exit.");
        System.out.println(">########################################################################");
        System.out.println("> Enter your option here: ");
        choice = choiceInput.nextInt();//User inputs a choice (integer).

    }

    public String displayBook(){

        String BookInfo = "----------------------------"+
                        "\nTitle:.................."+title+
                        "\nAuthor:................."+author+
                        "\nPublisher:.............."+publisher+ 
                        "\nPublicationYear:........"+publicationYear+
                        "\nStatus:................."+status+
                        "\nBorrower:..............."+borrower+
                        "\nDate Borrowed:.........."+borrowDate+
                        "\nReturn date:............"+returnDate+
                        "\n----------------------------";
        return BookInfo;    
    }

    public void createBook(){
        System.out.println("> Enter the title of the book: ");
        title = userInput.nextLine();

        System.out.println("> Enter the author of the book: ");
        author = userInput.nextLine();

        System.out.println("> Enter the publisher of the book: ");
        publisher = userInput.nextLine();

        System.out.println("> Enter the publication year of the book: ");
        publicationYear = userInput.nextLine();

        borrower = "nobody";
        borrowDate = "none";
        returnDate = "none";

        status = "Available";
    }

    public void addBook(){
        Book newBook = new Book(); //create new book object with status "Available."
        newBook.createBook();
        BookList.add(newBook);//add the book to the BookList ArrayList.
        System.out.println("---------------------------------------------------------");
        System.out.println("> You have successfully added the book to the library!\n");
        System.out.println("---------------------------------------------------------");    
    }

    public void displayBookList(){
        if (BookList.size() == 0){//If the library is empty, it goes back to main menu and choice.
            System.out.println(">-------------------------------------------------------------");
            System.out.println("> There Library is Emply! Please add a book first!\n");
            System.out.println(">-------------------------------------------------------------");
            Book.displayFirstMenu();//Display to main menu.
            choice = choiceInput.nextInt();//Register new choice.

        } else {                    
            for (int i = 0; i < BookList.size(); i++){
                System.out.printf("\n>-----------Book Index: [%s]---------------------------------\n",i+1);
                System.out.println(BookList.get(i).displayBook());  
                System.out.println(">-------------------------------------------------------------");
            }//End of For Loop.         
        }// End of Else Statement.          
    }//End of if Statement.

    public void borrowBook(){
        System.out.println("---------------------------------------------------------");
        System.out.println("> Here are all the books registered in the library: ");
        System.out.println("---------------------------------------------------------");        
        displayBookList();

        borrowLoop1:
        while(choice == 3){
            System.out.println("\n\n> Choose an available book from the above list and write down it's index number: ");
            BookChoice = choiceInput.nextInt()-1;//register user's book choice.
            if(BookChoice > BookList.size()){
                System.out.println("> The number of the book you entered is not in the list!");
                choice = 7;
            }else if(BookChoice <= BookList.size()){
                break borrowLoop1;
            }
        }       

        borrowLoop2:
        while(choice == 3){
            //Check if the book to be borrowed is available.
            if (BookList.get(BookChoice).status.equalsIgnoreCase(status1) && BookList.size() >= BookChoice){
                //Print the borrowed book information and change the book status to borrowed.
                BookList.get(BookChoice).status = "Borrowed";
                System.out.printf("\n> You have chosen the following book: %s\n", BookList.get(BookChoice).displayBook());

                //add the user name to the book borrower variable:
                BookList.get(BookChoice).borrower = borrower;
                BookList.get(BookChoice).borrowDate = "Today.";
                BookList.get(BookChoice).returnDate = "In two weeks.";
                System.out.println("> You have to return the book in two weeks!");
                choice = 7;
                break borrowLoop2;

            }else if(BookList.get(BookChoice).status.equalsIgnoreCase(status2) && BookList.size() >= BookChoice){
                System.out.println("> The Book you are trying to borrow is unavailable!");
                choice = 7;
                break borrowLoop2;
            }else if(BookChoice > BookList.size()-1){
                System.out.println("> The number you entered in not in the list!");
                choice = 7;
                break borrowLoop2;
            }
        }
    }


    public void returnBook(){
        System.out.println("> Enter the Title of the book you want to return: ");
        String returnBookTitle = userInput.nextLine();
        int x = 0;
        while (x < BookList.size()){//Search for the book by title, if it exists change it's status,
                                    //it's borrower and borrowDate.
            if (BookList.get(x).title.equalsIgnoreCase(returnBookTitle)){
                BookList.get(x).status = "Available";
                BookList.get(x).borrower = "none";
                BookList.get(x).borrowDate = "none";
                System.out.println("> You have successfully returned the book to the library!");
                Book.displayFirstMenu();//Display main menu.
                choice = choiceInput.nextInt();//Register new choice.
                break;//if a title is found, break out of the loop and display choice menu.
            }
            x = x+1;
        }//end of while loop.
        x = 0;
        while (x < BookList.size() && BookList.size() > 0){//Search for the title and if it's not in the library, 
                                    //print a warning message to the user, and register a new menu choice.
            if (BookList.get(x).title.equalsIgnoreCase(returnBookTitle)){
        }else{
            System.out.println("> The are no books with this title in the library." +
                    " Please make sure the title is spelt correctly or choose to add the book " +
                    "to the library from the main menu. ");
            Book.displayFirstMenu();//Display main menu.
            choice = choiceInput.nextInt();//Register new choice.                   
            }//End of else statement.
        }//End of while loop.
        Book.displayFirstMenu();//Display main menu.
        choice = choiceInput.nextInt();//Register new choice.
    }


    public void removeBook(){
        int i = 0;
        System.out.println("---------------------------------------------------------");
        System.out.println("> Here are all the books registered in the library: ");
        System.out.println("---------------------------------------------------------");

        while (i < BookList.size() && BookList.size() > 0){//show the user the list of all the books
            System.out.printf("\n> Book number %s:\n%s",i+1,BookList.get(i));
            i = i+1;
        }//end of while loop.

        System.out.println("\n\n> Choose an available book from the above list and write down it's number: ");
        int BookChoice = choiceInput.nextInt()-1;//register user's book choice.

        while(choice == 5){
            try{
                if (BookChoice > 0 && BookChoice < BookList.size() && BookList.get(BookChoice).status.equalsIgnoreCase("Available")){//Check if the book to be borrowed is available.
                    //Print the borrowed book information and change the book status to borrowed.
                    BookList.remove(BookChoice);
                    System.out.printf("\n> You have chosen to delete the following book: %s\n", BookList.get(BookChoice));
                    System.out.printf("\n> The Book %s is deleted.\n", BookList.get(BookChoice));
                    choice = 7;
                }
            }catch(InputMismatchException error){
                System.out.println("<ERROR> Please enter a number of book from the list: ");
                choiceInput.nextInt();
                choice = 5;
            }catch(IndexOutOfBoundsException error){
                System.out.println("<ERROR> Please enter a number of book from the list: ");
                choice = 5;
            }
        }       
    }


    public void emptyLibrary(){
        System.out.println("> WARNING < You have chosen to delete all books in the library! ");
        System.out.println("> Are you sure?? Enter yes or no: ");
        String confirmation = userInput.nextLine();
        try{
            if(confirmation.equalsIgnoreCase("yes")){
                System.out.println("> Library is being deleted...");
                BookList.clear();
                System.out.println("> Library is Empty!");
                choice = 7;
            }
        }catch(InputMismatchException error){
            System.out.println("<ERROR> Make sure you spell yes or no correctrly: ");
            choice = 6;
        }
    }


    public void addUser(){
        System.out.println("> Enter your name: ");
        borrower = userInput.nextLine();
        UserList.add(borrower); //Add the userName to the UserList arrayList.   
    }

    public void run(){

        System.out.println("@TEST@ <<< 1 >>>>");

        addUser();
        System.out.println("@TEST@ <<< 2 >>>>");

        Book.displayFirstMenu();//Displays the main menu and ask for choice.

        System.out.println("@TEST@ <<< 3- Entering main while loop...>>>>");

        exit:

            while(choice != 0){ 
                try{
//Choice 1:                 
                    if(choice == 1 && BookList.size() > 0){

                        displayBookList();
                        choice = 7;
                    }

                    if(choice == 1 && BookList.size() == 0){
                        System.out.println("<ERROR> Library is empty! Please add a Book first!");
                        choice = 7;
                    }
//Choice 2:                 
                    if(choice == 2){
                        //createBook();
                        addBook();
                        displaySecondMenu();
                    }
//Choice 3:                 
                    if(choice == 3){
                        if(BookList.size() > 0){
                            borrowBook();                           
                        }                       
                    }
//Choice 4:                 
                    if(choice == 4){
                        returnBook();
                    }
//Choice 5:                 
                    if(choice == 5){
                        removeBook();
                        try{
                            if(BookList.size() > 0){
                                displaySecondMenu();
                            }
                        }catch(IndexOutOfBoundsException error){
                            System.out.println("<ERROR> The array is Empty! Please add a book first!");
                            choice = 7;
                            //break; //Test the Break statement!!!!!!!!!!!!!!!!!!!
                        }
                    }
//Choice 6:                 
                    if(choice == 6){
                        emptyLibrary();                     
                    }
//Choice 7:                 
                    if(choice == 7){
                        if(BookList.size() > 0){
                            displaySecondMenu();
                        }else if(BookList.size() == 0){                         
                            displayFirstMenu();
                        }
                    }
//Choice 0:                 
                    if(choice == 0){
                        break exit;
                    }
                }catch(InputMismatchException error){               
                    System.out.println("@TEST@ <<< 5- Breaking from main while loop... >>>>");
                    break exit;
                }

            }//end of while loop.

        System.out.println("####  You have Exited the Library!  ####");

        }//End of run() method.


    /**
     * ===================================================================================================
     * End of Class Methods.
     * ===================================================================================================
     */

    public static void main(String[] args){

        System.out.println("> Welcome to the library!");

        Book newBook = new Book();
        newBook.run();

    }//End of Main Method.

}

Edited 4 Years Ago by pyTony: See formatting instructions for code tagging!

i think you shopuld create several classes for your library system, it will be much easier.
this is a simple trial code for a library system managing 2 libraries for a town.
it might help.

 public class Book {

    String title;
    boolean borrowed;

    public Book(String bookTitle){
        title=bookTitle;
        borrowed=false;

    }
    public void borrowed(){
        borrowed=true;
    }
    public void returned(){
        borrowed = false;
    }
    public boolean isBorrowed(){
        return borrowed;
    }
    public String getTitle(){
        return title;
    }
   }

define class library

public class Library {

    String address;
    Book bookList[]=new Book[4];
    int count=0;
    boolean available;
    int index=99;
    int index2;

   public Library(String str){

       address=str;

   } 
   public void addBook(Book example){
       bookList[count]=example;
       count++;
   }

   static void printOpeningHours(){
       System.out.println("Libraries are open daily from 9am to 5pm");

   }

   public void borrowBook(String title){

       //first check if the book exists in the library
       for(int k=0; k<4; k++){
           if (bookList[k]==null)
               break;
           if(bookList[k].getTitle().equals(title)) {
           available=true;
           index=k;
                }  

       }
       //check if the book is borrowed or not
      while(bookList[0]!=null){
      if (bookList[index].isBorrowed()== false && available==true ){//book exists and is not borrowed
            System.out.println("You successfully borrowed " +bookList[index].getTitle());
            bookList[index].borrowed(); 
      } 
      else if (bookList[index].isBorrowed()==true && available==true){//book exists and is borrowed
          System.out.println("Sorry, this book is already borrowed");
      }

                break;

   }//end of while statement
         if(index==99)   //book doesnt exist
          System.out.println("Sorry, this book is not in our catalog");
       }


   public void printAddress(){
       System.out.println(address);
   }

   public void printAvailableBooks(){

       for(index2=0;index2<4;index2++){
           if (bookList[index2]==null)
               break;
          if(bookList[index2].isBorrowed()==false)
          System.out.println(" "+bookList[index2].getTitle());

       }
       if(bookList[0]==null){
           System.out.println("no book in catalog");
       }
   }

   public void returnBook(String str){
       for(int m=0; m<4; m++){
           if (bookList[m]==null)
               break;
           if(bookList[m].getTitle().equalsIgnoreCase(str)){
               bookList[m].returned();
           }
       }

   }
    public static void main(String args[]){

        //create new libraries
        Library firstLibrary=new Library("10 Main St");
        Library secondLibrary=new Library("228 Liberty St."); 

        //add books to library database
        firstLibrary.addBook(new Book("The Da Vinci Code"));
        firstLibrary.addBook(new Book("Le Petit Prince"));
        firstLibrary.addBook(new Book("A Tale Of Two Cities"));
        firstLibrary.addBook(new Book("The Lord Of The Rings"));

        //print opening hours
        System.out.println("Library Hours");
        printOpeningHours();
        System.out.println();

        //print library addresses
        System.out.println("Library addresses");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        System.out.println();

        //to borrow lord of the rings from both libraries
        System.out.println("borrowing The Lord Of The Rings ");
        firstLibrary.borrowBook("The Lord Of The Rings");
        firstLibrary.borrowBook("The Lord Of The Rings");
        secondLibrary.borrowBook("The Lord Of The Rings");
        System.out.println();

        //print titles of available books
        System.out.println("Books available in the first library");
        firstLibrary.printAvailableBooks();
        System.out.println();
        System.out.println("Books available in the second library");
        secondLibrary.printAvailableBooks();
        System.out.println();

        //return the lord of the rings to the first library
         System.out.println("returning The Lord Of The Rings ");
         firstLibrary.returnBook("The Lord Of The Rings");
         System.out.println();

          //print titles of available books
        System.out.println("Books available in the first library");
        firstLibrary.printAvailableBooks();

    }
}

Edited 4 Years Ago by Ezzaral: Fixed code formatting.

This article has been dead for over six months. Start a new discussion instead.