shaunaa

Untitled

Jan 11th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class InventoryManager2 {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. String input = scanner.nextLine();
  10. Set<Item> allItems = new HashSet<>();
  11. Set<String> names = new HashSet();
  12.  
  13.  
  14. while (input != "exit") {
  15. if (input.contains("add")) {
  16. String name = input.split(" ")[1];
  17. Double price = Double.parseDouble(input.split(" ")[2]);
  18. String type = input.split(" ")[3];
  19. Item item = new Item(name, price, type);
  20.  
  21. if(!names.contains(name)) {
  22. allItems.add(item);
  23. names.add(name);
  24. System.out.printf("Ok: Item %s added successfully%n", name);
  25. } else {
  26. System.out.printf("Error: Item %s already exists%n", name);
  27. }
  28.  
  29. } else if (input.contains("filter")) {
  30. String filterType = input.split(" ")[2];
  31.  
  32. switch (filterType) {
  33. case "type":
  34. String itemType = input.split(" ")[3];
  35. String result = allItems.stream().sorted(Comparator.comparing(Item::getPrice).thenComparing(Item::getName))
  36. .filter(item -> item.type.equals(itemType))
  37. .limit(10)
  38. .map(Item::toString)
  39. .collect(Collectors.joining(", ", "Ok: ", ""));
  40.  
  41. if(result.length() < 5) {
  42. System.out.printf("Error: Type %s does not exist%n", itemType);
  43. } else {
  44. System.out.println(result);
  45. }
  46. case "price":
  47. if (input.split(" ").length == 7) {
  48. Double minPrice = Double.parseDouble(input.split(" ")[4]);
  49. Double maxPrice = Double.parseDouble(input.split(" ")[6]);
  50. String result2 = allItems.stream().sorted(Comparator.comparing(Item::getPrice).thenComparing(Item::getName))
  51. .filter(item -> item.getPrice() >= minPrice && item.getPrice() <= maxPrice)
  52. .limit(10)
  53. .map(Item::toString)
  54. .collect(Collectors.joining(", ", "Ok: ", ""));
  55. System.out.println(result2);
  56.  
  57. } else if(input.contains("from")) {
  58. Double minPrice = Double.parseDouble(input.split(" ")[4]);
  59. String result2 = allItems.stream().sorted(Comparator.comparing(Item::getPrice).thenComparing(Item::getName))
  60. .filter(item -> item.getPrice() >= minPrice)
  61. .limit(10)
  62. .map(Item::toString)
  63. .collect(Collectors.joining(", ", "Ok: ", ""));
  64. System.out.println(result2);
  65.  
  66.  
  67. } else if(input.contains("to")) {
  68. Double maxPrice = Double.parseDouble(input.split(" ")[4]);
  69. String result2 = allItems.stream().sorted(Comparator.comparing(Item::getPrice).thenComparing(Item::getName))
  70. .filter(item -> item.getPrice() <= maxPrice)
  71. .limit(10)
  72. .map(Item::toString)
  73. .collect(Collectors.joining(", ", "Ok: ", ""));
  74. System.out.println(result2);
  75.  
  76. }
  77. }
  78. }
  79.  
  80. input = scanner.nextLine();
  81. }
  82. }
  83. }
  84.  
  85. class Item {
  86. public String name;
  87. public Double price;
  88. public String type;
  89.  
  90. public Item(String name, Double price, String type) {
  91. this.name = name;
  92. this.price = price;
  93. this.type = type;
  94. }
  95.  
  96. public Double getPrice() {
  97. return price;
  98. }
  99.  
  100. public String getName() {
  101. return name;
  102. }
  103.  
  104. @Override
  105. public String toString() {
  106. return String.format("%s(%.2f)", name, price);
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment