Advertisement
nikeza

Iron Girder /Map & List/

Jul 27th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package TechFundamentals;
  2.  
  3. import java.util.*;
  4.  
  5. public class old_FinalExams_Iron_Girder {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String input = scanner.nextLine();
  10.  
  11.         Map<String, List<Integer>> railway = new LinkedHashMap<>();
  12.  
  13.         while (!input.equals("Slide rule")) {
  14.             String[] tokens = input.split(":");
  15.             String town = tokens[0];
  16.  
  17.             List<Integer> timePassengers = railway.get(town);
  18.  
  19.             String[] tokensCommand = tokens[1].split("->");
  20.  
  21.             if (!tokensCommand[0].equals("ambush")) {
  22.                 int time = Integer.parseInt(tokensCommand[0]);
  23.                 int passengers = Integer.parseInt(tokensCommand[1]);
  24.                 if (!railway.containsKey(town)) {
  25.                     timePassengers = new ArrayList<>();
  26.                     timePassengers.add(time);
  27.                     timePassengers.add(passengers);
  28.                     railway.put(town, timePassengers);
  29.                 } else {
  30.                     int oldTime = railway.get(town).get(0);
  31.                     if (time < oldTime || oldTime == 0) {
  32.                         railway.get(town).set(0, time);
  33.                     }
  34.                     int newSumPassenger = railway.get(town).get(1) + passengers;
  35.                     railway.get(town).set(1, newSumPassenger);
  36.                 }
  37.             } else {
  38.                 int passengers = Integer.parseInt(tokensCommand[1]);
  39.  
  40.                 if (railway.containsKey(town)) {
  41.                     int removePassengers = railway.get(town).get(1) - passengers;
  42.                     railway.get(town).set(0, 0);
  43.                     railway.get(town).set(1, removePassengers);
  44.  
  45.                 }
  46.  
  47.             }
  48.  
  49.             input = scanner.nextLine();
  50.         }
  51.  
  52.         railway.entrySet().stream()
  53.                 .filter(e -> {
  54.                     List<Integer> tempList = e.getValue();
  55.                     return tempList.get(0) > 0 && tempList.get(1) > 0;
  56.                 })
  57.                 .sorted((f, s) -> {
  58.  
  59.                     int sorted = f.getValue().get(0) - s.getValue().get(0);
  60.                     if (sorted == 0) {
  61.                         return f.getKey().compareTo(s.getKey());
  62.                     } else {
  63.                         return Integer.compare(f.getValue().get(0), s.getValue().get(0));
  64.                     }
  65.                 })
  66.                 .forEach(entry -> {
  67.                     System.out.print(entry.getKey() + " -> Time: ");
  68.                     System.out.printf("%d -> Passengers: %d%n", entry.getValue().get(0), entry.getValue().get(1));
  69.  
  70.                 });
  71.  
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement