Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
2.4 kB
1
Indexable
Never
class PriceLadder {
    private final Map<String, Map<Double, Map<Integer, Integer>>> marketData;
    private final Map<Integer, Double> orderPrices;

    public PriceLadder() {
        marketData = new HashMap<>();
        orderPrices = new HashMap<>();
    }

    public void AddOrder(AddOrder order) {
        marketData.putIfAbsent(order.artist, new TreeMap<>(Collections.reverseOrder()));
        Map<Double, Map<Integer, Integer>> artistData = marketData.get(order.artist);
        artistData.putIfAbsent(order.price, new HashMap<>());
        
        artistData.get(order.price).put(order.orderId, order.quantity);
        orderPrices.put(order.orderId, order.price);
    } 

    public void DeleteOrder(DeleteOrder delete) {
        if (orderPrices.containsKey(delete.orderId)) {
            double price = orderPrices.get(delete.orderId);
            Map<Double, Map<Integer, Integer>> artistData = marketData.get(delete.artist);
            if (artistData.containsKey(price)) {
                artistData.get(price).remove(delete.orderId);
                if (artistData.get(price).isEmpty()) {
                    artistData.remove(price);
                }
            }
            orderPrices.remove(delete.orderId);
        }
    } 

    public void DeletePriceLevel(DeletePriceLevel deletePriceLevel) {
        if (marketData.containsKey(deletePriceLevel.artist)) {
            Map<Double, Map<Integer, Integer>> artistData = marketData.get(deletePriceLevel.artist);
            artistData.remove(deletePriceLevel.price);
        }
    }

    public void GetPriceLevels(String artist, int numberOfPriceLevels) {
        System.out.println(artist);

        if (marketData.containsKey(artist)) {
            int counter = 0;
            for (Map.Entry<Double, Map<Integer, Integer>> entry : marketData.get(artist).entrySet()) {
                if (counter >= numberOfPriceLevels) break;
                double price = entry.getKey();
                int buyQty = 0, sellQty = 0;
                for (int qty : entry.getValue().values()) {
                    if (qty > 0) {
                        buyQty += qty;
                    } else {
                        sellQty -= qty;
                    }
                }

                System.out.println(buyQty + " " + (price % 1 == 0 ? String.format("%.0f", price) : price) + " " + sellQty);
                counter++;
            }
        }
    }
}