Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am making a library application in Java that will allow me to search, check ou

ID: 3777605 • Letter: I

Question

I am making a library application in Java that will allow me to search, check out, and return a book. I completed the search option, but am struggling to figure out how to build the check out method. Here is my code so far. I have two classes. Library is my main class.

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Library {

   public static void main(String[] args) {

       // TODO Auto-generated method stub

      

      

       Scanner input = new Scanner(System.in);

      

       int a;

       boolean good;

      

       System.out.println("Welcome to Dropout library");

       delay();

      

       System.out.println("Please enter your name");

      

       String name = input.next();

      

       do{

       System.out.println(name + ", Please select from the following options: Press 1 to search a book Press 2 to checkout a book press 3 to return a book press 4 to exit");

      

      

      

       a = input.nextInt();

      

      

       if( a==1){

          

           good = true;

           System.out.println(" What book would you like to search? Type in the title of the book.");

           String books = input.next();

           System.out.print( " The book " + books + Book.titleSearch(books));

           }

          

      

      

       else if (a == 2){

          

           good = true;

           System.out.println("Enter the book you want to check out " );

             

          

       }

      

       else if(a == 3){

           good = true;

           System.out.print("What book are you returning today?");

       }

      

       else if(a == 4){

           good = true;

           System.out.print("See you agian soon");

       }

      

       else{

           System.out.print("Sorry invalid input, try again ");

           good = false;

       }

       }

       while(!(good) || a==1 || a==2 || a==3);

          

   }      

      

       public static void delay(){

           try{  

              

               Thread.sleep(1000);

      

      

       }catch(java.lang.InterruptedException ex){

           System.out.print(ex);

       }

       }

      

      

             

             

  

}

      

import java.io.Serializable;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Book implements Serializable{

  

   private String title;

   private String author;

   private String genre;

   private int isbnNumber;

   private int quantity;

   public boolean available = true;

  

  

   public Book(String title, String author, String genre, int isbnNumber, int quantity) {

      

       this.title = title;

       this.author = author;

       this.genre = genre;

       this.isbnNumber = isbnNumber;

       this.quantity = quantity;

      

      

   }

   public int getQuantity() {

       return quantity;

   }

   public void setQuantity(int quantity) {

       this.quantity = quantity;

   }

   public String getTitle() {

       return title;

   }

   public void setTitle(String title) {

       this.title = title;

   }

   public String getAuthor() {

       return author;

   }

   public void setAuthor(String author) {

       this.author = author;

   }

   public String getGenre() {

       return genre;

   }

   public void setGenre(String genre) {

       this.genre = genre;

   }

   public int getIsbnNumber() {

       return isbnNumber;

   }

   public void setIsbnNumber(int isbnNumber) {

       this.isbnNumber = isbnNumber;

   }

  

  

   public static String titleSearch(String title){

   Book book1 = new Book("Java" ,"hd" , "Horror", 192837647, 2);

   Book book2 = new Book("Constructors", "John","ScienceFiction", 839284823, 3);

   Book book3 = new Book("History_of_Java", "Albert", "History", 928374651, 4);

   List<Book> books = new ArrayList<Book>();

   books.add(book1);

   books.add(book2);

   books.add(book3);

  

   for(int i = 0; i<books.size(); i++){

       if(title.equals(books.get(i).getTitle())){

           title = (" is available " );

           break;

         

}

  

       for(int j = 1; j<books.size(); j++){

           if(title.equals(books.get(j).getTitle())){

               title = (" is available ");

               break;

                 

           }

          

           for(int k = 2; k<books.size(); k++){

               if(title.equals(books.get(k).getTitle())){

                   title = (" is available ");

                   break;

               }

   else{

       title = (" is not available ");

         

}

  

}

   return title;

   }

  

       return title;

  

   }

  

   return title;

}

}

Explanation / Answer

For Checkout the solution in your code. Instead of creating one list object Books you can create two, one is BooksInLibrary and another is IssueBook or checkOut Books. So when user want to search the book just do what you are doing but use this time BooksInLibrary list which we have created. You can add extra thing if you want. You can search both IssueBook list and BooksInLibrary and send the message to user depending on the scenario. Suppose book "Harry potter" is available and Book ""green mile" is issue. So when user search in library first search it in BooksInLibrary list if it found say to user "is available" , otherwise check issueBook list, search it if its there send response to user, "sorry this has already been booked". If its not in any of this list return "not available" as you are returning.

Now checkout - i think now you might be getting the hint how to do that. We have two list so we will use those two. Initially add all books in BooksInLibrary list and issueBook list is empty. So suppose user wants to checout "java" books as it is available in the BooksinLibrary list , just search it availablity , if its available and not issed then remove this book from BooksInlibrary list and add it to issueBook and return positive response message. Now if user return the book just delete it from issueBook list and add it to BooksInLibrary. tada.

Try this if you get any problem ask me. I have not provided the code because personally i think you can solve it by on your own. If you still face problem I am here for help.

Some suggestion so that you can improve your code.

1)

for(int i = 0; i<books.size(); i++){

       if(title.equals(books.get(i).getTitle())){

           title = (" is available " );

           break;

         

}

  

       for(int j = 1; j<books.size(); j++){

           if(title.equals(books.get(j).getTitle())){

               title = (" is available ");

               break;

                 

           }

          

           for(int k = 2; k<books.size(); k++){

               if(title.equals(books.get(k).getTitle())){

                   title = (" is available ");

                   break;

               }

   else{

       title = (" is not available ");

         

}

You are using for loops so you don't need to mention it 3 times.

Just for(int i = 0;i < books.size();++i){

// if condition and break it if match

}

2)

You can add the books from file. So that your library will be big.

3) Use switch case instead of if else , your code looks more beautiful cosidering in your scenario.

Code :

Books.java

Library.java

file book-

O/p

Welcome to Dropout library
Please enter your name
Marry
Title: java
Author: hd
Genre: programming
ISBN: 129183
Quantity Left: 3
Title: harry
Author: jk rowling
Genre: fantasy
ISBN: 232389
Quantity Left: 2
Title: the fault in our stars
Author: john green
Genre: romantic
ISBN: 223232
Quantity Left: 1
Hi Marry
Please select from the following options:
Press 1 to search a book
Press 2 to checkout a book
press 3 to return a book
press 4 to exit
1
What book would you like to search? Type in the title of the book.

java
Title: java
Author: hd
Genre: programming
ISBN: 129183
Quantity Left: 3
Hi Marry
Please select from the following options:
Press 1 to search a book
Press 2 to checkout a book
press 3 to return a book
press 4 to exit