Advertisement
Whi7eW0lf

Untitled

Jul 19th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class test {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         Map<String, Integer> nameAndCount = new TreeMap<>();
  8.         Map<String, Integer> toyAndCount = new LinkedHashMap<>();
  9.  
  10.         String input = scanner.nextLine();
  11.  
  12.         while (!input.equals("END")) {
  13.             String[] tokens = input.split("->");
  14.             String name = tokens[0];
  15.             if (name.equals("Remove")) {
  16.                 nameAndCount.remove(tokens[1]);
  17.             } else {
  18.                 name = tokens[0];
  19.                 String typeGift = tokens[1];
  20.                 Integer amount = Integer.parseInt(tokens[2]);
  21.  
  22.                 if (!nameAndCount.containsKey(name)){
  23.  
  24.                     nameAndCount.put(name,amount);
  25.                     if (!toyAndCount.containsKey(typeGift)){
  26.  
  27.                         toyAndCount.put(typeGift,amount);
  28.                     }else{
  29.  
  30.                         int current = toyAndCount.get(typeGift);
  31.                         toyAndCount.put(typeGift,current + amount);
  32.                     }
  33.  
  34.                 }else{
  35.  
  36.                     int currentNameCount = nameAndCount.get(name);
  37.                     nameAndCount.put(name,currentNameCount + amount);
  38.  
  39.                     String toy = tokens[1];
  40.  
  41.                     if (!toyAndCount.containsKey(toy)){
  42.                         toyAndCount.put(tokens[1],amount);
  43.                     }else {
  44.                         int currentToyCount = toyAndCount.get(toy);
  45.                         toyAndCount.put(tokens[1],currentToyCount+amount);
  46.                     }
  47.                 }
  48.             }
  49.             input = scanner.nextLine();
  50.         }
  51.         System.out.println("Children:");
  52.  
  53.         nameAndCount.entrySet().stream().sorted((f,s)->
  54.                 Integer.compare(s.getValue(),f.getValue())
  55.         ) .forEach(e -> System.out.println(String.format("%s -> %s",e.getKey(), e.getValue())));
  56.  
  57.         System.out.println("Presents:");
  58.         toyAndCount.forEach((key, value) -> System.out.println(String.format("%s -> %s", key, value)));
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement