Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class test {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Integer> nameAndCount = new TreeMap<>();
- Map<String, Integer> toyAndCount = new LinkedHashMap<>();
- String input = scanner.nextLine();
- while (!input.equals("END")) {
- String[] tokens = input.split("->");
- String name = tokens[0];
- if (name.equals("Remove")) {
- nameAndCount.remove(tokens[1]);
- } else {
- name = tokens[0];
- String typeGift = tokens[1];
- Integer amount = Integer.parseInt(tokens[2]);
- if (!nameAndCount.containsKey(name)){
- nameAndCount.put(name,amount);
- if (!toyAndCount.containsKey(typeGift)){
- toyAndCount.put(typeGift,amount);
- }else{
- int current = toyAndCount.get(typeGift);
- toyAndCount.put(typeGift,current + amount);
- }
- }else{
- int currentNameCount = nameAndCount.get(name);
- nameAndCount.put(name,currentNameCount + amount);
- String toy = tokens[1];
- if (!toyAndCount.containsKey(toy)){
- toyAndCount.put(tokens[1],amount);
- }else {
- int currentToyCount = toyAndCount.get(toy);
- toyAndCount.put(tokens[1],currentToyCount+amount);
- }
- }
- }
- input = scanner.nextLine();
- }
- System.out.println("Children:");
- nameAndCount.entrySet().stream().sorted((f,s)->
- Integer.compare(s.getValue(),f.getValue())
- ) .forEach(e -> System.out.println(String.format("%s -> %s",e.getKey(), e.getValue())));
- System.out.println("Presents:");
- toyAndCount.forEach((key, value) -> System.out.println(String.format("%s -> %s", key, value)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement