Advertisement
Guest User

santa

a guest
Nov 15th, 2019
2,839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import java.io.ByteArrayInputStream;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class SantasHelper {
  9. static List<Wish> wishes = new ArrayList<>();
  10.  
  11. public static void testInput() {
  12. String input = "8\n" +
  13. "AddWish Electric Scooter 2000Z;3500.05;Rayko Petrov\n" +
  14. "AddWish Fortnite Skin;3000;Rayko Petrov\n" +
  15. "AddWish AMD Radeon;16400;Hristo\n" +
  16. "AddWish Apple AirPods;21111.50;Nadya\n" +
  17. "FindWishesByChild Rayko Petrov\n" +
  18. "DeleteWishes Rayko Petrov\n" +
  19. "FindWishesByChild Rayko Petrov\n" +
  20. "FindWishesByPriceRange 5000;30000";
  21. System.setIn(new ByteArrayInputStream(input.getBytes()));
  22. }
  23.  
  24. public static void main(String[] args) {
  25. testInput();
  26. Scanner scanner = new Scanner(System.in);
  27. int commandsCount = Integer.parseInt(scanner.nextLine());
  28.  
  29. for (int i = 0; i < commandsCount; i++) {
  30. String input = scanner.nextLine();
  31. executeCommand(input);
  32. }
  33. }
  34.  
  35. private static void executeCommand(String input) {
  36. int index = input.indexOf(" ");
  37. String command = input.substring(0, index);
  38. String remainder = input.substring(index + 1);
  39.  
  40. switch (command) {
  41. case "AddWish":
  42. addWishCommand(remainder);
  43. break;
  44. case "FindWishesByChild":
  45. findWishesByChild(remainder);
  46. break;
  47. case "DeleteWishes":
  48. deleteWishes(remainder);
  49. break;
  50. case "FindWishesByPriceRange":
  51. findWishesByPriceRange(remainder);
  52. break;
  53. default:
  54. }
  55. }
  56.  
  57.  
  58. private static void addWishCommand(String remainder) {
  59. String[] parameters = remainder.split(";");
  60. Wish wish = new Wish(parameters[0], parameters[2], Double.parseDouble(parameters[1]));
  61. wishes.add(wish);
  62. System.out.println("Wish added");
  63. }
  64.  
  65. private static void deleteWishes(String remainder) {
  66. String name = remainder.trim();
  67. List<Wish> wishesLeft =
  68. wishes.stream().filter(wish -> !name.equals(wish.childName)).collect(Collectors.toList());
  69.  
  70. int count = wishes.size() - wishesLeft.size();
  71. wishes = wishesLeft;
  72. if (count <= 0) {
  73. System.out.println("No Wishes deleted");
  74. } else {
  75. System.out.printf("%d Wishes deleted%n", count);
  76. }
  77. }
  78.  
  79. private static void findWishesByPriceRange(String remainder) {
  80. String[] prices = remainder.split(";");
  81. double minPrice = Double.parseDouble(prices[0]);
  82. double maxPrice = Double.parseDouble(prices[1]);
  83.  
  84. List<Wish> withinPriceRange =
  85. wishes.stream().filter(wish -> wish.price >= minPrice && wish.price <= maxPrice)
  86. .sorted(Comparator.comparing(Wish::toString))
  87. .collect(Collectors.toList());
  88.  
  89.  
  90. printWishes(withinPriceRange);
  91. }
  92.  
  93. private static void findWishesByChild(String remainder) {
  94. String name = remainder.trim();
  95.  
  96. List<Wish> wishesByChild = wishes.stream().filter(wish -> name.equals(wish.childName)).
  97. sorted(Comparator.comparing(Wish::toString))
  98. .collect(Collectors.toList());
  99.  
  100. printWishes(wishesByChild);
  101. }
  102.  
  103. private static void printWishes(List<Wish> wishesLeft) {
  104. if (wishesLeft.size() <= 0) {
  105. System.out.println("No Wishes found");
  106. } else {
  107. wishesLeft.forEach(wish -> System.out.println(wish.toString()));
  108. }
  109. }
  110.  
  111. static class Wish {
  112. private String itemName;
  113. private String childName;
  114. private double price;
  115.  
  116. public Wish(String itemName, String childName, double price) {
  117. this.itemName = itemName;
  118. this.childName = childName;
  119. this.price = price;
  120.  
  121. }
  122.  
  123. @Override
  124. public String toString() {
  125. return String.format("{%s;%s;%.2f}", itemName, childName, price);
  126. }
  127. }
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement