Advertisement
meteor4o

JF-Exams-PracticeSessions

Aug 2nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class PracticeSessions {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         String input = sc.nextLine();
  10.         Map<String, List<String>> roads = new LinkedHashMap<>();
  11.         String road = "";
  12.         String racer = "";
  13.  
  14.  
  15.         while (!input.equals("END")) {
  16.  
  17.             String[] tokens = input.split("->");
  18.             String command = tokens[0];
  19.             road = tokens[1];
  20.  
  21.             switch (command) {
  22.                 case "Add":
  23.                     racer = tokens[2];
  24.                     roads.putIfAbsent(road, new ArrayList<>());
  25.                     roads.get(road).add(racer);
  26.                 break;
  27.  
  28.                 case "Move":
  29.                     racer = tokens[2];
  30.                     String nextRoad = tokens[3];
  31.                     if (roads.get(road).contains(racer)) {
  32.                         roads.get(nextRoad).add(racer);
  33.                         roads.get(road).remove(racer);
  34.  
  35.                     }
  36.                     break;
  37.  
  38.                 case "Close":
  39.                     if (roads.containsKey(road)) {
  40.                         roads.remove(road);
  41.                     }
  42.                     break;
  43.             }
  44.  
  45.             input = sc.nextLine();
  46.         }
  47.         System.out.println("Practice sessions:");
  48.         roads.entrySet().stream().sorted((f,s) -> {
  49.             int result = s.getValue().size()- f.getValue().size();
  50.             if (result == 0) {
  51.                 result = f.getKey().compareTo(s.getKey());
  52.             }
  53.             return result;
  54.         }).forEach(e -> {System.out.println(e.getKey());
  55.             e.getValue().stream().forEach(d -> System.out.printf("++%s%n", d));
  56.  
  57.         });
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement