Guest User

04. Agents

a guest
Feb 23rd, 2019
531
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.xml.crypto.dsig.keyinfo.KeyValue;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. import java.util.zip.DeflaterOutputStream;
  5.  
  6. public class Agents {
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         LinkedHashMap<String, HashMap<String, Double>> agentsAndMissionsStart = new LinkedHashMap<>();
  10.         HashMap<String, Double> missions = new HashMap<>();
  11.         String[] tokens = scan.nextLine().split(":");
  12.         while (!tokens[0].equals("registration")) {
  13.             if (tokens.length == 1) {
  14.                 String agent = tokens[0];
  15.                 agentsAndMissionsStart.put(agent, new HashMap<>());
  16.             } else {
  17.                 String mission = tokens[0];
  18.                 double rating = Double.parseDouble(tokens[1]);
  19.                 missions.put(mission, rating);
  20.             }
  21.             tokens = scan.nextLine().split(":");
  22.         }
  23.         //1st phase completed
  24.  
  25.  
  26.         //assignments start here:
  27.         LinkedHashMap<String, HashMap<String, Double>> agentsAndMissions = new LinkedHashMap<>();
  28.         tokens = scan.nextLine().split("->");
  29.         while (!tokens[0].equals("operate")) {
  30.             String command = tokens[0];
  31.             if (command.equals("assign")) {
  32.                 String agent = tokens[1];
  33.                 String mission = tokens[2];
  34.                 if (missions.containsKey(mission)
  35.                         && !agentsAndMissionsStart.get(agent).containsKey(mission)) {
  36.                     double rating = missions.get(mission);
  37.                     agentsAndMissions.putIfAbsent(agent, new HashMap<>());
  38.                     agentsAndMissions.get(agent).put(mission, rating);
  39.                 }
  40.  
  41.             } else if (command.equals("abort")) {
  42.                 String mission = tokens[1];
  43.                 for (String agent : agentsAndMissions.keySet()) {
  44.                     if (agentsAndMissions.get(agent).containsKey(mission)) {
  45.                         agentsAndMissions.get(agent).remove(mission);
  46.                     }
  47.                 }
  48.  
  49.             } else if (command.equals("change")) {
  50.                 String agent_1 = tokens[1];
  51.                 String agent_2 = tokens[2];
  52.                 HashMap<String, Double> agentMissions = agentsAndMissions.get(agent_1);
  53.                 agentsAndMissions.put(agent_1, agentsAndMissions.get(agent_2));
  54.                 agentsAndMissions.put(agent_2, agentMissions);
  55.  
  56.             }
  57.             tokens = scan.nextLine().split("->");
  58.         }
  59.         //assignments completed
  60.  
  61.         List<String> freeAgents = agentsAndMissions.keySet().stream()
  62.                 .filter(x -> agentsAndMissions.get(x).size() == 0)
  63.                 .collect(Collectors.toCollection(ArrayList::new));
  64.         for (String freeAgent : freeAgents) {
  65.             agentsAndMissions.remove(freeAgent);
  66.         }
  67.  
  68.         //print result:
  69.         LinkedHashMap<String, Double> agentsPerRatingIni = new LinkedHashMap<>();
  70.         for (String agent : agentsAndMissions.keySet()) {
  71.             double totalRating = 0;
  72.             for (String mission : agentsAndMissions.get(agent).keySet()) {
  73.                 totalRating += agentsAndMissions.get(agent).get(mission);
  74.             }
  75.             agentsPerRatingIni.put(agent, totalRating);
  76.         }
  77.  
  78.         List<String> agentsPerRating = new ArrayList<>();
  79.  
  80.         agentsPerRating = agentsPerRatingIni.keySet().stream()
  81.                 .sorted((b, a) -> Double.compare(agentsPerRatingIni.get(a), agentsPerRatingIni.get(b)))
  82.                 .collect(Collectors.toList());
  83.  
  84.         for (String agent : agentsPerRating) {
  85.             System.out.printf("Agent: %s - Total Rating: %.2f%n",
  86.                     agent, agentsPerRatingIni.get(agent));
  87.             agentsAndMissions.get(agent).entrySet().stream()
  88.                     .sorted((b, a) -> Double.compare(a.getValue(), b.getValue()))
  89.                     .forEach(mission -> System.out.printf("- %s -> %.2f%n"
  90.                             , mission.getKey(), mission.getValue()));
  91.         }
  92.  
  93.     }
  94. }
RAW Paste Data