Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
1,649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.*;
  6.  
  7. public class Santa {
  8. private static void testInput() {
  9. String input = "10\n" +
  10. "AddWish Electric Scooter 2000Z;1536.50;Stefan Petrov\n" +
  11. "AddWish Fortnite Skin;3000;Stefan\n" +
  12. "AddWish AMD Radeon;4099.99;Pesho\n" +
  13. "AddWish Apple AirPods;200000;Kiril\n" +
  14. "AddWish Socks;10000;Tosho\n" +
  15. "AddWish Swater;999;Stefan\n" +
  16. "FindWishesByChild Stefan Gosho\n" +
  17. "DeleteWishes Stefan Petrov\n" +
  18. "FindWishesByChild Stefan\n" +
  19. "FindWishesByPriceRange 100000;200000";
  20. String input2 = "3\n" +
  21. "DeleteWishes BBB\n" +
  22. "FindWishesByChild XXX\n" +
  23. "FindWishesByPriceRange 0;10000\n";
  24. System.setIn(new ByteArrayInputStream(input.getBytes()));
  25. }
  26.  
  27. public static class Wish {
  28. String itemName;
  29. double price;
  30. String childName;
  31.  
  32. Wish(String itemName, double price, String childName) {
  33. this.itemName = itemName;
  34. this.price = price;
  35. this.childName = childName;
  36. }
  37.  
  38. @Override
  39. public String toString() {
  40. return String.format(Locale.ROOT, "||%s, %.2f, %s||", itemName, price, childName);
  41. }
  42. }
  43.  
  44. private static List<Wish> wishesList = new ArrayList<>();
  45. private static Map<String, List<Wish>> mapByChildren = new HashMap<>();
  46.  
  47. public static void main(String[] args) throws IOException {
  48. testInput();
  49. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  50. int numberOfCommands = Integer.parseInt(reader.readLine());
  51.  
  52. for (int i = 0; i < numberOfCommands; i++) {
  53. String[] command = reader.readLine().split(" ");
  54.  
  55. switch (command[0]) {
  56. case "AddWish":
  57. addWish(command);
  58. break;
  59. case "DeleteWishes":
  60. deleteWishes(command);
  61. break;
  62. case "FindWishesByChild":
  63. findWishesByChild(command);
  64. break;
  65. case "FindWishesByPriceRange":
  66. findWishesByPriceRange(command);
  67. break;
  68. }
  69. }
  70. }
  71.  
  72. private static void findWishesByPriceRange(String[] partialCommand) {
  73. String join = String.join(" ", partialCommand);
  74. join = join.replace("FindWishesByPriceRange ", "");
  75. String[] command = join.split(";");
  76.  
  77. double fromPrice = Double.parseDouble(command[0]);
  78. double toPrice = Double.parseDouble(command[1]);
  79. List<Wish> list = new ArrayList<>();
  80. for (Wish value : wishesList) {
  81. if (value.price >= fromPrice && value.price <= toPrice) {
  82. list.add(value);
  83. }
  84. }
  85. if (list.size() == 0) {
  86. System.out.println("No Wishes found");
  87. } else {
  88. list.sort(Comparator.comparing(o -> o.itemName));
  89. for (Wish wish : list) {
  90. System.out.println(String.format(Locale.ROOT, "{%s;%s;%.2f}", wish.itemName, wish.childName, wish.price));
  91. }
  92. }
  93. }
  94.  
  95. private static void findWishesByChild(String[] command) {
  96. String name;
  97. if (command.length == 2) {
  98. name = command[1];
  99. } else {
  100. name = command[1] + " " + command[2];
  101. }
  102. if (mapByChildren.get(name) == null) {
  103. System.out.println("No Wishes found");
  104. } else {
  105. List<Wish> list = new ArrayList<>(mapByChildren.get(name));
  106. list.sort(Comparator.comparing(o -> o.itemName));
  107. for (Wish wish : list) {
  108. System.out.println(String.format(Locale.ROOT, "{%s;%s;%.2f}", wish.itemName, wish.childName, wish.price));
  109. }
  110. }
  111. }
  112.  
  113. private static void deleteWishes(String[] command) {
  114. String name;
  115. if (command.length == 2) {
  116. name = command[1];
  117. } else {
  118. name = command[1] + " " + command[2];
  119. }
  120. if (mapByChildren.get(name) == null) {
  121. System.out.println("No Wishes found");
  122. return;
  123. }
  124. System.out.println(String.format("%d Wishes deleted", mapByChildren.get(name).size()));
  125. mapByChildren.remove(name);
  126. for (int i = 0; i < wishesList.size(); i++) {
  127. if (wishesList.get(i).childName.equals(name)) {
  128. wishesList.remove(i);
  129. i--;
  130. }
  131. }
  132. }
  133.  
  134. private static void addWish(String[] partialCommand) {
  135. String join = String.join(" ", partialCommand);
  136. join = join.replace("AddWish ", "");
  137. String[] command = join.split(";");
  138. Wish wish = new Wish(command[0], Double.parseDouble(command[1]), command[2]);
  139.  
  140. List<Wish> wishes = mapByChildren.getOrDefault(wish.childName, new ArrayList<>());
  141. wishes.add(wish);
  142. mapByChildren.put(wish.childName, wishes);
  143. wishesList.add(wish);
  144. System.out.println("Wish added");
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement