Advertisement
Guest User

P04.Agents

a guest
Feb 2nd, 2019
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. package agents;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class Main {
  9.     public static void main(String[] args) throws IOException {
  10.         Map<String, Map<String, Double>> agentMissions = new LinkedHashMap<>();
  11.         Map<String, Double> missions = new HashMap<>();
  12.  
  13.         BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
  14.  
  15.         String input;
  16.         while (!("registration".equals(input = consoleReader.readLine()))) {
  17.             if (isMission(input)) {
  18.                 String missionName = input.split(":")[0];
  19.                 Double missionRating = Double.parseDouble(input.split(":")[1]);
  20.                 missions.put(missionName, missionRating);
  21.             } else if (isAgent(input)) {
  22.                 if (input.length() < 3)
  23.                     continue;
  24.  
  25.                 agentMissions.put(input, new LinkedHashMap<>());
  26.             }
  27.         }
  28.  
  29.         while (!("operate".equals(input = consoleReader.readLine()))) {
  30.             String[] tokens= input.split("->");
  31.             String operationName = tokens[0];
  32.  
  33.             switch (operationName) {
  34.                 case "assign":
  35.                     assign(agentMissions, missions, tokens);
  36.                     break;
  37.  
  38.                 case "abort":
  39.                     abort(agentMissions, tokens);
  40.                     break;
  41.  
  42.                 case "change":
  43.                     change(agentMissions, tokens);
  44.                     break;
  45.             }
  46.         }
  47.  
  48.         consoleReader.close();
  49.  
  50.         agentMissions.entrySet().stream() //streaming all agents
  51.                 .filter(a -> !a.getValue().isEmpty()) //remove all agents with 0 missions
  52.                 .sorted((a1, a2) -> Double.compare(sumRatings(a2.getValue()), sumRatings(a1.getValue()))) //descending
  53.                 .forEach(a -> {
  54.                     System.out.printf("Agent: %s - Total Rating: %.2f%n", a.getKey(), sumRatings(a.getValue()));
  55.                     a.getValue().entrySet().stream() //streaming a single agents missions
  56.                             .sorted((m1, m2) -> Double.compare(m2.getValue(), m1.getValue())) //descending
  57.                             .forEach(m -> {
  58.                                 System.out.printf(" - %s -> %.2f%n", m.getKey(), m.getValue());
  59.                             });
  60.                 });
  61.     }
  62.  
  63.     private static boolean isMission(String input) {
  64.         return '#' == input.charAt(0);
  65.  
  66.     }
  67.  
  68.     private static boolean isAgent(String input) {
  69.         return input.substring(input.length() - 3).charAt(0) == '0';
  70.     }
  71.  
  72.     /*
  73.     If command is "assign" – line format will be:  assign->{agentName}->{missionName}.
  74.     Your task is to assign a mission with its rating to a given agent.
  75.     (One agent cannot assign a mission twice, but one mission can be assigned to many agents.)
  76.     ONLY REGISTERED MISSION CAN BE ASSIGNED.
  77.      */
  78.     private static void assign(Map<String, Map<String, Double>> agentMissions, Map<String, Double> missions, String... tokens) {
  79.         String agentName = tokens[1];
  80.         String missionName = tokens[2];
  81.  
  82.         if (!missions.containsKey(missionName))
  83.             return;
  84.  
  85.         if (!agentMissions.containsKey(agentName))
  86.             return;
  87.  
  88.         agentMissions.get(agentName).put(missionName, missions.get(missionName));
  89.     }
  90.  
  91.     /*
  92.     If command is "abort" – line format will be: abort->{missionName}.
  93.     Remove a mission from every agent that is assigned to it (one or many).
  94.      */
  95.     private static void abort(Map<String, Map<String, Double>> agentMissions, String... tokens) {
  96.         String missionName = tokens[1];
  97.  
  98.         for (Map<String, Double> missionNameRating : agentMissions.values()) {
  99.             missionNameRating.remove(missionName);
  100.         }
  101.     }
  102.  
  103.     /*
  104.     If command is "change" – line format will be: change->{agentName}->{agentName}.
  105.     Swap the missions with their ratings, of the two given agents (they are always be valid).
  106.      */
  107.     private static void change(Map<String, Map<String, Double>> agentMissions, String... tokens) {
  108.         String firstAgentName = tokens[1];
  109.         String secondAgentName = tokens[2];
  110.  
  111.         Map<String, Double> firstAgentsMissions = agentMissions.get(firstAgentName);
  112.         Map<String, Double> secondAgentMissions = agentMissions.get(secondAgentName);
  113.  
  114.         agentMissions.put(firstAgentName, secondAgentMissions); //updates
  115.         agentMissions.put(secondAgentName, firstAgentsMissions); //updates
  116.     }
  117.  
  118.     private static Double sumRatings(Map<String, Double> missions) {
  119.         return missions.entrySet().stream().mapToDouble(m -> m.getValue()).sum();
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement