Advertisement
Guest User

Untitled

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