Advertisement
valitomi

Untitled

Jun 23rd, 2021
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) throws IOException {
  10. int counter = 0;
  11. StringBuilder sb = new StringBuilder();
  12. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  13. Set<Product> set = new TreeSet<>();
  14. boolean flag = true;
  15. while (flag) {
  16. String[] command = bf.readLine().split(" ");
  17. switch (command[0]) {
  18. case "add":
  19. if (command[1].length() < 3 ||
  20. command[1].length() > 20 ||
  21. Double.parseDouble(command[2]) < 0 ||
  22. Double.parseDouble(command[2]) > 5000 ||
  23. command[3].length() < 3 ||
  24. command[3].length() > 20){
  25. break;
  26. }
  27. String name = command[1];
  28. double price = Double.parseDouble(command[2]);
  29. String type = command[3];
  30. if (set.stream().noneMatch(u -> u.getName().equals(name))) {
  31. set.add(new Product(name, price, type));
  32. System.out.println("Ok: Item " + name + " added successfully");
  33. } else {
  34. System.out.println("Error: Item " + name + " already exists");
  35. }
  36. break;
  37. case "filter":
  38. switch (command.length) {
  39. //type from/to price
  40. case 5:
  41. if (command[3].equals("from")) {
  42. String prefix = "";
  43. sb.append("Ok: ");
  44. for (Product p : set) {
  45. if (p.getPrice() > Double.parseDouble(command[4])) {
  46. sb.append(prefix).append(p.getName()).append("(").append(p.getPrice()).append(")");
  47. prefix = ", ";
  48. counter++;
  49. }
  50. if (counter == 10) {
  51. break;
  52. }
  53. }
  54.  
  55. System.out.println(sb);
  56. sb.setLength(0);
  57. counter = 0;
  58. }
  59.  
  60. if (command[3].equals("to")) {
  61. String prefix = "";
  62. sb.append("Ok: ");
  63. for (Product p : set) {
  64. if (p.getPrice() < Double.parseDouble(command[4])) {
  65. sb.append(prefix).append(p.getName()).append("(").append(p.getPrice()).append(")");
  66. prefix = ", ";
  67. counter++;
  68. }
  69. if (counter == 10) {
  70. break;
  71. }
  72. }
  73. System.out.println(sb);
  74. sb.setLength(0);
  75. counter = 0;
  76. }
  77. break;
  78. // type
  79. case 4:
  80. String prefix1 = "";
  81. if (set.stream().noneMatch(u -> u.getType().equals(command[3]))) {
  82. System.out.println("Error: Type " + command[3] + " does not exists");
  83. break;
  84. }
  85. sb.append("Ok: ");
  86. for (Product p : set) {
  87. if (p.getType().equals(command[3])) {
  88. sb.append(prefix1).append(p.getName()).append("(").append(p.getPrice()).append(")");
  89. prefix1 = ", ";
  90. counter++;
  91. }
  92. if (counter == 10) {
  93. break;
  94. }
  95. }
  96. System.out.println(sb);
  97. sb.setLength(0);
  98. counter = 0;
  99.  
  100. break;
  101. // from to price
  102. case 7:
  103. String prefix2 = "";
  104. sb.append("Ok: ");
  105. for (Product p : set) {
  106. if (p.getPrice() > Double.parseDouble(command[4]) && p.getPrice() < Double.parseDouble(command[6])) {
  107. sb.append(prefix2).append(p.getName()).append("(").append(p.getPrice()).append(")");
  108. prefix2 = ", ";
  109. counter++;
  110. }
  111. if (counter == 10) {
  112. break;
  113. }
  114. }
  115.  
  116. System.out.println(sb);
  117. sb.setLength(0);
  118. counter = 0;
  119. break;
  120. }
  121. break;
  122.  
  123. case "end":
  124. flag = false;
  125. break;
  126.  
  127. }
  128. }
  129. }
  130.  
  131. private static class Product implements Comparable<Product> {
  132. private String name;
  133. private double price;
  134. private String type;
  135.  
  136. public Product(String name, double price, String type) {
  137. this.name = name;
  138. this.price = price;
  139. this.type = type;
  140. }
  141.  
  142. public String getName() {
  143. return name;
  144. }
  145.  
  146. public double getPrice() {
  147. return price;
  148. }
  149.  
  150. public String getType() {
  151. return type;
  152. }
  153.  
  154. @Override
  155. public int compareTo(Product o) {
  156. return Comparator.comparing(Product::getPrice).thenComparing(Product::getName).thenComparing(Product::getType).compare(this, o);
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement