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 Inventory_Manager {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input;
- Map<String, Double> inventoryPrices = new TreeMap<>();
- Map<String, String> inventoryTypes = new TreeMap<>();
- while (!"end".equals(input = scanner.nextLine())) {
- ArrayList<String> inputLine = Arrays.stream(input.split(" "))
- .collect(Collectors.toCollection(ArrayList::new));
- String command = inputLine.get(0);
- if (command.equals("add")) {
- String itemName = inputLine.get(1);
- Double itemPrice = Double.parseDouble(inputLine.get(2));
- String itemType = inputLine.get(3);
- if (inventoryPrices.containsKey(itemName)) {
- System.out.printf("Error: Item %s already exists%n", itemName);
- } else {
- inventoryPrices.put(itemName, itemPrice);
- inventoryTypes.put(itemName, itemType);
- System.out.printf("Ok: Item %s added successfully%n", itemName);
- }
- } else if (command.equals("filter")) {
- String filtrationType = inputLine.get(2);
- if (filtrationType.equals("price")) {
- String priceType = inputLine.get(3);
- if (priceType.equals("to")) {
- Double maxPrice = Double.parseDouble(inputLine.get(4));
- List<String> output = new ArrayList<>();
- inventoryPrices.entrySet().stream()
- .filter(e -> e.getValue() <= maxPrice)
- .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 (priceType.equals("from")) {
- if (inputLine.size() == 5) {
- // min
- double minValue = Double.parseDouble(inputLine.get(4));
- List<String> output = new ArrayList<>();
- inventoryPrices.entrySet().stream()
- .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.get(4));
- Double maxValue = Double.parseDouble(inputLine.get(5));
- List<String> output = new ArrayList<>();
- inventoryPrices.entrySet().stream()
- .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 = inputLine.get(3);
- List<String> output = new ArrayList<>();
- if (!inventoryTypes.containsValue(typeToFilter)) {
- System.out.printf("Error: Type %s does not exist%n", typeToFilter);
- } else {
- inventoryTypes.entrySet().stream()
- .filter(e -> e.getValue().equals(typeToFilter))
- .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);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement