Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class PracticeSessions {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String input = sc.nextLine();
- Map<String, List<String>> roads = new LinkedHashMap<>();
- String road = "";
- String racer = "";
- while (!input.equals("END")) {
- String[] tokens = input.split("->");
- String command = tokens[0];
- road = tokens[1];
- switch (command) {
- case "Add":
- racer = tokens[2];
- roads.putIfAbsent(road, new ArrayList<>());
- roads.get(road).add(racer);
- break;
- case "Move":
- racer = tokens[2];
- String nextRoad = tokens[3];
- if (roads.get(road).contains(racer)) {
- roads.get(nextRoad).add(racer);
- roads.get(road).remove(racer);
- }
- break;
- case "Close":
- if (roads.containsKey(road)) {
- roads.remove(road);
- }
- break;
- }
- input = sc.nextLine();
- }
- System.out.println("Practice sessions:");
- roads.entrySet().stream().sorted((f,s) -> {
- int result = s.getValue().size()- f.getValue().size();
- if (result == 0) {
- result = f.getKey().compareTo(s.getKey());
- }
- return result;
- }).forEach(e -> {System.out.println(e.getKey());
- e.getValue().stream().forEach(d -> System.out.printf("++%s%n", d));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement