Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TechFundamentals;
- import java.util.*;
- public class old_FinalExams_Iron_Girder {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Map<String, List<Integer>> railway = new LinkedHashMap<>();
- while (!input.equals("Slide rule")) {
- String[] tokens = input.split(":");
- String town = tokens[0];
- List<Integer> timePassengers = railway.get(town);
- String[] tokensCommand = tokens[1].split("->");
- if (!tokensCommand[0].equals("ambush")) {
- int time = Integer.parseInt(tokensCommand[0]);
- int passengers = Integer.parseInt(tokensCommand[1]);
- if (!railway.containsKey(town)) {
- timePassengers = new ArrayList<>();
- timePassengers.add(time);
- timePassengers.add(passengers);
- railway.put(town, timePassengers);
- } else {
- int oldTime = railway.get(town).get(0);
- if (time < oldTime || oldTime == 0) {
- railway.get(town).set(0, time);
- }
- int newSumPassenger = railway.get(town).get(1) + passengers;
- railway.get(town).set(1, newSumPassenger);
- }
- } else {
- int passengers = Integer.parseInt(tokensCommand[1]);
- if (railway.containsKey(town)) {
- int removePassengers = railway.get(town).get(1) - passengers;
- railway.get(town).set(0, 0);
- railway.get(town).set(1, removePassengers);
- }
- }
- input = scanner.nextLine();
- }
- railway.entrySet().stream()
- .filter(e -> {
- List<Integer> tempList = e.getValue();
- return tempList.get(0) > 0 && tempList.get(1) > 0;
- })
- .sorted((f, s) -> {
- int sorted = f.getValue().get(0) - s.getValue().get(0);
- if (sorted == 0) {
- return f.getKey().compareTo(s.getKey());
- } else {
- return Integer.compare(f.getValue().get(0), s.getValue().get(0));
- }
- })
- .forEach(entry -> {
- System.out.print(entry.getKey() + " -> Time: ");
- System.out.printf("%d -> Passengers: %d%n", entry.getValue().get(0), entry.getValue().get(1));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement