You would modify the BookStore class according to the following instructions: Ma
ID: 3675293 • Letter: Y
Question
You would modify the BookStore class according to the following instructions:
Make sure to include the Book class, BookStore and BookSearchEngine class in your NetBeans project folder.
Modify the BookStore constructor so, when a BookStore object is created it is empty.
Implement a addBook( ) method that takes a Book object as a parameter and adds the book to the library.
Implement another version of addBook( ) method that takes, the title of the book, author and price as parameters and using the values in the parameters it creates a Book object and finally adds the book to the library.
Next modify the BookSearchEngine class that has a main( ) method as per the below instructions:
Create a BookStore object
Add any five books related to java or other programming languages to the BookStore.
Demonstrate use of both the versions of the addBook( ) methods.
Finally search for all the books that has “java” (treat the keyword as case-insensitive) and display the results of the search.
Using the following
1.
//* BookStore class
package Example09_13_15;
import java.util.ArrayList;
public class BookStore
{
private ArrayList<Book> library;
// default constructor instantiates ArrayList of Books
public BookStore( )
{
library = new ArrayList<Book>( );
library.add( new Book( "Intro to Java", "James", 56.99 ) );
library.add( new Book( "Advanced Java", "Green", 65.99 ) );
library.add( new Book( "Java Servlets", "Brown", 75.99 ) );
library.add( new Book( "Intro to HTML", "James", 29.49 ) );
library.add( new Book( "Intro to Flash", "James", 34.99 ) );
library.add( new Book( "Advanced HTML", "Green", 56.99 ) );
library.trimToSize( );
}
//** toString
public String toString( )
{
String result = "";
for( Book tempBook : library )
{
result += tempBook.toString( ) + " ";
}
return result;
}
//** Generates list of books containing searchString
public ArrayList<Book> searchForTitle( String searchString )
{
ArrayList<Book> searchResult = new ArrayList<Book>( );
for ( Book currentBook : library )
{
if ( ( currentBook.getTitle( ) ).indexOf( searchString ) != -1 )
searchResult.add( currentBook );
}
searchResult.trimToSize( );
return searchResult;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.
package Example09_13_15;
/* BookSearchEngine class
Anderson, Franceschi
*/
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class BookSearchEngine
{
public static void main( String [] args )
{
BookStore bs = new BookStore( );
String keyword = JOptionPane.showInputDialog( null,
"Enter a keyword" );
System.out.println( "Our book collection is:" );
System.out.println( bs.toString( ) );
ArrayList<Book> results = bs.searchForTitle( keyword );
System.out.println( "The search results for " + keyword
+ " are:" );
for( Book tempBook : results )
System.out.println( tempBook.toString( ) );
}
}
Explanation / Answer
1.
I have added required functions.
//* BookStore class
package Example09_13_15;
import java.util.ArrayList;
public class BookStore
{
private ArrayList<Book> library;
// default constructor instantiates ArrayList of Books
public BookStore( ) {
library = new ArrayList<Book>( );
}
//addBook, takes a Book object and add to list
public void addBook(Book book){
library.add(book);
}
//addBook, takes a Book object and add to list
public void addBook(String title, String author, double price){
library.add(new Book(title, author, price));
}
//** toString
public String toString( )
{
String result = "";
for( Book tempBook : library )
{
result += tempBook.toString( ) + " ";
}
return result;
}
//** Generates list of books containing searchString
public ArrayList<Book> searchForTitle( String searchString )
{
ArrayList<Book> searchResult = new ArrayList<Book>( );
for ( Book currentBook : library )
{
if ( ( currentBook.getTitle( ) ).indexOf( searchString ) != -1 )
searchResult.add( currentBook );
}
searchResult.trimToSize( );
return searchResult;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
package Example09_13_15;
/* BookSearchEngine class
Anderson, Franceschi
*/
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class BookSearchEngine
{
public static void main( String [] args )
{
BookStore bs = new BookStore( );
Book b1 = new Book( "Intro to Java", "James", 56.99 );
Book b2 = new Book( "Java Servlets", "Brown", 75.99 );
Book b3 = new Book( "Intro to HTML", "James", 29.49 );
// adding book to book store
bs.addBook(b1);
bs.addBook(b2);
bs.addBook(b3);
bs.addBook("Advanced Java", "Green", 65.99 );
bs.addBook("Advanced HTML", "Green", 56.99);
bs.addBook("Intro to Flash", "James", 34.99);
String keyword = JOptionPane.showInputDialog( null,
"Enter a keyword" );
System.out.println( "Our book collection is:" );
System.out.println( bs.toString( ) );
ArrayList<Book> results = bs.searchForTitle( keyword );
System.out.println( "The search results for " + keyword
+ " are:" );
for( Book tempBook : results )
System.out.println( tempBook.toString( ) );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.