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

PLEASE ANSWER THIS AS SOON AS POSSIBLE I NEED IT TODAY! PLEASE READ THE INSTRUCT

ID: 3833309 • Letter: P

Question

PLEASE ANSWER THIS AS SOON AS POSSIBLE I NEED IT TODAY!

PLEASE READ THE INSTRUCTION BEFORE STARTING

THERE IS A SHELL AT THE BOTTOM THAT MUST BE FOLLOWED

Part 1

In this lab we will be creating a class called LibraryBook encapsulating the concept of a book in a class. This class will have the instance variables such as title, author, id, dateBorrowed and dateReturned.

This class also has a list of the methods such as constructor, accessor, mutator, toString and equals method. Refer to the provided shell for the description of each of the methods.

Part 2:

Create a class called LibraryDriver that is the client of the class LibraryBook that we just created. This program is going to create an array of LibraryBook. This array will be filled by reading from a file which contains the information for each book.

You must create a notepad file that contains the information for all the book. Information needed are:

                Title

               Author

               Id

Therefore, the information for each book will take 3 lines and that is how you need to create the file. After the array is populated, we will display the list of the books at the library. User will choose to check in or check out a book. A book is being checked out, the date instance variable must be updated.

If the user chooses a book that is not available, then a message must be outputted on the screen.

If a book is returned then that book must become available again.

The user will be asked if he/she has any other transaction.

Refer to the provided shell for more details.

Your program must generate the exact same output.   

Sample output

-----------WECOME TO THE LIBRARY AT CSUS--------------

Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 1

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Choose one of the available books,

Enter the number associated with the book: 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%

You have checked out:

12356789, The Harry Potter and the Sorcerer's Stone, J. K. Rowling

You must return the book by: Sat Apr 29 10:11:00 PDT 2017

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 2

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Not Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Enter the id of the book: 12356789

Your book has been returned to the library. Thanks

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 1

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Choose one of the available books,

Enter the number associated with the book: 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%

You have checked out:

12356789, The Harry Potter and the Sorcerer's Stone, J. K. Rowling

You must return the book by: Sat Apr 29 10:11:17 PDT 2017

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 1

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Not Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Choose one of the available books,

Enter the number associated with the book: 1

The selected book is not available until: Sat Apr 29 10:11:17 PDT 2017

Choose another book: 2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%

You have checked out:

34567890, The Harry Potter and the Chamber of Secrets, J. K. Rowling

You must return the book by: Sat Apr 29 10:11:28 PDT 2017

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 1

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Not Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Not Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Choose one of the available books,

Enter the number associated with the book: 1

The selected book is not available until: Sat Apr 29 10:11:17 PDT 2017

Choose another book: 5

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%

You have checked out:

98674532, The Pride and Prejudice, Jane Austen

You must return the book by: Sat Apr 29 10:11:37 PDT 2017

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 2

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Not Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Not Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Not Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Enter the id of the book: 6666666 you entered the wrong ID.

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 2

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Not Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Not Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Not Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Enter the id of the book: 12356789

Your book has been returned to the library. Thanks

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 2

----------------------------------------------------------

TEXT FILE:

----------------------------------------------------------

JAVA FILE 1:

-----------------------------------------------

JAVA FILE 2:

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Not Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Not Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Not Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Enter the id of the book: 6666666 you entered the wrong ID.

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 2

          LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO

----------------------------------------------------------------------------------

      ID                  Title                             Author

----------------------------------------------------------------------------------

1.12356789, Harry Potter and the Sorcerer's Stone, J. K. Rowling, Not Available

2.34567890, Harry Potter and the Chamber of Secrets, J. K. Rowling, Not Available

3.56789435, The Hobbit, J. R. R. Tolkien, Available

4.67894355, The Da Vinci Code, Dan Brown, Available

5.98674532, Pride and Prejudice, Jane Austen, Not Available

6.34245346, To Kill a Mockingbird, Harper Lee, Available

7.89564756, The Catcher in the Rye, J. D. Salinger, Available

----------------------------------------------------------------------------------

Enter the id of the book: 12356789

Your book has been returned to the library. Thanks

Another transaction y/n? y Enter 1 to check out a book.

Enter 2 to return a book.

Enter your choice--> 2

Explanation / Answer

Here is the code for LibraryBook.java:

import java.util.*;
public class LibraryBook
{
//declare all the instance variables such as title, author, borrower, id
private String title;
private String author;
private String borrower;
private String ID;
private Date dateBorrowed ;
private Date dateReturned;
//initilalize the instance variables using the paramter being passed to this method
public LibraryBook(String t, String a, String i)
{
        title = t;
        author = a;
        ID = i;
        borrower = null;
        dateBorrowed = null;
        dateReturned = null;
        //System.out.println("Book created.");
}
//initialize the instance variable borrower to the new value
public void borrowBook(String b)
{
    borrower = b;
}
public void setDateBorrowed()
{
Calendar calendar = Calendar.getInstance();
dateBorrowed = calendar.getTime();   
}
public void setDateReturned( )
{
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 15);
dateReturned = calendar.getTime();
}
//this method stes both dateBorrowed and dateReturned to null which indicates that the book is avaiable
public void setDateNull()
{
        dateBorrowed = dateReturned = null;
}
//accessor method returns the id
public String getId()
{
return ID;
}
//retruns the date that the book was borrowed   
public Date getBorrowedDate()
{
return dateBorrowed;
}
//returns the returned date
public Date getReturnedDate()
{
return dateReturned;
}
//sets the borrower to null when the book is returned   
public void returnBook()
{
borrower = null;
}
//returns the title of the book
public String getTitle()
{
return title;
}
//returns the author of the book
public String getAuthor()
{
return author;
}
public String getBorrower()
{
return borrower;
}
//compares two books, if they have the same id return true, otherwise return false
public boolean equals(LibraryBook b)
{
return ID == b.ID;
}
//returns a string representing a book, including the id, titile and author
public String toString()
{
return ID + ", " + title + ", " + author;
}
}

And the code for LibraryDriver.java is:

import java.util.*;
import java.io.*;
public class LibraryDriver
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner kb = new Scanner(System.in);
LibraryBook[] books = null;
books = fillArray(books);
boolean repeat = true;
while (repeat)
{
  
//display the options to check in or check out a book;
System.out.println("-----------WECOME TO THE LIBRARY AT CSUS--------------");
System.out.println("Enter 1 to check out a book.");
System.out.println("Enter 2 to return a book.");
System.out.print("Enter your choice--> ");
//get the user's choice
int choice = kb.nextInt();
//if choice is 1
if(choice == 1)
{
    //display the books
    displayBooks(books);
    //call the method getChoice
    getChoice(books, kb);
}
//else
else
{
    //display the books
    displayBooks(books);
    //call the method check in
    checkIn(books, kb);
}
//ask the user if there is another transaction
System.out.print("Another transaction y/n? ");
char again = kb.next().charAt(0);
if(again == 'Y' || again == 'y')
    repeat = true;
else
    repeat = false;  
}   
}
public static void checkIn(LibraryBook[] books, Scanner kb)
{
kb.nextLine();
System.out.print("Enter the id of the book : ");
boolean found = false;
String id = kb.next();
int i = 0;
//as long as i is less than the length of the array book and found is false
for(i = 0; i < books.length && !found; i++)
{
//get the id of the book at the index i
String ID = books[i].getId();
//if the borrowed date and returned date is not null
if(books[i].getBorrowedDate() != null && books[i].getReturnedDate() != null)
{
//call the method setDateNull from the LibraryBook class to set the dates to null
books[i].setDateNull();
//set found to true
found = true;
}
//else if you find the book in the list and the returned date is not null
else if(found && books[i].getReturnedDate() != null)
{
//output invlaid id
System.out.println(ID);
//break out of the loop
break;
}
//update the index i   
  
}
//if the book was not found
if(!found)
   //dispaly that the id for the book is invalid
   System.out.println("you entered the wrong ID.");
  
}
//this method reads the info for the books from a file and stores it in the array
public static LibraryBook[] fillArray(LibraryBook[] books) throws FileNotFoundException
{
    books = new LibraryBook[7];
Scanner kb = new Scanner(new File("LibraryBooks.txt"));
int count = 0;
while(kb.hasNextLine())
{
    String temp1 = kb.nextLine();
    String temp2 = kb.nextLine();
    String temp3 = kb.nextLine();
    //System.out.println(temp1 + temp2 + temp3);
    //System.out.println(count + " " + books.length);
    books[count] = new LibraryBook(temp1, temp2, temp3);
    //System.out.println(books[count]);
    count++;
}
return books;
}
//this method displays the info about the books and their avaiability
public static void displayBooks(LibraryBook[] books)
{
    System.out.println("LIBRARY OF CALIFORNIA STATE UNIVERSITY< SACRAMENTO");
    System.out.println("----------------------------------------------------------------------------------");
    System.out.println(" ID Title Author");
    System.out.println("----------------------------------------------------------------------------------");
    for(int i = 0; i < books.length; i++)
    {
       System.out.print((i+1) + ". ");
       System.out.print(books[i]);
       if(books[i].getReturnedDate() == null)
           System.out.println(" " + "Available");
       else
           System.out.println(" " + "Not Available");  
    }  
       
}
//this method allows the user to choose a book from the list. as long as the selected book is checked out
//the user will be prompted for a valid book
public static void getChoice(LibraryBook[] books, Scanner kb)
{
System.out.println(" Choose one of the avaible books,");
System.out.print("Enter the number assosiated with the book: ");
int num = kb.nextInt();
//as long as the book is not avaiable*/)
while(true)
{
int i;
for(i = 0; i < books.length; i++)
    if(books[i].equals(books[num-1].getId()))
        break;
       
if(i < books.length && books[i].getReturnedDate() != null)      
    {
       System.out.println("The selected book is not available until: " + books[i].getReturnedDate());
       System.out.print("Choose another book: ");
       num = kb.nextInt();
    }
    else
        break;   
       
//display the due date for the selected book
//ask the user to enter a new book number

}
  
//call the method setdateBorrowed and setDateReturned
books[num-1].setDateBorrowed();
books[num-1].setDateReturned();
//display the info the book that the user is boroowing
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
System.out.println("You have checked out:");
System.out.println(books[num-1]);
System.out.println("You must return the book by: " + books[num-1].getReturnedDate());
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
// display the return date for the book
}
}   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote