Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use a lambda expression to reduce code duplication when searching a book catalog for certain types of books 1- Import the code for this application

Use a lambda expression to reduce code duplication when searching a book catalog for certain types of books

1- Import the code for this application and run it to make sure it works Then, review the books filtered by title, category, and format correctly.

here is the code for the bookCatalog

package murach.bookcatalog;

import java.util.ArrayList; import java.util.List;

public class BookCatalog { private List bookCatalog; public BookCatalog() { bookCatalog = new ArrayList<>(); bookCatalog.add(new Book("PHP and MySQL", Book.WEB, Book.ELECTRONIC)); bookCatalog.add(new Book("MySQL", Book.DATABASE, Book.PAPERBACK)); bookCatalog.add(new Book("MySQL", Book.DATABASE, Book.ELECTRONIC)); bookCatalog.add(new Book("Dreamweaver", Book.WEB, Book.ELECTRONIC)); bookCatalog.add(new Book("Java Servlets and JSP", Book.JAVA, Book.PAPERBACK)); bookCatalog.add(new Book("Java Servlets and JSP", Book.JAVA, Book.ELECTRONIC)); bookCatalog.add(new Book("C# 2012", Book.DOTNET, Book.ELECTRONIC)); bookCatalog.add(new Book("Visual Basic 2012", Book.DOTNET, Book.ELECTRONIC)); bookCatalog.add(new Book("SQL Server 2012", Book.DATABASE, Book.ELECTRONIC)); bookCatalog.add(new Book("Oracle SQL and PL/SQL", Book.DATABASE, Book.ELECTRONIC)); bookCatalog.add(new Book("HTML5 and CSS3", Book.WEB, Book.ELECTRONIC)); bookCatalog.add(new Book("JavaScript and jQuery", Book.WEB, Book.ELECTRONIC)); bookCatalog.add(new Book("Java Programming", Book.JAVA, Book.PAPERBACK)); bookCatalog.add(new Book("Java Programming", Book.JAVA, Book.ELECTRONIC)); bookCatalog.add(new Book("Android Programming", Book.MOBILE, Book.PAPERBACK)); bookCatalog.add(new Book("Android Programming", Book.MOBILE, Book.ELECTRONIC)); bookCatalog.add(new Book("JavaScript and DOM Scripting", Book.WEB, Book.ELECTRONIC)); } public List getCatalog() { return bookCatalog; } }

It should look like this

BOOKS BY TITLE:

[Java Programming, Java, Paperback]

[Java Programming, Java, Electronic]

BOOKS BY CATEGORY:

[Java Servlets and JSP, Java, Paperback]

[Java Servlets and JSP, Java, Electronic]

[Java Programming, Java, Paperback]

[Java Programming, Java, Electronic]

BOOKS BY FORMAT:

[MySQL, Database, Paperback]

[Java Servlets and JSP, Java, Paperback]

[Java Programming, Java, Paperback]

[Android Programming, Mobile, Paperback]

2- open the BookManager class and examine these three methods: getBooksBy Title, getBooksByTech, and Note the code for these methods is almost identical, except for the condition at the beginning of the if statement

Bookmanger codes

package murach.bookcatalog;

import java.util.ArrayList; import java.util.List;

public class BookManager { private List bookList; public BookManager() { bookList = new BookCatalog().getCatalog(); } public List getBooksByTitle(String title) { List books = new ArrayList<>(); for (Book b : bookList) { if (b.getTitle().equals(title)) { books.add(b); } } return books; } public List getBooksByCategory(String category) { List books = new ArrayList<>(); for (Book b : bookList) { if (b.getCategory().equals(category)) { books.add(b); } } return books; } public List getBooksByFormat(String format) { List books = new ArrayList<>(); for (Book b : bookList) { if (b.getFormat().equals(format)) { books.add(b); } } return books; } }

3. Add a functional interface named TestBook that includes a method named test that accepts a Book object as a parameter and returns a boolean value.

4. In the BookManager class, add a single method named getBooksByLambda that accepts a TestBook object as a parameter and uses its test method in the condition at the beginning of the if statement

5. open the Main class and replace the old calls to the methods of the etBooksBy method class three calls to the new To do that, you'll need to pass a lambda expression as an argument to thi method. For example, here's the lambda expression to test the title of a bock b b.getTitle.equals("Java Programming"

mainclass codes

package murach.bookcatalog;

import java.util.List;

public class Main {

public static void main(String[] args) { BookManager manager = new BookManager(); List booksByTitle = manager.getBooksByTitle("Java Programming"); System.out.println(" BOOKS BY TITLE:"); printList(booksByTitle); List booksByCategory = manager.getBooksByCategory(Book.JAVA); System.out.println(" BOOKS BY CATEGORY:"); printList(booksByCategory); List booksByFormat = manager.getBooksByFormat(Book.PAPERBACK); System.out.println(" BOOKS BY FORMAT:"); printList(booksByFormat); } public static void printList(List bookList) { for (Book b : bookList) { System.out.println(b); } } }

6. In the BookManager class, delete the getBooksByTitle getBooksByTech and and getBooksByFormat methods, Note how this reduces code duplication increases the flexibility of your code

7. Run the application and test it to MAke sure it works correctly. It should print the same list to the console as in step one

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

Step: 3

blur-text-image

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

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

4. Make recommendations on steps to take to melt the glass ceiling.

Answered: 1 week ago

Question

1. Discuss the potential legal issues that relate to training.

Answered: 1 week ago