Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions