Advertisement
Alekss33

Untitled

May 3rd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.InputStreamReader;
  4.  
  5. import java.text.DecimalFormat;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.TreeSet;
  9. import java.util.stream.Collectors;
  10.  
  11. public class OnlineMarket {
  12.  
  13. public static void testInput() {
  14. String result = "add Milk 1.90 dairy\n" +
  15. "add Yogurt 1.90 dairy\n" +
  16. "add Notebook 1111.90 technology\n" +
  17. "add Orbit 0.90 food\n" +
  18. "add Rakia 11.90 drinks\n" +
  19. "add Dress 121.90 clothes\n" +
  20. "add Jacket 49.90 clothes\n" +
  21. "add Milk 1.90 dairy\n" +
  22. "add Eggs 2.34 food\n" +
  23. "add Cheese 5.55 dairy\n" +
  24. "filter by type clothes\n" +
  25. "filter by price from 1.00 to 2.00\n" +
  26. "add CappyOrange 1.99 juice \n" +
  27. "add Nestey 2.7 juice \n" +
  28. "filter by price from 1200\n" +
  29. "add Socks 2.90 clothes\n" +
  30. "filter by type fruits\n" +
  31. "nextpls\n" +
  32. "add MacBookPro 1700.1234 technology\n" +
  33. "filter by price from 1200\n" +
  34. "filter by price from 1.50\n" +
  35. "filter by price to 2.00\n" +
  36. "filter by type clothes\n" +
  37. "end";
  38.  
  39. System.setIn(new ByteArrayInputStream(result.getBytes()));
  40.  
  41. }
  42.  
  43. static class Product implements Comparable {
  44. String productName;
  45. double productPrice;
  46. String productType;
  47.  
  48. public Product(String productName, double productPrice, String productType) {
  49. this.productName = productName;
  50. this.productPrice = productPrice;
  51. this.productType = productType;
  52. }
  53.  
  54. DecimalFormat df = new DecimalFormat("#.####");
  55. @Override
  56. public String toString() {
  57. return String.format("%s(%s)", productName, df.format(productPrice));
  58. }
  59.  
  60. // @Override
  61. // public String toString() {
  62. // String str = productName + "(" + productPrice + ")";
  63. // return str;
  64. // }
  65.  
  66. @Override
  67. public int compareTo(Object o) {
  68. Product product = (Product) o;
  69. int priceToCompare = Double.compare(product.productPrice, this.productPrice);
  70. if (priceToCompare != 0) {
  71. return -priceToCompare;
  72. }
  73. return 1;
  74. }
  75. }
  76.  
  77. private static StringBuilder strBuilder = new StringBuilder();
  78. private static HashMap<String, Product> productMap = new HashMap<>();
  79. private static HashMap<String, TreeSet<Product>> productsFirstByType = new HashMap<>();
  80. private static TreeSet<Product> productsOrderedByPrice = new TreeSet<>();
  81.  
  82.  
  83. public static void main(String[] args) throws Exception {
  84.  
  85. //testInput();
  86. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  87.  
  88. while (true) {
  89. String inputReader = input.readLine();
  90. String[] parameters = inputReader.split(" ");
  91. String command = parameters[0];
  92.  
  93. switch (command) {
  94.  
  95. case "add":
  96. addProduct(parameters);
  97. break;
  98. case "filter":
  99. if (parameters.length == 4) {
  100. filterByType(parameters);
  101. } else if (parameters.length == 7) {
  102. filterMinToMax(parameters);
  103. } else if (parameters.length == 5) {
  104. if (parameters[3].equals("from")) {
  105. filterMin(parameters);
  106. } else if (parameters[3].equals("to")) {
  107. filterMax(parameters);
  108. }
  109. }
  110. break;
  111.  
  112. case "end":
  113. System.out.println(strBuilder);
  114. return;
  115.  
  116. }
  117.  
  118. }
  119.  
  120.  
  121. }
  122.  
  123. private static void filterMax(String[] parameters) {
  124. Double value = Double.parseDouble(parameters[4]);
  125.  
  126. strBuilder.append("OK: ");
  127. List<Product> showList = productsOrderedByPrice.stream()
  128. .filter(product -> product.productPrice < value)
  129. .collect(Collectors.toList());
  130. for (int i = 0; i < showList.size(); i++) {
  131. if (i<10){
  132. strBuilder.append(showList.get(i) + ", ");
  133. }
  134. }
  135. if (showList.size() != 0) {
  136. strBuilder.deleteCharAt(strBuilder.length() - 1);
  137. strBuilder.deleteCharAt(strBuilder.length() - 1);
  138. }
  139. strBuilder.append("\n");
  140.  
  141.  
  142. }
  143.  
  144. private static void filterMin(String[] parameters) {
  145. Double value = Double.parseDouble(parameters[4]);
  146.  
  147. strBuilder.append("Ok: ");
  148. List<Product> showList = productsOrderedByPrice.stream()
  149. .filter(product -> product.productPrice > value)
  150. .collect(Collectors.toList());
  151. for (int i = 0; i < showList.size(); i++) {
  152. if (i<10){
  153. strBuilder.append(showList.get(i) + ", ");
  154. }
  155. }
  156. if (showList.size() != 0) {
  157. strBuilder.deleteCharAt(strBuilder.length() - 1);
  158. strBuilder.deleteCharAt(strBuilder.length() - 1);
  159. }
  160. strBuilder.append("\n");
  161.  
  162. // for (int i = 0; i < ; i++) {
  163. //
  164. // }
  165. }
  166.  
  167. private static void filterMinToMax(String[] parameters) {
  168. Double minValue = Double.parseDouble(parameters[4]);
  169. Double maxValue = Double.parseDouble(parameters[6]);
  170.  
  171. strBuilder.append("Ok: ");
  172. List<Product> showList = productsOrderedByPrice.stream()
  173. .filter(product -> product.productPrice > minValue && product.productPrice < maxValue)
  174. .collect(Collectors.toList());
  175. for (int i = 0; i < showList.size(); i++) {
  176. if (i<10){
  177. strBuilder.append(showList.get(i) + ", ");
  178. }
  179. }
  180. strBuilder.deleteCharAt(strBuilder.length() - 1);
  181. strBuilder.deleteCharAt(strBuilder.length() - 1);
  182. strBuilder.append("\n");
  183. }
  184.  
  185. private static void filterByType(String[] parameters) {
  186. if (productsFirstByType.containsKey(parameters[3])) {
  187. strBuilder.append("Ok: ");
  188. strBuilder.append(productsFirstByType.get(parameters[3]).stream().
  189. limit(10).map(x -> x.toString()).collect(Collectors.joining(", ")));
  190. strBuilder.append("\n");
  191. } else {
  192. strBuilder.append(String.format("Error: Type %s does not exists\n", parameters[3]));
  193. }
  194. }
  195.  
  196. private static void addProduct(String[] parameters) {
  197. if (!productMap.containsKey(parameters[1])) {
  198. Product newProduct = new Product(parameters[1], Double.parseDouble(parameters[2]), parameters[3]);
  199. productMap.put(parameters[1], newProduct);
  200. if (!productsFirstByType.containsKey(parameters[3])) {
  201. productsFirstByType.put(parameters[3], new TreeSet<>());
  202. }
  203. TreeSet<Product> tempSet = productsFirstByType.get(parameters[3]);
  204. tempSet.add(newProduct);
  205. productsOrderedByPrice.add(newProduct);
  206. strBuilder.append(String.format("Ok: Product %s added successfully\n", parameters[1]));
  207. } else {
  208. strBuilder.append(String.format("Error: Product %s already exists\n", parameters[1]));
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement