Delivery Billing

 avatar
unknown
java
a month ago
805 B
6
Indexable
public class DeliveryBillingService {
    // driverId -> hourly rate
    Map<Integer, Double> driverRateMap;
    // [driverId, startTime, endTime]
    List<int[]> deliveries;
    // running total
    double totalCost; 

    public DeliveryBillingService() {
        driverRateMap = new HashMap<>();
        deliveries = new ArrayList<>();
    }

    public void addDriver(int driverId, double rate) {
        driverRateMap.put(driverId, rate);
    }

    public void recordDelivery(int driverId, int start, int end) {
        deliveries.add(new int[]{driverId, start, end});
        // force double division
        totalCost = totalCost + (end - start) / 3600.0 * driverRateMap.get(driverId);
    }

    // time complexity must be O(1)
    public double getTotalCost() {
        return totalCost;
    }
}
Editor is loading...
Leave a Comment