Question: import java.util. * ; public class Payment { private HashMap userToAmount; private PriorityQueue > topUsers; private static final int TOP _ USERS _ COUNT =

import java.util.*;
public class Payment {
private HashMap userToAmount;
private PriorityQueue> topUsers;
private static final int TOP_USERS_COUNT =100;
public Payment(){
userToAmount = new HashMap<>();
topUsers = new PriorityQueue<>(Comparator.comparingInt(Map.Entry::getValue));
}
public TransactionSummary makePayment(int transactionId, int senderId, int amount, TransactionType transactionType){
if (transactionType == TransactionType.P2M){
userToAmount.put(senderId, userToAmount.getOrDefault(senderId,0)+ amount);
Map.Entry currentUser = new AbstractMap.SimpleEntry<>(senderId, userToAmount.get(senderId));
if (topUsers.size()< TOP_USERS_COUNT){
topUsers.add(currentUser);
} else if (topUsers.peek().getValue()< currentUser.getValue()){
topUsers.poll();
topUsers.add(currentUser);
}
}
boolean isSenderEligibleForReward = topUsers.contains(new AbstractMap.SimpleEntry<>(senderId, userToAmount.get(senderId)));
return new TransactionSummary(transactionId, isSenderEligibleForReward);
}
public int getNumberOfTransactions(int senderId, TransactionType transactionType){
return userToAmount.getOrDefault(senderId,0);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!