sunfresh

Inventory Manager

Jan 18th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.08 KB | None | 0 0
  1. Inventory Manager
  2. Objective
  3. Your task is to implement a system that helps a store owner manage his inventory. New items can be added and filtered in the market. Here are the commands that the owner needs implemented:
  4.  
  5. add {ITEM_NAME} {ITEM_PRICE} {ITEM_TYPE} – adds a new item to the system;
  6. ITEM_NAME is a character sequence and has to be unique;
  7. ITEM_PRICE is a positive floating-point number;
  8. ITEM_TYPE is a character sequence and does not have to be unique;
  9. If the item is added successfully, print: Ok: Item {ITEM_NAME} added successfully;
  10. If an item with the given name already exists, print: Error: Item {ITEM_NAME} already exists;
  11. filter by type {ITEM_TYPE} – lists the first 10 items (sorted) that have type equal to ITEM_TYPE;
  12. If the given ITEM_TYPE does not exist, print: Error: Type {ITEM_TYPE} does not exist;
  13. filter by price from {MIN_PRICE} to {MAX_PRICE} – lists the first 10 (sorted) items that have ITEM_PRICE in the given range, inclusive;
  14. filter by price from {MIN_PRICE} – lists the first 10 items (sorted) that have a greater ITEM_PRICE than the given, inclusive;
  15. filter by price to {MAX_PRICE} – lists the first 10 items (sorted) that have a smaller ITEM_PRICE that the given, inclusive;
  16. end – marks the end of the commands. No commands will follow.
  17. All items that are listed by the filter commands must be printed in the following format:
  18.  
  19. Ok: {LIST_OF_ITEMS}
  20. LIST_OF_ITEMS contains the filtered items, separated by a space and a comma (", ") and each item is represented as ITEM_NAME(ITEM_PRICE). The list should be 10 or less products. The price is formatted up two decimal places. They must also be sorted by the following criteria:
  21.  
  22. First by ITEM_PRICE, ascending
  23. Then by ITEM_NAME, ascending
  24. Lastly by ITEM_TYPE, ascending
  25. If LIST_OF_ITEMS contains no items, then print just:
  26.  
  27. Ok:
  28. Input
  29. The input data is given at the standard input. It consists of a sequence of commands, each at a separate line, ending by the command end. The commands will be valid (as described in the above list), in the specified format, within the constraints given below. There is no need to check the input data explicitly.
  30.  
  31. Output
  32. For each command from the input sequence print at the standard output its result on a single line.
  33.  
  34. Constraints
  35. All ITEM_NAME and ITEM_TYPE will consist of letters and digits only. No spaces are allowed.
  36. The total number of lines in the input will be in the range [1 … 50 000]
  37. Sample Tests
  38. Input
  39. add CowMilk 1.90 dairy
  40. add BulgarianYogurt 1.90 dairy
  41. add SmartWatch 1111.90 technology
  42. add Candy 0.90 food
  43. add Lemonade 11.90 drinks
  44. add Sweatshirt 121.90 clothes
  45. add Pants 49.90 clothes
  46. add CowMilk 1.90 dairy
  47. add Eggs 2.34 food
  48. add Cheese 5.55 dairy
  49. filter by type clothes
  50. filter by price from 1.00 to 2.00
  51. add FreshOrange 1.99 juice
  52. add Aloe 2.7 juice
  53. filter by price from 1200
  54. add Socks 2.90 clothes
  55. filter by type fruits
  56. add DellXPS13 1700.1234 technology
  57. filter by price from 1200
  58. filter by price from 1.50
  59. filter by price to 2.00
  60. filter by type clothes
  61. end
  62. Output
  63. Ok: Item CowMilk added successfully
  64. Ok: Item BulgarianYogurt added successfully
  65. Ok: Item SmartWatch added successfully
  66. Ok: Item Candy added successfully
  67. Ok: Item Lemonade added successfully
  68. Ok: Item Sweatshirt added successfully
  69. Ok: Item Pants added successfully
  70. Error: Item CowMilk already exists
  71. Ok: Item Eggs added successfully
  72. Ok: Item Cheese added successfully
  73. Ok: Pants(49.90), Sweatshirt(121.90)
  74. Ok: BulgarianYogurt(1.90), CowMilk(1.90)
  75. Ok: Item FreshOrange added successfully
  76. Ok: Item Aloe added successfully
  77. Ok:
  78. Ok: Item Socks added successfully
  79. Error: Type fruits does not exist
  80. Ok: Item DellXPS13 added successfully
  81. Ok: DellXPS13(1700.12)
  82. Ok: BulgarianYogurt(1.90), CowMilk(1.90), FreshOrange(1.99), Eggs(2.34), Aloe(2.70), Socks(2.90), Cheese(5.55), Lemonade(11.90), Pants(49.90), Sweatshirt(121.90)
  83. Ok: Candy(0.90), BulgarianYogurt(1.90), CowMilk(1.90), FreshOrange(1.99)
  84. Ok: Socks(2.90), Pants(49.90), Sweatshirt(121.90)
  85.  
  86.  
  87. import java.util.*;
  88. import java.util.stream.Collectors;
  89.  
  90. public class Main {
  91. public static void main(String[] args) {
  92. Scanner scanner = new Scanner(System.in);
  93. String[] input = scanner.nextLine().split(" ");
  94. String command = input[0];
  95. Set<Item> products = new TreeSet<>();
  96. HashMap<String, List<Item>> hashMap = new HashMap<>();
  97. StringBuilder builder = new StringBuilder();
  98. while (!command.equals("end")) {
  99. if (command.equals("add")) {
  100. String name = input[1];
  101. double price = Double.parseDouble(input[2]);
  102. String type = input[3];
  103. Item item = new Item(name, price, type);
  104. if (products.contains(item)) {
  105. System.out.printf("Error: Item %s already exists\n", name);
  106. } else {
  107. products.add(item);
  108. if (!hashMap.containsKey(type)) {
  109. hashMap.put(type, new ArrayList<>());
  110. }
  111. hashMap.get(type).add(item);
  112. System.out.printf("Ok: Item %s added successfully\n", name);
  113. }
  114. } else if (command.equals("filter")) {
  115. switch (input[2]) {
  116. case "type":
  117. String type = input[3];
  118. List<String> result = new ArrayList<>();
  119. if (hashMap.containsKey(type)) {
  120. result = hashMap.get(type)
  121. .stream().sorted().limit(10)
  122. .map(Item::toString).collect(Collectors.toList());
  123. }
  124. if (!result.isEmpty()) {
  125. System.out.print(print(result));
  126. } else {
  127. System.out.printf("Error: Type %s does not exist\n", type);
  128. }
  129. break;
  130. case "price":
  131. if (input.length == 7) {
  132. double minPrice = Double.parseDouble(input[4]);
  133. double maxPrice = Double.parseDouble(input[6]);
  134. result = products.stream()
  135. .filter(item1 -> item1.getPrice() <= maxPrice && item1.getPrice() >= minPrice)
  136. .limit(10).map(Item::toString).collect(Collectors.toList());
  137. System.out.print(print(result));
  138. } else if (input.length == 5 && input[3].equals("from")) {
  139. double minPrice = Double.parseDouble(input[4]);
  140. result = products.stream()
  141. .filter(item1 -> item1.getPrice() >= minPrice)
  142. .limit(10).map(Item::toString).collect(Collectors.toList());
  143. System.out.print(print(result));
  144. } else if (input.length == 5 && input[3].equals("to")) {
  145. double maxPrice = Double.parseDouble(input[4]);
  146. result = products.stream()
  147. .filter(item1 -> item1.getPrice() <= maxPrice)
  148. .limit(10).map(Item::toString).collect(Collectors.toList());
  149. System.out.print(print(result));
  150. }
  151. }
  152. }
  153. input = scanner.nextLine().split(" ");
  154. command = input[0];
  155. }
  156. }
  157. public static String print(List<String> result) {
  158. return String.format("Ok: %s\n",String.join(", ",result));
  159. }
  160.  
  161.  
  162. private static class Item implements Comparable<Item> {
  163. String name;
  164. double price;
  165. String type;
  166.  
  167. public Item(String name, double price, String type) {
  168. this.name = name;
  169. this.price = price;
  170. this.type = type;
  171. }
  172.  
  173. public String getName() {
  174. return name;
  175. }
  176.  
  177. public double getPrice() {
  178. return price;
  179. }
  180.  
  181. public String getType() {
  182. return type;
  183. }
  184.  
  185. @Override
  186. public boolean equals(Object o) {
  187. if (this == o) return true;
  188. if (o == null || getClass() != o.getClass()) return false;
  189. Item item = (Item) o;
  190. return name.equals(item.name) &&
  191. price == item.price &&
  192. type.equals(item.type);
  193. }
  194.  
  195. @Override
  196. public int hashCode() {
  197. return Objects.hash(name, price);
  198. }
  199.  
  200. @Override
  201. public String toString() {
  202. return String.format("%s(%.2f)", this.name, this.price);
  203. }
  204.  
  205. @Override
  206. public int compareTo(Item item) {
  207. return Comparator.comparing(Item::getPrice)
  208. .thenComparing(Item::getName)
  209. .thenComparing(Item::getType)
  210. .compare(this, item);
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment