Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package set_and_Map_CodingTasks_1;
- import java.util.*;
- import java.util.stream.Collectors;
- public class newSolution {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Item> inventory = new HashMap<>();
- String input;
- while (!"end".equals(input = scanner.nextLine())) {
- String[] parameters = input.split(" ");
- String command = parameters[0];
- if (command.equals("add")) {
- String itemName = parameters[1];
- double itemPrice = Double.parseDouble(parameters[2]);
- String itemType = parameters[3];
- if (inventory.containsKey(itemName)) {
- System.out.printf("Error: Item %s already exists%n", itemName);
- } else {
- Item item = new Item(itemName, itemPrice, itemType);
- inventory.put(itemName, item);
- System.out.printf("Ok: Item %s added successfully%n", itemName);
- }
- } else if (command.equals("filter")) {
- String filtrationType = parameters[2];
- if (filtrationType.equals("price")) {
- String priceType = parameters[3];
- if (priceType.equals("to")) {
- double maxPrice = Double.parseDouble(parameters[4]);
- List<Item> filteredItems = new ArrayList<>();
- inventory.entrySet().stream()
- .filter(e -> e.getValue().price <= maxPrice)
- .forEach(e -> {
- filteredItems.add(e.getValue());
- StringBuilder sb = new StringBuilder();
- sb.append("Ok: ");
- sb.append(String.join(", ", output));
- System.out.println(sb);
- } else if (priceType.equals("from")) {
- if (inputLine.length == 5) {
- // min
- double minValue = Double.parseDouble(inputLine[4]);
- List<String> output = new ArrayList<>();
- inventoryPrices.entrySet().stream()
- .sorted(Map.Entry.comparingByValue())
- .filter(e -> e.getValue() >= minValue)
- .limit(10)
- .forEach(e -> {
- output.add(String.format("%s(%.2f)", e.getKey(), inventoryPrices.get(e.getKey())));
- });
- StringBuilder sb = new StringBuilder();
- sb.append("Ok: ");
- sb.append(String.join(", ", output));
- System.out.println(sb);
- } else {
- // size 6 - min i max
- Double minValue = Double.parseDouble(inputLine[4]);
- Double maxValue = Double.parseDouble(inputLine[6]);
- List<String> output = new ArrayList<>();
- inventoryPrices.entrySet().stream()
- .sorted(Map.Entry.comparingByValue())
- .filter(e -> e.getValue() >= minValue && e.getValue() <= maxValue)
- .limit(10)
- .forEach(e -> {
- output.add(String.format("%s(%.2f)", e.getKey(), inventoryPrices.get(e.getKey())));
- });
- StringBuilder sb = new StringBuilder();
- sb.append("Ok: ");
- sb.append(String.join(", ", output));
- System.out.println(sb);
- }
- }
- } else if (filtrationType.equals("type")) {
- String typeToFilter = parameters[3];
- if (inventory.values().stream().noneMatch(item -> item.type.equals(typeToFilter))) {
- System.out.printf("Error: Type %s does not exist%n", typeToFilter);
- } else {
- StringBuilder sb = new StringBuilder();
- sb.append("Ok:");
- inventory.entrySet().stream()
- .filter(e -> e.getValue().type.equals(typeToFilter))
- .limit(10)
- .forEach(e -> sb.append(String.format(" %s,", e.getValue().toString())));
- System.out.println(sb.substring(0, sb.length() - 2));
- }
- }
- }
- }
- }
- }
- static class Item {
- String name;
- double price;
- String type;
- public Item(String name, double price, String type) {
- this.name = name;
- this.price = price;
- this.type = type;
- }
- @Override
- public String toString() {
- return String.format("%s (%.2f)", name, price);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement