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

Need help with a Java program about Library, user can check in check out books e

ID: 3552596 • Letter: N

Question

Need help with a Java program about Library, user can check in check out books etc.


this is what we have so far:

The ImportBooks.java


import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

import java.util.NoSuchElementException;


/**

* @author A. Ramona Lopez

* This program imports data from a text file and stores it into different ArrayLists.

*/


public class ImportBooks

{

public static ArrayList<Integer> bookId = new ArrayList<Integer>();

public static ArrayList<String> category = new ArrayList<String>();

public static ArrayList<String> author = new ArrayList<String>();

public static ArrayList<String> title = new ArrayList<String>();

public static ArrayList<String> isbn = new ArrayList<String>();

public static ArrayList<String> year = new ArrayList<String>();

public static ArrayList<String> publisher = new ArrayList<String>();

public static ArrayList<Boolean> status = new ArrayList<Boolean>(); //Checked out: true or false

//public static void main(String[] args)

public void locateFileB()

{

try

{

String filename = "BookData.txt";

readFileB(filename);

}

catch (FileNotFoundException exception)

{

System.out.println("File not found.");

}

catch (NoSuchElementException exception)

{

System.out.println("File contents invalid.");

}

catch (IOException exception)

{

exception.printStackTrace();

}

}

/**

* Reads the data in the text file and places it in the appropriate ArrayList

* @param filename

*/

private static void readFileB(String filename) throws IOException

{

File inFile = new File(filename);

Scanner in = new Scanner(inFile);

try

{

while (in.hasNextLine())

{

String line = in.nextLine();

String[] lineItem = line.split("/");

bookId.add(Integer.parseInt(lineItem[0]));

category.add(lineItem[1]);

author.add(lineItem[2]);

title.add(lineItem[3]);

isbn.add(lineItem[4]);

year.add(lineItem[5]);

publisher.add(lineItem[6]);

status.add(Boolean.parseBoolean(lineItem[7]));


}

}

finally

{

in.close();

}

}

/**

* This method gets the book Id for the indicated index number (i)

* @param i (index #)

* @return book Id (Integer)

*/

public static Integer getBookId(int i)

{

return bookId.get(i);

}

/**

* This method gets the category for the indicated index number (i)

* @param i (index #)

* @return category (String)

*/

public static String getCategory(int i)

{

return category.get(i);

}

/**

* This method gets the author for the indicated index number (i)

* @param i (index #)

* @return author (String)

*/

public static String getAuthor(int i)

{

return author.get(i);

}

/**

* This method gets the Title for the indicated index number (i)

* @param i (index #)

* @return Title (String)

*/

public static String getTitle(int i)

{

return title.get(i);

}

/**

* This method gets the ISBN# for the indicated index number (i)

* @param i (index #)

* @return ISBN (String)

*/

public static String getIsbn(int i)

{

return isbn.get(i);

}

/**

* This method gets the Year for the indicated index number (i)

* @param i (index #)

* @return year (String)

*/

public static String getYear(int i)

{

return year.get(i);

}

/**

* This method gets the publisher for the indicated index number (i)

* @param i (index #)

* @return publisher (String)

*/

public static String getPublisher(int i)

{

return publisher.get(i);

}

/**

* This method gets the status for the indicated index number (i)

* @param i (index #)

* @return status (boolean)

*/

public static Boolean getStatus(int i)

{

return status.get(i);

}

}

Explanation / Answer

package library;


import java.util.Scanner;


// TODO: Auto-generated Javadoc

/**

* The Class Book.

*/

public class Book {

String title;

String author;

String publisher;

String publicationYear;

String status;

String borrower;

String borrowDate;

String returnDate;

Scanner userInput = new Scanner(System.in);

/**

* Display the book:

* @return BookInfo

*/

final String displayBook(){

String BookInfo = "----------------------------"+

" Title:.................."+title+

" Author:................."+author+

" Publisher:.............."+publisher+

" PublicationYear:........"+publicationYear+

" Status:................."+status+

" Borrower:..............."+borrower+

" Date Borrowed:.........."+borrowDate+

" Return date:............"+returnDate+

" ----------------------------";

return BookInfo;

}

/**

* Creates the book.

*/

final void createBook(){

title = Console.readString(Messages.enterTitleMessage, Messages.tooShortMessage, 3);

author = Console.readString(Messages.enterAuthorMessage, Messages.tooShortMessage, 3);

publisher = Console.readString(Messages.enterPublisherMessage, Messages.tooShortMessage, 3);

publicationYear = Console.readString(Messages.enterPublicationYearMessage, Messages.tooShortMessage, 3);

borrower = "nobody";

borrowDate = "none";

returnDate = "none";

status = "Available";

}

/***

* Runs the main part of the program and terminates if the choice is 0.

*/

public void run(){

User.addUser();

Menu.displayMenu();//Displays the main menu and ask for choice.

exit:

while(Menu.menuChoice != 0){

// Choice 1: Display all the books:

if(Menu.menuChoice == 1 && Library.bookList.size() > 0){

Library.displayBookList();

Menu.displayMenu();

}

if(Menu.menuChoice == 1 && Library.bookList.isEmpty()){

System.out.println(Messages.invalidInputMessage);

Menu.displayMenu();

}

//Choice 2: Add a book:

if(Menu.menuChoice == 2){

//createBook();

Library.addBook();

Menu.displayMenu();

}

//Choice 3: Borrow a book:

if(Menu.menuChoice == 3 && Library.bookList.size() > 0){

Library.borrowBook();

}

if(Menu.menuChoice == 3 && Library.bookList.isEmpty()){

System.out.println(Messages.invalidInputMessage);

Menu.displayMenu();

}

//Choice 4: Return a book:

if(Menu.menuChoice == 4 && Library.bookList.size() > 0 && Library.borrowedBooks.size() > 0){

Library.returnBook();

Menu.displayMenu();

}

if(Menu.menuChoice == 4 && Library.bookList.isEmpty()){

System.out.println(Messages.invalidInputMessage);

Menu.displayMenu();

}

//Choice 5: Remove a book:

if(Menu.menuChoice == 5 && Library.bookList.size() > 0){

Library.removeBook();

}

if(Menu.menuChoice == 5 && Library.bookList.isEmpty()){

System.out.println(Messages.invalidInputMessage);

Menu.displayMenu();

}


//Choice 6: Empty the library:

if(Menu.menuChoice == 6 && Library.bookList.size() > 0){

Library.emptyLibrary();

Menu.displayMenu();

}

if(Menu.menuChoice == 6 && Library.bookList.isEmpty()){

System.out.println(Messages.invalidInputMessage);

Menu.displayMenu();

}


//Choice 7: Display main Menu:

if(Menu.menuChoice == 7){

if(Library.bookList.size() > 0){

Menu.displayMenu();

}else if(Library.bookList.isEmpty()){

Menu.displayMenu();

}

}

//Choice 0: Exit the program:

if(Menu.menuChoice == 0){

break exit;

}

}//end of while loop.

System.out.println(" ");

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

System.out.println(" ");

}//End of run() method.

}



package library;


import java.util.ArrayList;


// TODO: Auto-generated Javadoc

/**

* The Class Library.

*/

public class Library {

/** The book choice. */

public static int bookChoice;

static String returnBookTitle;

/** The status1. */

static String status1 = "Available";

/** The status2. */

static String status2 = "Borrowed";

/** The book list. */

static ArrayList<Book> bookList = new ArrayList<Book>();

static ArrayList<Book> borrowedBooks = new ArrayList<Book>();

/**

* Adds the book.

*/

static 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! ");

System.out.println("---------------------------------------------------------");

}


/**

* Display book list.

*/

static void displayBookList(){

if (bookList.isEmpty()){//If the library is empty, it goes back to main menu and choice.

System.out.println(">-------------------------------------------------------------");

System.out.println(Messages.empltyLibraryMessage + Messages.tryAgainMessage);

System.out.println(">-------------------------------------------------------------");

Menu.menuChoice = 7;

} else {

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

System.out.printf(" >-----------Book Index: [%s]--------------------------------- ",i);

System.out.println(bookList.get(i).displayBook());

System.out.println(">-------------------------------------------------------------");

}//End of For Loop.

}// End of Else Statement.

}//End of if Statement.

static void displayBorrowedBooks(){

if (bookList.isEmpty()){//If the library is empty, it goes back to main menu and choice.

System.out.println(">-------------------------------------------------------------");

System.out.println(Messages.empltyLibraryMessage + Messages.tryAgainMessage);

System.out.println(">-------------------------------------------------------------");

Menu.menuChoice = 7;

} else {

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

System.out.printf(" >-----------Book Index: [%s]--------------------------------- ",i);

System.out.println(borrowedBooks.get(i).displayBook());

System.out.println(">-------------------------------------------------------------");

}//End of For Loop.

}// End of Else Statement.

}//End of if Statement.

/**

* Borrow book.

*/

public static void borrowBook(){

System.out.println("---------------------------------------------------------");

System.out.println("> Here are all the books registered in the library: ");

System.out.println("---------------------------------------------------------");

displayBookList();

//register user's book choice.

bookChoice = (Console.readInteger(Messages.enterBookIndexMessage, Messages.bookIndexNotInListMessage, 0, Library.bookList.size()));

borrowLoop:

while(Menu.menuChoice == 3){

//Check if the book to be borrowed is available.

//bookChoice = (Console.readInteger(Messages.enterBookIndexMessage, Messages.bookIndexNotInListMessage, 1, Library.bookList.size()));


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";

bookList.get(bookChoice).borrower = User.userName;

bookList.get(bookChoice).borrowDate = "Today.";

bookList.get(bookChoice).returnDate = "In two weeks.";

//Add the borrowed book to the borrowedBooks arraylist:

borrowedBooks.add(bookList.get(bookChoice));

System.out.printf(" > You have chosen the following book: %s ", bookList.get(bookChoice).displayBook());

System.out.println(" > You have to return the book in two weeks! ");

break borrowLoop;

}else if(bookList.get(bookChoice).status.equalsIgnoreCase(status2) && bookList.size() >= bookChoice){

System.out.println(" <ERROR> The Book you are trying to borrow is unavailable! ");

break borrowLoop;

}else if(bookChoice > bookList.size()-1){

System.out.println(Messages.noSuchBookMessage);

break borrowLoop;

}

}

Menu.displayMenu();

}

/**

* Return book.

*/

static void returnBook(){

System.out.println("---------------------------------------------------------");

System.out.println("> Here are all the borrowed books: ");

System.out.println("---------------------------------------------------------");

displayBorrowedBooks();

returnBookTitle = Console.readString(Messages.enterTitleMessage, Messages.tooShortMessage, 3);

int x = 0;

boolean titleExistance = false;

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";

bookList.get(x).returnDate = "none";

int p = 0;

borrowLoop:

while (p < borrowedBooks.size()){//Search for the book by title, if it exists change it's status,

//it's borrower and borrowDate.


if (borrowedBooks.get(p).title.equalsIgnoreCase(returnBookTitle)){


borrowedBooks.remove(p);

break borrowLoop;

}

}

System.out.println(Messages.successReturnMessage);

titleExistance = true;

break;//if a title is found, break out of the loop and display choice menu.

}

x = x+1;

}//end of while loop.

if(titleExistance == false){

boolean repeatReturnBook = Console.readYesNo("<ERROR> The book with the title "+"""+returnBookTitle +"""+ "wasn't found!"

+" > Do you want to try again? Yes/No? >");

if(repeatReturnBook){

returnBook();

}else{

Menu.displayMenu();

}

}else if(titleExistance){

Menu.menuChoice = 7;

}

}

/**

* Removes the book.

*/

public static 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()){//show the user the list of all the books

displayBookList();

i = i+1;

}//end of while loop.

bookChoice = Console.readInteger(Messages.enterRemoveBookIndex ,Messages.bookIndexNotInListMessage + Messages.tryAgainMessage, 0, bookList.size());

int p = 0;

while (p < borrowedBooks.size()){//Search for the book by title, if it exists change it's status,

//it's borrower and borrowDate.


if (borrowedBooks.get(p).title.equalsIgnoreCase(returnBookTitle)){


borrowedBooks.remove(p);

}

}

bookList.remove(bookChoice);

System.out.print(Messages.successRemovedBookMessages);

Menu.menuChoice = 7;

}

/**

* Empty library.

*/

static void emptyLibrary(){

System.out.println(">>> WARNING <<< You have chosen to delete all books in the library! ");

boolean emptyLibraryChoice = Console.readYesNo("> Are you sure?? Enter yes or no: ");

if(emptyLibraryChoice){

Library.bookList.clear();

System.out.println(Messages.successEmptyLibraryMesssage);

Menu.menuChoice = 7;

}

}

}

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