Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
2
Indexable
class PriceLadder {
  private final Map<String, TreeMap<Double, Integer[]>> marketBook; // Holds Artist, Price, Buy/Sell quantities
  
  public PriceLadder() {
    marketBook = new HashMap<>();
  }

  public void AddOrder(AddOrder order) {
    // Retrieve the artist's orders or initialize if absent
    TreeMap<Double, Integer[]> artistOrders = marketBook.getOrDefault(order.artist, new TreeMap<>(Collections.reverseOrder()));
    
    Integer[] quantities = artistOrders.getOrDefault(order.price, new Integer[] {0, 0});
    // Determine buy or sell order
    if (order.quantity > 0) {
      quantities[0] += order.quantity; // Buy order
    } else {
      quantities[1] -= order.quantity; // Sell order
    }
    
    artistOrders.put(order.price, quantities);
    marketBook.put(order.artist, artistOrders);
  } 
    
  public void DeleteOrder(DeleteOrder delete) {
    // Iterate through artist's orders to find and remove order by ID
    TreeMap<Double, Integer[]> artistOrders = marketBook.get(delete.artist);
    if (artistOrders != null) {
      artistOrders.values().removeIf(quantities -> quantities[0] == delete.orderId || quantities[1] == delete.orderId);
      marketBook.put(delete.artist, artistOrders);
    }
  } 

  public void DeletePriceLevel(DeletePriceLevel deletePriceLevel) {
    // Retrieve the artist's orders and remove the price level
    TreeMap<Double, Integer[]> artistOrders = marketBook.get(deletePriceLevel.artist);
    if (artistOrders != null) {
      artistOrders.remove(deletePriceLevel.price);
      marketBook.put(deletePriceLevel.artist, artistOrders);
    }
  } 
      
  public void GetPriceLevels(String artist, int numberOfPriceLevels) {
    System.out.println(artist);
    TreeMap<Double, Integer[]> artistOrders = marketBook.get(artist);
    if (artistOrders != null) {
      int printedLevels = 0;
      for (Map.Entry<Double, Integer[]> entry : artistOrders.entrySet()) {
        Integer[] quantities = entry.getValue();
        if ((quantities[0] != 0 || quantities[1] != 0) && printedLevels < numberOfPriceLevels) {
          System.out.println(quantities[0] + " " + entry.getKey() + " " + quantities[1]);
          printedLevels++;
        }
      }
    }
  }  
}