Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. import static java.lang.Double.parseDouble;
  4. import static java.lang.Integer.parseInt;
  5.  
  6. public class Problem3_SantaHelper {
  7. private static Map<String, List<Wish>> santa_map = new HashMap<>();
  8.  
  9. private static final String WISH_ADDED_MSG = "Wish added";
  10. private static final String WISHES_DELETED_MSG = "%d Wishes deleted";
  11. private static final String NO_WISHES_FOUND = "No Wishes found";
  12.  
  13. public static void main(String[] args) {
  14. Scanner scanner = new Scanner(System.in);
  15. int commandCount = parseInt(scanner.nextLine());
  16.  
  17. String[] input;
  18. String command;
  19. String[] params;
  20.  
  21. for (int i = 0; i < commandCount; i++) {
  22. input = scanner.nextLine().split(" ", 2);
  23. command = input[0];
  24. params = input[1].split(";");
  25.  
  26. switch (command) {
  27. case "AddWish":
  28. addWish(params);
  29. break;
  30. case "DeleteWishes":
  31. deleteWishes(params);
  32. break;
  33. case "FindWishesByPriceRange":
  34. findWishesByPriceRange(params);
  35. break;
  36. case "FindWishesByChild":
  37. findWishesByChild(params);
  38. break;
  39. }
  40. }
  41. }
  42. //Find Wishes
  43. private static void findWishesByPriceRange(String[] params) {
  44. List<Wish> found = new ArrayList<>();
  45. int countOfWishesFound = 0;
  46. double from = parseDouble(params[0]);
  47. double to = parseDouble(params[1]);
  48.  
  49. for (Map.Entry<String, List<Wish>> entry : santa_map.entrySet()) {
  50. for (Wish wish : entry.getValue()) {
  51. if (wish.getItemPrice() >= from && wish.getItemPrice() <= to) {
  52. countOfWishesFound++;
  53. found.add(wish);
  54. }
  55. }
  56. }
  57.  
  58. if (countOfWishesFound == 0) {
  59. System.out.println(NO_WISHES_FOUND);
  60. } else {
  61. found.stream().sorted(Comparator.comparing(Wish::getItemName)).forEach(System.out::print);
  62. }
  63. }
  64. private static void findWishesByChild(String[] params) {
  65. String childName = params[0];
  66.  
  67. if (santa_map.containsKey(childName)) {
  68. System.out.println(Wish.printWishes(santa_map.get(childName)));
  69. } else {
  70. System.out.println(NO_WISHES_FOUND);
  71. }
  72. }
  73.  
  74.  
  75. //Delete Wish
  76. private static void deleteWishes(String[] params) {
  77. String childName = params[0]; //child's wishes to find
  78. if (santa_map.containsKey(childName)) {
  79. int removedListSize = santa_map.remove(childName).size(); //removes and returns removed list
  80. System.out.println(String.format(WISHES_DELETED_MSG, removedListSize));
  81. } else {
  82. System.out.println(NO_WISHES_FOUND);
  83. }
  84. }
  85.  
  86. //Add Wish
  87. private static void addWish(String[] params) {
  88. String itemName = params[0]; //item name from input
  89. Double price = parseDouble(params[1]); //price from input
  90. String childName = params[2]; //child name input
  91.  
  92. santa_map.putIfAbsent(childName, new ArrayList<>());
  93. santa_map.get(childName).add(new Wish(childName, itemName, price));
  94.  
  95. System.out.println(WISH_ADDED_MSG);
  96. }
  97.  
  98.  
  99. //Class Wish
  100. static class Wish {
  101. private String childName;
  102. private String itemName;
  103. private Double itemPrice;
  104.  
  105. Wish(String name, String item, Double price) {
  106. this.childName = name;
  107. this.itemName = item;
  108. this.itemPrice = price;
  109. }
  110.  
  111. String getChildName() {
  112. return childName;
  113. }
  114.  
  115. String getItemName() {
  116. return itemName;
  117. }
  118.  
  119. Double getItemPrice() {
  120. return itemPrice;
  121. }
  122.  
  123. static String printWishes(List<Wish> wishes) {
  124. StringBuilder strBuilder = new StringBuilder();
  125.  
  126. wishes
  127. .stream()
  128. .sorted(Comparator.comparing(Wish::getItemName))
  129. .forEach(x1 -> strBuilder.append(x1).append(System.lineSeparator()));
  130.  
  131. return strBuilder.toString().trim();
  132. }
  133.  
  134. @Override
  135. public String toString() {
  136. return String.format("{%s;%s;%.2f}\n", this.getItemName(), this.getChildName(), this.getItemPrice());
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement