Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: Implent the following 3 methods in the bookstore class: a method searching the library of Books for a given author and returning an ArrayList

Java: Implent the following 3 methods in the bookstore class:

a method searching the library of Books for a given author and returning an ArrayList of such Books.

a method returning an ArrayList of Books whose price is less than a given number.

a method returning the Book object with the lowest price in the library.

########################

import java.util.ArrayList;

public class BookStore { private ArrayList library; private final int NOT_FOUND = -1; /** default constructor * instantiates ArrayList of Books */ public BookStore( ) { this.library = new ArrayList<>( ); this.library.add( new Book( "Intro to Java", "James", 56.99 ) ); this.library.add( new Book( "Advanced Java", "Green", 65.99 ) ); this.library.add( new Book( "Java Servlets", "Brown", 75.99 ) ); this.library.add( new Book( "Intro to HTML", "James", 29.49 ) ); this.library.add( new Book( "Intro to Perl", "James", 34.99 ) ); this.library.add( new Book( "Advanced HTML", "Greenberg", 56.99 ) ); this.library.trimToSize( ); } /** toString * @return each book in library, one per line */ public String toString( ) { String result = ""; for( Book tempBook : this.library ) { result += tempBook.toString( ) + " "; } return result; } /** Generates list of books containing searchString * @param searchString the keyword to search for * @return the ArrayList of books containing the keyword */ public ArrayList searchForTitle( String searchString ) { ArrayList searchResult = new ArrayList<>( ); for ( Book currentBook : this.library ) { if ( ( currentBook.getTitle( ).toLowerCase() ).indexOf( searchString ) != NOT_FOUND ) searchResult.add( currentBook ); } searchResult.trimToSize( ); return searchResult; } }

###############################

import java.util.*;

public class BookSearchEngine { public static void main( String [] args ) { BookStore bookStore = new BookStore( ); System.out.println( " Our book collection is:" ); System.out.println( bookStore ); Scanner keyboard = new Scanner(System.in); System.out.println( " Searching titles, enter a keyword" ); String keyword = keyboard.next(); ArrayList result = bookStore.searchForTitle( keyword.toLowerCase() ); System.out.println( "The search results for \"" + keyword + "\" are:" ); if (result.size() > 0) { for( Book tempBook : result ) System.out.println( tempBook ); } else { System.out.println("No book meeting your search criteria has been found"); } } }

########################################

public class Book { private String title; private String author; private double price; /** default constructor */ public Book( ) { this.title = ""; this.author = ""; this.price = 0.0; } /** overloaded constructor * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book( String newTitle, String newAuthor, double newPrice ) { this.title = newTitle; this.author = newAuthor; this.price = newPrice; } /** getTitle accessor method * @return the title */ public String getTitle( ) { return this.title; } /** getAuthor accessor method * @return the author */ public String getAuthor( ) { return this.author; } /** getPrice accessor method * @return the price */ public double getPrice( ) { return this.price; } /** toString * @return title, author, and price */ public String toString( ) { return ( "title: " + this.title + "\t" + "author: " + this.author + "\t" + "price: " + this.price ); } }

###################################

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions