Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
2.7 kB
1
Indexable
Never
import java.util.*;

public class Main {

    public static void settleExactPayouts(HashMap<String, Integer> inputMap)
    {
        for (String i : inputMap.keySet()) {
            if(inputMap.get(i) < 0)
            {
                for (String j : inputMap.keySet())
                {
                    if(i.equals(j)) continue;
                    if(Math.abs(inputMap.get(i))==(inputMap.get(j)))
                    {
                        System.out.println(i+" "+Math.abs(inputMap.get(i))+" to "+j);
                        inputMap.put(i,0);
                        inputMap.put(j,0);
                        break;
                    }

                }

            }

        }
    }

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("How many players:");
        int numOfPlayers = sc.nextInt();sc.nextLine();
        int profit,tempEarn;
        String name,biggestEarner = null;

        HashMap<String, Integer> nameAndProfit = new HashMap<>();


        for(int i=0;i<numOfPlayers;i++)
        {
            System.out.print("Enter name then profit ");
            name = sc.nextLine();
            profit = sc.nextInt();sc.nextLine();
            nameAndProfit.put(name,profit);
        }


        for (String i : nameAndProfit.keySet()) {
            while(nameAndProfit.get(i) < 0)
            {
                settleExactPayouts(nameAndProfit);
                if(!(nameAndProfit.get(i) <0)) break;
                for (String j : nameAndProfit.keySet())
                {

                    if(i.equals(j)) continue;
                    if(Math.abs(nameAndProfit.get(i)) <= nameAndProfit.get(j))
                    {
                        System.out.println(i+" "+Math.abs(nameAndProfit.get(i))+" to "+j);
                        nameAndProfit.put(j,nameAndProfit.get(j)-Math.abs(nameAndProfit.get(i)));
                        nameAndProfit.put(i,0);
                        break;
                    }

                }

                if(!(nameAndProfit.get(i) <0)) break;
                tempEarn=0;
                for (String j : nameAndProfit.keySet()) {
                    if(nameAndProfit.get(j)>tempEarn) {
                        tempEarn = nameAndProfit.get(j);
                        biggestEarner=j;
                    }
                }

                System.out.println(i+" "+nameAndProfit.get(biggestEarner)+" to "+biggestEarner);
                nameAndProfit.put(i,nameAndProfit.get(i)+nameAndProfit.get(biggestEarner));
                nameAndProfit.put(biggestEarner,0);

            }



        }


    }
}