Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Inventory Manager
- Objective
- Your task is to implement a system that helps a store owner manage his inventory. New items can be added and filtered in the market. Here are the commands that the owner needs implemented:
- add {ITEM_NAME} {ITEM_PRICE} {ITEM_TYPE} – adds a new item to the system;
- ITEM_NAME is a character sequence and has to be unique;
- ITEM_PRICE is a positive floating-point number;
- ITEM_TYPE is a character sequence and does not have to be unique;
- If the item is added successfully, print: Ok: Item {ITEM_NAME} added successfully;
- If an item with the given name already exists, print: Error: Item {ITEM_NAME} already exists;
- filter by type {ITEM_TYPE} – lists the first 10 items (sorted) that have type equal to ITEM_TYPE;
- If the given ITEM_TYPE does not exist, print: Error: Type {ITEM_TYPE} does not exist;
- filter by price from {MIN_PRICE} to {MAX_PRICE} – lists the first 10 (sorted) items that have ITEM_PRICE in the given range, inclusive;
- filter by price from {MIN_PRICE} – lists the first 10 items (sorted) that have a greater ITEM_PRICE than the given, inclusive;
- filter by price to {MAX_PRICE} – lists the first 10 items (sorted) that have a smaller ITEM_PRICE that the given, inclusive;
- end – marks the end of the commands. No commands will follow.
- All items that are listed by the filter commands must be printed in the following format:
- Ok: {LIST_OF_ITEMS}
- LIST_OF_ITEMS contains the filtered items, separated by a space and a comma (", ") and each item is represented as ITEM_NAME(ITEM_PRICE). The list should be 10 or less products. The price is formatted up two decimal places. They must also be sorted by the following criteria:
- First by ITEM_PRICE, ascending
- Then by ITEM_NAME, ascending
- Lastly by ITEM_TYPE, ascending
- If LIST_OF_ITEMS contains no items, then print just:
- Ok:
- Input
- The input data is given at the standard input. It consists of a sequence of commands, each at a separate line, ending by the command end. The commands will be valid (as described in the above list), in the specified format, within the constraints given below. There is no need to check the input data explicitly.
- Output
- For each command from the input sequence print at the standard output its result on a single line.
- Constraints
- All ITEM_NAME and ITEM_TYPE will consist of letters and digits only. No spaces are allowed.
- The total number of lines in the input will be in the range [1 … 50 000]
- Sample Tests
- Input
- add CowMilk 1.90 dairy
- add BulgarianYogurt 1.90 dairy
- add SmartWatch 1111.90 technology
- add Candy 0.90 food
- add Lemonade 11.90 drinks
- add Sweatshirt 121.90 clothes
- add Pants 49.90 clothes
- add CowMilk 1.90 dairy
- add Eggs 2.34 food
- add Cheese 5.55 dairy
- filter by type clothes
- filter by price from 1.00 to 2.00
- add FreshOrange 1.99 juice
- add Aloe 2.7 juice
- filter by price from 1200
- add Socks 2.90 clothes
- filter by type fruits
- add DellXPS13 1700.1234 technology
- filter by price from 1200
- filter by price from 1.50
- filter by price to 2.00
- filter by type clothes
- end
- Output
- Ok: Item CowMilk added successfully
- Ok: Item BulgarianYogurt added successfully
- Ok: Item SmartWatch added successfully
- Ok: Item Candy added successfully
- Ok: Item Lemonade added successfully
- Ok: Item Sweatshirt added successfully
- Ok: Item Pants added successfully
- Error: Item CowMilk already exists
- Ok: Item Eggs added successfully
- Ok: Item Cheese added successfully
- Ok: Pants(49.90), Sweatshirt(121.90)
- Ok: BulgarianYogurt(1.90), CowMilk(1.90)
- Ok: Item FreshOrange added successfully
- Ok: Item Aloe added successfully
- Ok:
- Ok: Item Socks added successfully
- Error: Type fruits does not exist
- Ok: Item DellXPS13 added successfully
- Ok: DellXPS13(1700.12)
- Ok: BulgarianYogurt(1.90), CowMilk(1.90), FreshOrange(1.99), Eggs(2.34), Aloe(2.70), Socks(2.90), Cheese(5.55), Lemonade(11.90), Pants(49.90), Sweatshirt(121.90)
- Ok: Candy(0.90), BulgarianYogurt(1.90), CowMilk(1.90), FreshOrange(1.99)
- Ok: Socks(2.90), Pants(49.90), Sweatshirt(121.90)
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(" ");
- String command = input[0];
- Set<Item> products = new TreeSet<>();
- HashMap<String, List<Item>> hashMap = new HashMap<>();
- StringBuilder builder = new StringBuilder();
- while (!command.equals("end")) {
- if (command.equals("add")) {
- String name = input[1];
- double price = Double.parseDouble(input[2]);
- String type = input[3];
- Item item = new Item(name, price, type);
- if (products.contains(item)) {
- System.out.printf("Error: Item %s already exists\n", name);
- } else {
- products.add(item);
- if (!hashMap.containsKey(type)) {
- hashMap.put(type, new ArrayList<>());
- }
- hashMap.get(type).add(item);
- System.out.printf("Ok: Item %s added successfully\n", name);
- }
- } else if (command.equals("filter")) {
- switch (input[2]) {
- case "type":
- String type = input[3];
- List<String> result = new ArrayList<>();
- if (hashMap.containsKey(type)) {
- result = hashMap.get(type)
- .stream().sorted().limit(10)
- .map(Item::toString).collect(Collectors.toList());
- }
- if (!result.isEmpty()) {
- System.out.print(print(result));
- } else {
- System.out.printf("Error: Type %s does not exist\n", type);
- }
- break;
- case "price":
- if (input.length == 7) {
- double minPrice = Double.parseDouble(input[4]);
- double maxPrice = Double.parseDouble(input[6]);
- result = products.stream()
- .filter(item1 -> item1.getPrice() <= maxPrice && item1.getPrice() >= minPrice)
- .limit(10).map(Item::toString).collect(Collectors.toList());
- System.out.print(print(result));
- } else if (input.length == 5 && input[3].equals("from")) {
- double minPrice = Double.parseDouble(input[4]);
- result = products.stream()
- .filter(item1 -> item1.getPrice() >= minPrice)
- .limit(10).map(Item::toString).collect(Collectors.toList());
- System.out.print(print(result));
- } else if (input.length == 5 && input[3].equals("to")) {
- double maxPrice = Double.parseDouble(input[4]);
- result = products.stream()
- .filter(item1 -> item1.getPrice() <= maxPrice)
- .limit(10).map(Item::toString).collect(Collectors.toList());
- System.out.print(print(result));
- }
- }
- }
- input = scanner.nextLine().split(" ");
- command = input[0];
- }
- }
- public static String print(List<String> result) {
- return String.format("Ok: %s\n",String.join(", ",result));
- }
- private static class Item implements Comparable<Item> {
- String name;
- double price;
- String type;
- public Item(String name, double price, String type) {
- this.name = name;
- this.price = price;
- this.type = type;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public String getType() {
- return type;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Item item = (Item) o;
- return name.equals(item.name) &&
- price == item.price &&
- type.equals(item.type);
- }
- @Override
- public int hashCode() {
- return Objects.hash(name, price);
- }
- @Override
- public String toString() {
- return String.format("%s(%.2f)", this.name, this.price);
- }
- @Override
- public int compareTo(Item item) {
- return Comparator.comparing(Item::getPrice)
- .thenComparing(Item::getName)
- .thenComparing(Item::getType)
- .compare(this, item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment