Write a Java program to create the Book and Library Classes. Then create the tes
ID: 3682698 • Letter: W
Question
Write a Java program to create the Book and Library Classes. Then create the testMyLibrary class and do the following:
Create one instance of class Library,
Create4instancesofclassBook,
Create a file named “myFile” as a Global var.
Write a method named “write” to write the library name, library
address, book title, book author and book status (borrowed or
returned) into the file,
Write a method named “read” to read the contents of “myFile” file,
Theoutputmenushouldbeasbellow:
Note: Use “switch” for menu options (1-6) as shown above.
Explanation / Answer
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 UserList = new ArrayList(); static ArrayList BookList = new ArrayList(); 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 = "----------------------------"+ " Title:.................."+title+ " Author:................."+author+ " Publisher:.............."+publisher+ " PublicationYear:........"+publicationYear+ " Status:................."+status+ " Borrower:..............."+borrower+ " Date Borrowed:.........."+borrowDate+ " Return date:............"+returnDate+ " ----------------------------"; 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! "); 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! "); System.out.println(">-------------------------------------------------------------"); Book.displayFirstMenu();//Display to main menu. choice = choiceInput.nextInt();//Register new choice. } else { for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.