Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.tasks;
- import java.io.ByteArrayInputStream;
- import java.util.*;
- /*
- -> add {ITEM_NAME} {ITEM_PRICE} {ITEM_TYPE} – adds a new item to the system;
- String ITEM_NAME (UNIQUE)
- double ITEM_PRICE
- String ITEM_TYPE
- -> filter by type {ITEM_TYPE} – lists SORTED items that have type equal to ITEM_TYPE;
- Key/Value - type
- Type (as key) -> sorted list
- Map <String, Set<Item>> itemsByType = new HashMap<>(); - to get the sorted list O(1)
- -> filter by price from {MIN_PRICE} to {MAX_PRICE} – lists the first 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;
- Set<Item> itemsByPrice = TreeSet<>();
- SORTED -> TREE!!
- First by ITEM_PRICE, ascending
- Then by ITEM_NAME, ascending
- Lastly by ITEM_TYPE, ascending
- */
- public class InventoryManager {
- private final static Map<String, Set<Item>> itemsByType = new HashMap<>();
- private final static Set<Item> itemsByPrice = new TreeSet<>();
- public static void main(String[] args) {
- String sampleInput = """
- 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
- end
- """;
- System.setIn(new ByteArrayInputStream(sampleInput.getBytes()));
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- while (!input.equals("end")) {
- String[] command = input.split(" ");
- switch (command[0]){
- case "add":
- add(command);
- break;
- }
- input = scanner.nextLine();
- }
- }
- private static void add(String[] command) {
- //add CowMilk 1.90 dairy
- String name = command[1];
- double price = Double.parseDouble(command[2]);
- String type = command[3];
- Item itemToBeAdded = new Item(name, price, type);
- if (!itemsByPrice.add(itemToBeAdded)){
- System.out.printf("Error: Item %s already exists%s", name, System.lineSeparator());
- return;
- }
- Set<Item> itemsBySpecificType = itemsByType.get(type);
- if (itemsBySpecificType == null){
- itemsBySpecificType = new TreeSet<>();
- itemsBySpecificType.add(itemToBeAdded);
- itemsByType.put(type, itemsBySpecificType);
- } else {
- itemsBySpecificType.add(itemToBeAdded);
- }
- System.out.printf("Ok: Item %s added successfully%s", name, System.lineSeparator());
- }
- 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 int compareTo(Item o) {
- return Comparator.comparing(Item::getPrice)
- .thenComparing(Item::getName)
- .thenComparing(Item::getType)
- .compare(this, o);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement