Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Santa {
  4.  
  5. private static Map<String, List<Wishes>> children = new HashMap<>();
  6.  
  7. static class Wishes {
  8.  
  9. private String itemName;
  10. private String childName;
  11. private double itemPrice;
  12.  
  13. Wishes(String itemName, String childName, double itemPrice) {
  14. this.itemName = itemName;
  15. this.childName = childName;
  16. this.itemPrice = itemPrice;
  17. }
  18.  
  19. String getItemName() {
  20. return itemName;
  21. }
  22.  
  23. @Override
  24. public String toString() {
  25. return String.format("{%s;%s;%.2f}", itemName, childName, itemPrice);
  26. }
  27. }
  28.  
  29. public static void main(String[] args) {
  30. Scanner scanner = new Scanner(System.in);
  31.  
  32. int numberOfCommands = Integer.parseInt(scanner.nextLine());
  33.  
  34. for (int i = 0; i < numberOfCommands; i++) {
  35.  
  36. String input = scanner.nextLine();
  37. String[] commands = input.split(" ");
  38. String command = commands[0];
  39.  
  40. String[] commandParameters = input.substring(commands[0].length() + 1).split(";");
  41.  
  42. switch (command) {
  43. case "AddWish":
  44. addWish(commandParameters);
  45. break;
  46. case "DeleteWishes":
  47. deleteWish(commandParameters);
  48. break;
  49. case "FindWishesByPriceRange":
  50. findWishesByPriceRange(commandParameters);
  51. break;
  52. case "FindWishesByChild":
  53. findWishesByChild(commandParameters);
  54. break;
  55. }
  56. }
  57. }
  58.  
  59. private static void addWish(String[] command) {
  60.  
  61. String itemName = command[0];
  62. double price = Double.parseDouble(command[1]);
  63. String childName = command[2];
  64.  
  65. Wishes wish = new Wishes(itemName, childName, price);
  66.  
  67. if (!children.containsKey(wish.childName)) {
  68. children.put(wish.childName, new ArrayList<>());
  69. }
  70.  
  71. children.get(wish.childName).add(wish);
  72.  
  73. System.out.println("Wish added");
  74. }
  75.  
  76. private static void deleteWish(String[] command) {
  77. String childName = command[0];
  78.  
  79. if (!children.containsKey(childName)) {
  80. System.out.println("No Wishes found");
  81. return;
  82. }
  83. int wishesCount = children.get(childName).size();
  84. children.remove(childName);
  85.  
  86. System.out.printf("%d Wishes deleted", wishesCount);
  87. System.out.println();
  88. }
  89.  
  90. private static void findWishesByChild(String[] command) {
  91. String childName = command[0];
  92.  
  93. if (!children.containsKey(childName)) {
  94. System.out.println("No Wishes found");
  95. } else {
  96.  
  97. StringBuilder strBuilder = new StringBuilder();
  98.  
  99. children.get(childName)
  100. .stream()
  101. .sorted(Comparator.comparing(Wishes::getItemName))
  102. .forEach(wish -> strBuilder.append(wish).append(System.lineSeparator()));
  103.  
  104. System.out.println(strBuilder.toString().trim());
  105. }
  106. }
  107.  
  108. private static void findWishesByPriceRange(String[] command) {
  109.  
  110. double fromPrice = Double.parseDouble(command[0]);
  111. double toPrice = Double.parseDouble(command[1]);
  112.  
  113. StringBuilder strBuilder = new StringBuilder();
  114.  
  115. if (children.isEmpty()) {
  116. System.out.println("No Wishes found");
  117. return;
  118. }
  119.  
  120. children.values().stream()
  121. .flatMap(Collection::stream)
  122. .filter(wish -> !(wish.itemPrice < fromPrice) && !(wish.itemPrice > toPrice))
  123. .sorted(Comparator.comparing(Santa.Wishes::getItemName))
  124. .forEach(wish -> strBuilder.append(wish).append(System.lineSeparator()));
  125.  
  126. if (strBuilder.length() < 1) {
  127. System.out.println("No Wishes found");
  128. return;
  129. }
  130.  
  131. System.out.println(strBuilder.toString().trim());
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement