Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.util.stream.Collectors;
- public class InventoryManager2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Set<Item> allItems = new HashSet<>();
- Set<String> names = new HashSet();
- while (input != "exit") {
- if (input.contains("add")) {
- String name = input.split(" ")[1];
- Double price = Double.parseDouble(input.split(" ")[2]);
- String type = input.split(" ")[3];
- Item item = new Item(name, price, type);
- if(!names.contains(name)) {
- allItems.add(item);
- names.add(name);
- System.out.printf("Ok: Item %s added successfully%n", name);
- } else {
- System.out.printf("Error: Item %s already exists%n", name);
- }
- } else if (input.contains("filter")) {
- String filterType = input.split(" ")[2];
- switch (filterType) {
- case "type":
- String itemType = input.split(" ")[3];
- String result = allItems.stream().sorted(Comparator.comparing(Item::getPrice).thenComparing(Item::getName))
- .filter(item -> item.type.equals(itemType))
- .limit(10)
- .map(Item::toString)
- .collect(Collectors.joining(", ", "Ok: ", ""));
- if(result.length() < 5) {
- System.out.printf("Error: Type %s does not exist%n", itemType);
- } else {
- System.out.println(result);
- }
- case "price":
- if (input.split(" ").length == 7) {
- Double minPrice = Double.parseDouble(input.split(" ")[4]);
- Double maxPrice = Double.parseDouble(input.split(" ")[6]);
- String result2 = allItems.stream().sorted(Comparator.comparing(Item::getPrice).thenComparing(Item::getName))
- .filter(item -> item.getPrice() >= minPrice && item.getPrice() <= maxPrice)
- .limit(10)
- .map(Item::toString)
- .collect(Collectors.joining(", ", "Ok: ", ""));
- System.out.println(result2);
- } else if(input.contains("from")) {
- Double minPrice = Double.parseDouble(input.split(" ")[4]);
- String result2 = allItems.stream().sorted(Comparator.comparing(Item::getPrice).thenComparing(Item::getName))
- .filter(item -> item.getPrice() >= minPrice)
- .limit(10)
- .map(Item::toString)
- .collect(Collectors.joining(", ", "Ok: ", ""));
- System.out.println(result2);
- } else if(input.contains("to")) {
- Double maxPrice = Double.parseDouble(input.split(" ")[4]);
- String result2 = allItems.stream().sorted(Comparator.comparing(Item::getPrice).thenComparing(Item::getName))
- .filter(item -> item.getPrice() <= maxPrice)
- .limit(10)
- .map(Item::toString)
- .collect(Collectors.joining(", ", "Ok: ", ""));
- System.out.println(result2);
- }
- }
- }
- input = scanner.nextLine();
- }
- }
- }
- class Item {
- public String name;
- public Double price;
- public String type;
- public Item(String name, Double price, String type) {
- this.name = name;
- this.price = price;
- this.type = type;
- }
- public Double getPrice() {
- return price;
- }
- public String getName() {
- return name;
- }
- @Override
- public String toString() {
- return String.format("%s(%.2f)", name, price);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment