Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.telerikacademy.oop.setmap;
- import java.math.BigDecimal;
- import java.util.*;
- public class InventoryManager {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] commands = scanner.nextLine().split(" ");
- Map<String, Item> items = new HashMap<>();
- // Map<String, Double> itemsPrice = new HashMap<>();
- StringBuilder sb = new StringBuilder("Ok: ");
- while (!commands[0].equalsIgnoreCase("end")) {
- if (commands[0].equalsIgnoreCase("add")) {
- if (items.containsKey(commands[1])) {
- // sb.append("Item " + commands[1] + " already exists");
- System.out.println("Item " + commands[1] + " already exists");
- }
- Item item = new Item(commands[1], Double.parseDouble(commands[2]), commands[3]);
- items.put(commands[1], item);
- // sb.append("Ok: Item " + commands[1] + " added successfully");
- System.out.println("Ok: Item " + commands[1] + " added successfully");
- } else if (commands[0].equalsIgnoreCase("filter")) {
- if (commands[2].equalsIgnoreCase("type")) {
- if (!items.containsKey(commands[1])) {
- // sb.append("Error: type " + commands[1] + "does not exist");
- System.out.println("Error: type " + commands[1] + "does not exist");
- }
- String commandType = commands[3];
- items.values()
- .stream()
- .sorted()
- .limit(10)
- .filter(item -> item.getType().equals(commandType))
- .forEach(x -> sb.append(x));
- if (sb.length() > 4) {
- sb.deleteCharAt(sb.length() - 1);
- sb.deleteCharAt(sb.length() - 1);
- }
- System.out.println(sb);
- }
- else if (commands[2].equalsIgnoreCase("price")) {
- if (commands.length == 7) {
- double lowRange = Double.parseDouble(commands[4]);
- double upperRange = Double.parseDouble(commands[6]);
- items.values()
- .stream()
- .sorted()
- .filter(item -> item.getPrice() <= lowRange &&
- item.getPrice() <= upperRange)
- .forEach(x -> sb.append(x));
- if (sb.length() > 4) {
- sb.deleteCharAt(sb.length() - 1);
- sb.deleteCharAt(sb.length() - 1);
- }
- System.out.println(sb);
- } else if (commands.length == 5) {
- if (commands[3].equalsIgnoreCase("from")) {
- double lowRange = Double.parseDouble(commands[4]);
- items.values()
- .stream()
- .sorted()
- .filter(item -> item.getPrice() >= lowRange)
- .forEach(x -> sb.append(x));
- if (sb.length() > 4) {
- sb.deleteCharAt(sb.length() - 1);
- sb.deleteCharAt(sb.length() - 1);
- }
- System.out.println(sb);
- } else if (commands[3].equalsIgnoreCase("to")) {
- double highRange = Double.parseDouble(commands[4]);
- items.values()
- .stream()
- .sorted()
- .filter(item -> item.getPrice() <= highRange)
- .forEach(x -> sb.append(x));
- if (sb.length() > 4) {
- sb.deleteCharAt(sb.length() - 1);
- sb.deleteCharAt(sb.length() - 1);
- }
- System.out.println(sb);
- }
- }
- }
- }
- // System.out.println(sb);
- commands = scanner.nextLine().split(" ");
- }
- }
- public static class Item implements Comparable<Item> {
- private String itemName;
- private String itemType;
- private double itemPrice;
- public Item(String name, double price, String type) {
- this.itemName = name;
- this.itemType = type;
- this.itemPrice = price;
- }
- public String getName() {
- return itemName;
- }
- public String getType() {
- return itemType;
- }
- public double getPrice() {
- return itemPrice;
- }
- @Override
- public int compareTo(Item item) {
- return Comparator.comparing(Item::getPrice)
- .thenComparing(Item::getName)
- .thenComparing(Item::getType)
- .compare(this, item);
- }
- @Override
- public String toString() {
- BigDecimal price = BigDecimal.valueOf(itemPrice).stripTrailingZeros();
- return String.format("%s(%s), ", itemType, price);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement