Write a Java record called Book and a user-defined class called Library. Library will be an inner class of Project6_lastname.java
The data file is located here.
Learning outcomes
- Use a
record. - Build a user-defined class.
- Create overloaded methods.
- Read data from a file to create a mini database.
- Build general search functionality.
- Build code to test all code paths.
The Book record will be:
private record Book(String title, String author, String genre
, String subgenre, String publisher, int bookNumber)
For the record, you will override the toString() method to return a string in the form:
Title: Short History of the World, A Author: Wells, H G Genre: nonfiction Subgenre: history Publisher: UNKNOWN Book Number: 131
The Library class will consist of the following instance variables:
Book[] books;
int size, length;
And one constructor:
private Library(int size)
You will write the code for the following methods of the Library class:
private void addBook(Book book)
private int getBookCount()
private Book getBook(int index)
private Book[] findBookByCategory(String value, int cat)
Set up a menu of operations:
1. Search by Title. 2. Search by Author. 3. Search by Genre. 4. Search by Subgenre. 5. Search by Publisher. 6. Exit Please choose an option:
When you run the program, the
Our library contains 211 books. 1. Search by Title. 2. Search by Author. 3. Search by Genre. 4. Search by Subgenre. 5. Search by Publisher. 6. Exit Please choose an option: 2 Please enter the search string for Author: doyle Search results: Title: Complete Sherlock Holmes, The - Vol I Author: Doyle, Arthur Conan Genre: fiction Subgenre: classic Publisher: Random House Book Number: 29 Title: Complete Sherlock Holmes, The - Vol II Author: Doyle, Arthur Conan Genre: fiction Subgenre: classic Publisher: Random House Book Number: 30 1. Search by Title. 2. Search by Author. 3. Search by Genre. 4. Search by Subgenre. 5. Search by Publisher. 6. Exit Please choose an option: 1 Please enter the search string for Title: textile No search results found. 1. Search by Title. 2. Search by Author. 3. Search by Genre. 4. Search by Subgenre. 5. Search by Publisher. 6. Exit Please choose an option: 1 Please enter the search string for Title: tale Search results: Title: Tales of Mystery and Imagination Author: Poe, Edgar Allen Genre: fiction Subgenre: classic Publisher: HarperCollins Book Number: 43 Title: Tales of Beedle the Bard Author: Rowling, J K Genre: fiction Subgenre: novel Publisher: UNKNOWN Book Number: 201 1. Search by Title. 2. Search by Author. 3. Search by Genre. 4. Search by Subgenre. 5. Search by Publisher. 6. Exit Please choose an option: 6
There is a lot going on with this program, but you can complete it with less than 130 lines of code if you set things up well. Work on improving your conditional tests and recognize that searches are not case-sensitive. There are many ways to make your code efficient.
If you’re not sure, reach out via email for guidance.
Submit your Java file to the Learning Management System for grading.