Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- 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())) {
- String[] inputLine = input.split(" ");
- String command = inputLine[0];
- if (command.equals("add")) {
- String itemName = inputLine[1];
- Double itemPrice = Double.parseDouble(inputLine[2]);
- String itemType = inputLine[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[2];
- if (filtrationType.equals("price")) {
- String priceType = inputLine[3];
- if (priceType.equals("to")) {
- Double maxPrice = Double.parseDouble(inputLine[4]);
- List<String> output = new ArrayList<>();
- inventoryPrices.entrySet().stream()
- .sorted(Map.Entry.comparingByValue())
- .limit(10)
- .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.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 = inputLine[3];
- List<String> output = new ArrayList<>();
- if (!inventoryTypes.containsValue(typeToFilter)) {
- System.out.printf("Error: Type %s does not exist%n", typeToFilter);
- } else {
- Map<String, Double> localMap = new HashMap<>();
- inventoryTypes.entrySet().stream()
- .filter(e -> e.getValue().equals(typeToFilter))
- .limit(10)
- .forEach(e -> {
- localMap.put(e.getKey(), inventoryPrices.get(e.getKey()));
- });
- localMap.entrySet()
- .stream()
- .sorted(Map.Entry.comparingByValue())
- .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