In: Computer Science
HINT: Initialize the callNumber field to an empty String in #2 constructor
“<title> : <author> (<callNumber>)”
HINT: Complete with ONLY 1 line of code using String concatenation
public void setBookDetails(String author, String title, String callNumber)
NOTE: Use method call getBookDetails where the details of a book are needed (i.e. when listing/printing books or supplying information about a particular book)
public Genre(String genreName)
HINT: Make sure you instantiate a new books ArrayList object
public boolean bookIndexValid(int index)
HINT: First make sure books collection is NOT null or empty
HINT: Make sure you create a new Book object in #2
public int findGenreBookWithCallNumber(String callNumber)
public void removeGenreBookWithCallNumber(String callNumber)
HINT: MUST NOT use any type of loops at all
public void removeAllGenreBooksByAuthor(String author)
HINT: MUST use .remove method from Iterator to ensure proper removals
(Only after ENTIRE search loop is completed - thus, outside of loop) Check if NO match found, then print ”NO books by author: <author>”
HINT: Include instantiation of a new genres ArrayList object AND initialize bookOfTheWeek to either null or use (optional) pickBookOfTheWeek( )
HINT: MUST use for-each loop and call to getNumberOfGenreBooks( )
HINT: MUST create and use a new Genre anonymous object in #2.
HINT: MUST use Iterator to help get the genre to remove it and also include any formatted output detailing the removal or an error message (if not found)
HINT: Use for-each and print formatted genre info with leading spaces “ ”
HINT: First check using getNumberOfTotalBooks that the library has books and print error message if there are NO books in the entire library
HINT: MUST use Iterator and listAllGenreBooks to help print the books for each genre (Yes, I know it’s possible w/o an Iterator, BUT you MUST use it !!!)
Add method void printBookOfTheWeek( ) to print out the details of the bookOfTheWeek or an error stating that ”There is NO Book of the Week”HINT: MUST have a heading and use getBookDetails( ) to print the details
HINT: First check that Library has books to chose from, then use Random to pick a random Genre. Then, MUST check the chosen Genre has books before picking a random Book from the randomly selected genre. If there are NO books in the selected genre, then repeat the loop by trying another genre. Remember to call getGenreBooks and printBookOfTheWeek (as needed)
Solution:
Book.java:
public class Book {
private String author;
private String title;
private String callNumber;
public Book(String author, String title) {
setBookDetails(author, title,
"");
}
public Book(String author, String title, String
callNumber) {
setBookDetails(author, title,
callNumber);
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getCallNumber() {
return callNumber;
}
public String getBookDetails() {
return this.getTitle() + ": " +
this.getAuthor() + " (" + this.getCallNumber() + ")";
}
public void setBookDetails(String author, String
title, String callNumber) {
this.author = author;
this.title = title;
this.callNumber = callNumber;
}
}
Genre.java
import java.util.ArrayList;
import java.util.Iterator;
public class Genre {
private String genreName;
private ArrayList<Book> books;
public Genre(String genreName) {
this.genreName = genreName;
books = new
ArrayList<Book>();
}
public String getGenreName() {
return genreName;
}
public ArrayList<Book> getGenreBooks()
{
return books;
}
public int getNumberOfGenreBooks() {
return books.size();
}
public void addGenreBook(Book book) {
books.add(book);
}
public void addGenreBook(String author, String
title, String callNumber) {
books.add(new Book(author, title,
callNumber));
}
public boolean bookIndexValid(int index) {
if (books != null) {
return (index
< books.size() && index >= 0);
} else {
return
false;
}
}
public void removeGenreBookWithCallNumber(String
callNumber) {
int index =
findGenreBookWithCallNumber(callNumber);
if (bookIndexValid(index)) {
System.out.println("Removing: " +
books.get(index).getBookDetails());
books.remove(index);
} else {
System.out.println("NO book with call number: " +
callNumber);
}
}
//
public int findGenreBookWithCallNumber(String
callNumber) {
int index = 0;
boolean searching = false;
while (index < books.size())
{
if
(books.get(index).getCallNumber().equals(callNumber)) {
searching = true;
break;
}
index++;
}
if (searching) {
return
index;
} else {
return -1;
}
}
public void removeAllGenreBooksByAuthor(String
author) {
Iterator<Book> it =
books.iterator();
while (it.hasNext()) {
Book book =
it.next();
if
(book.getAuthor().equalsIgnoreCase(author)) {
System.out.println(book.getBookDetails());
it.remove();
}
}
}
public void listAllGenreBooks() {
if (books.isEmpty()) {
System.out.println("NO Books to print");
} else {
System.out.println("BOOKS:");
for (Book book :
books) {
System.out.println(book.getBookDetails());
}
}
}
public void listGenreBooksByAuthor(String author)
{
boolean found = false;
for (Book book : books) {
System.out.println("BOOKS BY AUTHOR " + author);
if
(book.getAuthor().equalsIgnoreCase(author)) {
System.out.println(book.getBookDetails());
found = true;
}
}
if (!found) {
System.out.println("NO books by author: " + author);
}
}
}
Library.java:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class Library {
private Book bookOfTheWeek;
private ArrayList<Genre> genres;
public Library() {
bookOfTheWeek = null;
genres = new
ArrayList<Genre>();
}
public int getNumberOfTotalBooks() {
int numberOfTotalBooks = 0;
for (Genre genre : genres)
numberOfTotalBooks += genre.getNumberOfGenreBooks();
return numberOfTotalBooks;
}
public void addGenre(Genre genre) {
genres.add(genre);
}
public void addGenre(String genreName) {
genres.add(new
Genre(genreName));
}
public void removeGenre(String genreName) {
boolean found = false;
Iterator<Genre> currentGenre
= genres.iterator();
while (currentGenre.hasNext())
{
Genre genre =
currentGenre.next();
if
(genre.getGenreName().equalsIgnoreCase(genreName)) {
System.out.println("Removing: " +
genre.getGenreName());
found = true;
currentGenre.remove();
break;
}
}
if (!found) {
System.out.println("Genre Not found: " + genreName);
}
}
public void listAllGenres() {
if (genres.size() == 0) {
System.out.println("NO Genres");
} else {
System.out.print("THE GENRE NAMES: ");
for (Genre genre
: genres) {
System.out.print(genre.getGenreName() + "
");
}
}
}
public void listAllLibraryBooks() {
if (getNumberOfTotalBooks() == 0)
{
System.out.println("THERE IS NO BOOKS IN THE LIBRARY");
} else {
Iterator<Genre> curremtGenre = genres.iterator();
while
(curremtGenre.hasNext()) {
Genre genre = curremtGenre.next();
genre.listAllGenreBooks();
}
}
}
public void printBookOfTheWeek() {
if (this.bookOfTheWeek == null)
{
System.out.println("There is NO Book of the Week");
} else {
System.out.println("BOOK OF THE WEEK: " +
this.bookOfTheWeek.getBookDetails());
}
}
public void pickBookOfTheWeek() {
Random random = new Random();
for (;;) {
int genreIndex =
(genres.size() - 1) - 0;
int genre =
random.nextInt(genreIndex + 1) + 0;
Genre
randomGenre = genres.get(genre);
if
(randomGenre.getNumberOfGenreBooks() == 0) {
continue;
} else {
ArrayList<Book> booksList =
randomGenre.getGenreBooks();
int index = ((booksList.size() - 1) - 0);
int bookIndex = random.nextInt(index + 1) +
0;
bookOfTheWeek = booksList.get(bookIndex);
}
}
}
}