sunfresh

Inventory Manager

Jan 18th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1.  
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. String[] input = scanner.nextLine().split(" ");
  10. String command = input[0];
  11. Set<Item> products = new TreeSet<>();
  12. HashMap<String, List<Item>> hashMap = new HashMap<>();
  13. StringBuilder builder = new StringBuilder();
  14. while (!command.equals("end")) {
  15. if (command.equals("add")) {
  16. String name = input[1];
  17. double price = Double.parseDouble(input[2]);
  18. String type = input[3];
  19. Item item = new Item(name, price, type);
  20. if (products.contains(item)) {
  21. System.out.printf("Error: Item %s already exists\n", name);
  22. } else {
  23. products.add(item);
  24. if (!hashMap.containsKey(type)) {
  25. hashMap.put(type, new ArrayList<>());
  26. }
  27. hashMap.get(type).add(item);
  28. System.out.printf("Ok: Item %s added successfully\n", name);
  29. }
  30. } else if (command.equals("filter")) {
  31. switch (input[2]) {
  32. case "type":
  33. String type = input[3];
  34. List<String> result = new ArrayList<>();
  35. if (hashMap.containsKey(type)) {
  36. result = hashMap.get(type)
  37. .stream().sorted().limit(10)
  38. .map(Item::toString).collect(Collectors.toList());
  39. }
  40. if (!result.isEmpty()) {
  41. System.out.print(print(result));
  42. } else {
  43. System.out.printf("Error: Type %s does not exist\n", type);
  44. }
  45. break;
  46. case "price":
  47. if (input.length == 7) {
  48. double minPrice = Double.parseDouble(input[4]);
  49. double maxPrice = Double.parseDouble(input[6]);
  50. result = products.stream()
  51. .filter(item1 -> item1.getPrice() <= maxPrice && item1.getPrice() >= minPrice)
  52. .limit(10).map(Item::toString).collect(Collectors.toList());
  53. System.out.print(print(result));
  54. } else if (input.length == 5 && input[3].equals("from")) {
  55. double minPrice = Double.parseDouble(input[4]);
  56. result = products.stream()
  57. .filter(item1 -> item1.getPrice() >= minPrice)
  58. .limit(10).map(Item::toString).collect(Collectors.toList());
  59. System.out.print(print(result));
  60. } else if (input.length == 5 && input[3].equals("to")) {
  61. double maxPrice = Double.parseDouble(input[4]);
  62. result = products.stream()
  63. .filter(item1 -> item1.getPrice() <= maxPrice)
  64. .limit(10).map(Item::toString).collect(Collectors.toList());
  65. System.out.print(print(result));
  66. }
  67. }
  68. }
  69. input = scanner.nextLine().split(" ");
  70. command = input[0];
  71. }
  72. }
  73. public static String print(List<String> result) {
  74. return String.format("Ok: %s\n",String.join(", ",result));
  75. }
  76.  
  77.  
  78. private static class Item implements Comparable<Item> {
  79. String name;
  80. double price;
  81. String type;
  82.  
  83. public Item(String name, double price, String type) {
  84. this.name = name;
  85. this.price = price;
  86. this.type = type;
  87. }
  88.  
  89. public String getName() {
  90. return name;
  91. }
  92.  
  93. public double getPrice() {
  94. return price;
  95. }
  96.  
  97. public String getType() {
  98. return type;
  99. }
  100.  
  101. @Override
  102. public boolean equals(Object o) {
  103. if (this == o) return true;
  104. if (o == null || getClass() != o.getClass()) return false;
  105. Item item = (Item) o;
  106. return name.equals(item.name) &&
  107. price == item.price &&
  108. type.equals(item.type);
  109. }
  110.  
  111. @Override
  112. public int hashCode() {
  113. return Objects.hash(name, price);
  114. }
  115.  
  116. @Override
  117. public String toString() {
  118. return String.format("%s(%.2f)", this.name, this.price);
  119. }
  120.  
  121. @Override
  122. public int compareTo(Item item) {
  123. return Comparator.comparing(Item::getPrice)
  124. .thenComparing(Item::getName)
  125. .thenComparing(Item::getType)
  126. .compare(this, item);
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment