Advertisement
chavdardim1990

Untitled

Aug 30th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. package com.company.tasks;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.util.*;
  5.  
  6.  
  7. /*
  8. -> add {ITEM_NAME} {ITEM_PRICE} {ITEM_TYPE} – adds a new item to the system;
  9. String ITEM_NAME (UNIQUE)
  10. double ITEM_PRICE
  11. String ITEM_TYPE
  12.  
  13. -> filter by type {ITEM_TYPE} – lists SORTED items that have type equal to ITEM_TYPE;
  14.  
  15. Key/Value - type
  16. Type (as key) -> sorted list
  17. Map <String, Set<Item>> itemsByType = new HashMap<>(); - to get the sorted list O(1)
  18.  
  19. -> filter by price from {MIN_PRICE} to {MAX_PRICE} – lists the first SORTED items that have ITEM_PRICE in the given range, inclusive;
  20. -> filter by price from {MIN_PRICE} – lists the first 10 items (sorted) that have a greater ITEM_PRICE than the given, INCLUSIVE;
  21. -> filter by price to {MAX_PRICE} – lists the first 10 items (sorted) that have a smaller ITEM_PRICE that the given, inclusive;
  22. Set<Item> itemsByPrice = TreeSet<>();
  23.  
  24. SORTED -> TREE!!
  25.  
  26. First by ITEM_PRICE, ascending
  27. Then by ITEM_NAME, ascending
  28. Lastly by ITEM_TYPE, ascending
  29. */
  30. public class InventoryManager {
  31. private final static Map<String, Set<Item>> itemsByType = new HashMap<>();
  32. private final static Set<Item> itemsByPrice = new TreeSet<>();
  33.  
  34. public static void main(String[] args) {
  35. String sampleInput = """
  36. add CowMilk 1.90 dairy
  37. add BulgarianYogurt 1.90 dairy
  38. add SmartWatch 1111.90 technology
  39. add Candy 0.90 food
  40. add Lemonade 11.90 drinks
  41. add Sweatshirt 121.90 clothes
  42. add Pants 49.90 clothes
  43. add CowMilk 1.90 dairy
  44. add Eggs 2.34 food
  45. add Cheese 5.55 dairy
  46. end
  47. """;
  48. System.setIn(new ByteArrayInputStream(sampleInput.getBytes()));
  49.  
  50. Scanner scanner = new Scanner(System.in);
  51. String input = scanner.nextLine();
  52.  
  53. while (!input.equals("end")) {
  54. String[] command = input.split(" ");
  55. switch (command[0]){
  56. case "add":
  57. add(command);
  58. break;
  59. }
  60.  
  61. input = scanner.nextLine();
  62. }
  63. }
  64.  
  65. private static void add(String[] command) {
  66. //add CowMilk 1.90 dairy
  67. String name = command[1];
  68. double price = Double.parseDouble(command[2]);
  69. String type = command[3];
  70. Item itemToBeAdded = new Item(name, price, type);
  71.  
  72. if (!itemsByPrice.add(itemToBeAdded)){
  73. System.out.printf("Error: Item %s already exists%s", name, System.lineSeparator());
  74. return;
  75. }
  76.  
  77. Set<Item> itemsBySpecificType = itemsByType.get(type);
  78.  
  79. if (itemsBySpecificType == null){
  80. itemsBySpecificType = new TreeSet<>();
  81. itemsBySpecificType.add(itemToBeAdded);
  82. itemsByType.put(type, itemsBySpecificType);
  83. } else {
  84. itemsBySpecificType.add(itemToBeAdded);
  85. }
  86. System.out.printf("Ok: Item %s added successfully%s", name, System.lineSeparator());
  87. }
  88.  
  89. static class Item implements Comparable<Item> {
  90. String name;
  91. double price;
  92. String type;
  93.  
  94. public Item(String name, double price, String type) {
  95. this.name = name;
  96. this.price = price;
  97. this.type = type;
  98. }
  99.  
  100. public String getName() {
  101. return name;
  102. }
  103.  
  104. public double getPrice() {
  105. return price;
  106. }
  107.  
  108. public String getType() {
  109. return type;
  110. }
  111.  
  112. @Override
  113. public int compareTo(Item o) {
  114. return Comparator.comparing(Item::getPrice)
  115. .thenComparing(Item::getName)
  116. .thenComparing(Item::getType)
  117. .compare(this, o);
  118. }
  119. }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement