Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class InventoryManagerSolution {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(" ");
- String command = input[0];
- Set<Item> products = new TreeSet<>();
- HashMap<String, List<Item>> hashMap = new HashMap<>();
- while (!command.equals("end")) {
- if (command.equals("add")) {
- String name = input[1];
- double price = Double.parseDouble(input[2]);
- String type = input[3];
- Item item = new Item(name, price, type);
- if (products.contains(item)) {
- System.out.printf("Error: Item %s already exists\n", name);
- } else {
- products.add(item);
- if (!hashMap.containsKey(type)) {
- hashMap.put(type, new ArrayList<>());
- }
- hashMap.get(type).add(item);
- System.out.printf("Ok: Item %s added successfully\n", name);
- }
- } else if (command.equals("filter")) {
- switch (input[2]) {
- case "type":
- String type = input[3];
- List<String> result = new ArrayList<>();
- if (hashMap.containsKey(type)) {
- result = hashMap.get(type)
- .stream().sorted().limit(10)
- .map(Item::toString).collect(Collectors.toList());
- }
- if (!result.isEmpty()) {
- System.out.print(print(result));
- } else {
- System.out.printf("Error: Type %s does not exist\n", type);
- }
- break;
- case "price":
- if (input.length == 7) {
- double minPrice = Double.parseDouble(input[4]);
- double maxPrice = Double.parseDouble(input[6]);
- result = products.stream()
- .filter(item1 -> item1.getPrice() <= maxPrice && item1.getPrice() >= minPrice)
- .limit(10).map(Item::toString).collect(Collectors.toList());
- System.out.print(print(result));
- } else if (input.length == 5 && input[3].equals("from")) {
- double minPrice = Double.parseDouble(input[4]);
- result = products.stream()
- .filter(item1 -> item1.getPrice() >= minPrice)
- .limit(10).map(Item::toString).collect(Collectors.toList());
- System.out.print(print(result));
- } else if (input.length == 5 && input[3].equals("to")) {
- double maxPrice = Double.parseDouble(input[4]);
- result = products.stream()
- .filter(item1 -> item1.getPrice() <= maxPrice)
- .limit(10).map(Item::toString).collect(Collectors.toList());
- System.out.print(print(result));
- }
- }
- }
- input = scanner.nextLine().split(" ");
- command = input[0];
- }
- }
- public static String print(List<String> result) {
- return String.format("Ok: %s\n", String.join(", ", result));
- }
- private static class Item implements Comparable<Item> {
- String name;
- double price;
- String type;
- public Item(String name, double price, String type) {
- this.name = name;
- this.price = price;
- this.type = type;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public String getType() {
- return type;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Item item = (Item) o;
- return name.equals(item.name) &&
- price == item.price &&
- type.equals(item.type);
- }
- @Override
- public String toString() {
- return String.format("%s(%.2f)", this.name, this.price);
- }
- @Override
- public int compareTo(Item item) {
- return Comparator.comparing(Item::getPrice)
- .thenComparing(Item::getName)
- .thenComparing(Item::getType)
- .compare(this, item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment