Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with this, I'm new to Java and I have tried to put this assignment together and would appreciate it if someone

Can someone help me with this, I'm new to Java and I have tried to put this assignment together and would appreciate it if someone could work out both tasks so I know if I'm on it

Will upload 2nd part too - and some of the code provided

image text in transcribed

ackage adminSite;

import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors;

import yourPrime.*;

public class FuncUtil { private Map userDb = new HashMap(); public FuncUtil(Map userDb) { this.userDb = userDb; } public Map getUserDb() { return userDb; } public void addSubscriber(Subscriber subscriber) { Supplier userId = () -> generateId().toString(); String id = userId.get(); subscriber.setUserID(id); userDb.putIfAbsent(id, subscriber); } public Integer generateId() { return new Random().nextInt(80000); } public boolean modifyPassword(String userId, String newPassword) { Subscriber subscriber = userDb.get(userId); subscriber.setPassword(newPassword); userDb.replace(userId, subscriber); if (userDb.get(userId).getPassword().equals(newPassword)) return true; else return false; } public boolean deleteSubscriber(String userId) { userDb.remove(userId); if (userDb.get(userId) == null) return true; else return false; } // TODO refactor the implementation of the searchSubscriber() method using Java 8 stream // and lambda expression. You can also use the existing interfaces if you prefer (but not required). // public List searchSubscriber(String keyword) { // TIP: // declare a list with type subscriber that will store the search result // you will need to return this list // use your subscriber map (check instance variable declared on top of this class) as stream, and // filter your data for matching keywords in user id or name (you can use predicate or lambda expression) // } // TODO refactor the implementation of the total sum of fees calculation using Java 8 streams // and lambda expression. You can also use the exisiting interfaces if you prefer (but not required). // public double calculateOverdueFees() { // TIP: // declare a variable type double that will store the result of total summation of fees, // use your subscriber map as stream, and include the necessary operations to get total sum using // the subscriber getFees() method (with lambda expersson or method reference // } // TODO refactor the implementation of the printAllSubscriber() method here with Java 8 stream and method // reference. You MUST use the function interface already defined here. The method will also print out the // details according to the sort by option - name of the subscriber, and the outstanding fees. // public void printAllSubscribers(String sortBy) { // TIP: // use the function interface as string that you print out // Function details = p -> p.getName() + " with outstanding amount = " + String.format("%.2f", p.getFees()); // you need a condition to check for the sortBy argument, use the subscriber map as stream source, and include the necessary // operations to sort (based on the argument type) and print out the string for each subscriber // } // TODO create a method to return the average outstanding fees from all accounts using Java 8 stream and lambda expression. // public double getAverageOutstanding() { // TIP: // declare a variable type double that will store the results of an average operation - you will return this value // use subscriber map as stream input and include necessary operations to calculate average // } // TODO create a method to return the outstanding fees from all accounts group by the media type. // You should make use of Java 8 Streams and lambda expression to do this - return map // public Map getTotalFeesGroupByMedia() { // TIP: // declare a map collection that will store the total fees you generate - check out the generic types // for your map as per the method signature. // // Check MyMedia class and explore the getters you can call for each media type. // For each subscriber, you need to calculate the total fees for each media type. As you traverse your // subscriber map, you will then call the list of each media, and calculate the total. // // For e.g., if you have 5 subscribers, you will have 4 list of Movie, Book and Song. So you need to // calculate the total fees for all movies, and then all books and lastly, all songs. // // *SECRET TIP: // With Java Stream, lambda expression and method reference, you can do this in 3 lines // (one for each media type). Use subscriber stream to get media, then invidividual list can be // processed as stream (all in a single pipeline) - but don't get hung up on it, use anything that you're // comfortable with ! -> the read tip :) // Map mapMedia = new HashMap(); } // TODO create a method to return the total number of items from all accounts using Java 8 streams and lambda expression. // group by the media type. Use the programming logic (idioms) that you've implemented in the getTotalFeesGroupByMedia() // method as a source of inspiration in completing this method. // public Map getCountGroupByMedia() { // TIP: // This will be exactly like the getTotalFeesByGroupMedia() method above. You just need to return the count // instead of the total sum. You will return three values (in a map) for each media type. The generic types // assigned to the map collection are a good clue to figure out the operators that you will use to construct // your stream pipeline/ // // *SECRET TIP: // Don't reinvent the wheel, use what you already created above - just replace the operator // Map mapMedia = new HashMap(); } }

The method implementations in FuncUtil (in the AdminSite package) are largely based on functional interface. However, these interfaces can be better implemented using lambda expression or / and Java 8 streams. In doing so, you will be able to simplify your codes, and at the same time improve the clarity and readability aspects. We will revisit the YourPrime program for this programming assignment, so, you should be familiar with the skeleton codes. The core of the program (yourPrime package) is pretty much unchanged, however, there are a few minor additions made to facilitate the assignment. For this tasks, you are required to refactor the following methods: 1 - searchSubcriber (String keyword) : use Java 8 Streams, and lambda expression to create the pipeline to search for a keyword in the user ID and name attributes of Subscriber. You should collect your stream as a List> and then return the list =[1 Mark ] 2 - calculateoverdueFees () : use Java 8 Streams, and lambda expression to return the total sum of the fees for all subscribers = [1 Mark]. 3 - printallsubscribers (String sortBy) : use Java 8 Streams, and method reference to print out a sorted list according to the argument sortBy. You must use the Function interface provided in the method to map your stream before printing your output. You can either sort the list by the subscriber's name, or their outstanding fees = [2 Marks]. 4 - getAverageoutstanding () : use Java 8 Streams and method reference to generate the average outstanding value, and return the amount =[1 Mark ]. Note: Use the comments available in the method body to help you in completing this task. It's important to not overthink the solution, you are basically writing a Java 8 version of function interface implementation, and it should be simple. Having said that, do remember that it is best to use simple java codes whenever possible

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

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

On a piece of paper or fl ip chart, write the problem statement.

Answered: 1 week ago

Question

What are the attributes of a technical decision?

Answered: 1 week ago

Question

How do the two components of this theory work together?

Answered: 1 week ago