Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package agents;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- Map<String, Map<String, Double>> agentMissions = new LinkedHashMap<>();
- Map<String, Double> missions = new HashMap<>();
- BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
- String input;
- while (!("registration".equals(input = consoleReader.readLine()))) {
- if (isMission(input)) {
- String missionName = input.split(":")[0];
- Double missionRating = Double.parseDouble(input.split(":")[1]);
- missions.put(missionName, missionRating);
- } else if (isAgent(input)) {
- if (input.length() < 3)
- continue;
- agentMissions.put(input, new LinkedHashMap<>());
- }
- }
- while (!("operate".equals(input = consoleReader.readLine()))) {
- String[] tokens= input.split("->");
- String operationName = tokens[0];
- switch (operationName) {
- case "assign":
- assign(agentMissions, missions, tokens);
- break;
- case "abort":
- abort(agentMissions, tokens);
- break;
- case "change":
- change(agentMissions, tokens);
- break;
- }
- }
- consoleReader.close();
- agentMissions.entrySet().stream() //streaming all agents
- .filter(a -> !a.getValue().isEmpty()) //remove all agents with 0 missions
- .sorted((a1, a2) -> Double.compare(sumRatings(a2.getValue()), sumRatings(a1.getValue()))) //descending
- .forEach(a -> {
- System.out.printf("Agent: %s - Total Rating: %.2f%n", a.getKey(), sumRatings(a.getValue()));
- a.getValue().entrySet().stream() //streaming a single agents missions
- .sorted((m1, m2) -> Double.compare(m2.getValue(), m1.getValue())) //descending
- .forEach(m -> {
- System.out.printf(" - %s -> %.2f%n", m.getKey(), m.getValue());
- });
- });
- }
- private static boolean isMission(String input) {
- return '#' == input.charAt(0);
- }
- private static boolean isAgent(String input) {
- return input.substring(input.length() - 3).charAt(0) == '0';
- }
- /*
- If command is "assign" – line format will be: assign->{agentName}->{missionName}.
- Your task is to assign a mission with its rating to a given agent.
- (One agent cannot assign a mission twice, but one mission can be assigned to many agents.)
- ONLY REGISTERED MISSION CAN BE ASSIGNED.
- */
- private static void assign(Map<String, Map<String, Double>> agentMissions, Map<String, Double> missions, String... tokens) {
- String agentName = tokens[1];
- String missionName = tokens[2];
- if (!missions.containsKey(missionName))
- return;
- if (!agentMissions.containsKey(agentName))
- return;
- agentMissions.get(agentName).put(missionName, missions.get(missionName));
- }
- /*
- If command is "abort" – line format will be: abort->{missionName}.
- Remove a mission from every agent that is assigned to it (one or many).
- */
- private static void abort(Map<String, Map<String, Double>> agentMissions, String... tokens) {
- String missionName = tokens[1];
- for (Map<String, Double> missionNameRating : agentMissions.values()) {
- missionNameRating.remove(missionName);
- }
- }
- /*
- If command is "change" – line format will be: change->{agentName}->{agentName}.
- Swap the missions with their ratings, of the two given agents (they are always be valid).
- */
- private static void change(Map<String, Map<String, Double>> agentMissions, String... tokens) {
- String firstAgentName = tokens[1];
- String secondAgentName = tokens[2];
- Map<String, Double> firstAgentsMissions = agentMissions.get(firstAgentName);
- Map<String, Double> secondAgentMissions = agentMissions.get(secondAgentName);
- agentMissions.put(firstAgentName, secondAgentMissions); //updates
- agentMissions.put(secondAgentName, firstAgentsMissions); //updates
- }
- private static Double sumRatings(Map<String, Double> missions) {
- return missions.entrySet().stream().mapToDouble(m -> m.getValue()).sum();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement