Question
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
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
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started