#C12937. Apply Discounts to Orders
Apply Discounts to Orders
Apply Discounts to Orders
You are given two sets of data: a list of orders and a list of discount offers.
Each order is described by four fields: order_id (an integer), customer_id (an integer), order_value (a numeric value) and order_date (a date in the format \(YYYY-MM-DD\)).
Each offer is described by three fields: customer_id (an integer), offer_discount (a numeric discount) and offer_validity_date (a date in the format \(YYYY-MM-DD\)).
Your task is to update each order by applying the best available discount for that customer, under the following rules:
- If the order's order_date is less than or equal to the offer's offer_validity_date, then the discount can be applied.
- For each customer, if multiple valid offers exist, the one with the maximum discount is chosen.
- The final order value is computed as \(\max(\text{order_value} - \text{offer_discount},\;0)\).
- If the order data cannot be parsed properly (for example, non-numeric order_value or invalid date) then that order is ignored.
Special cases:
- If the input for orders or offers is not provided (indicated by a value of -1), output the message "Orders or offers list not provided".
- If orders or offers are provided but the count is 0, output "No orders or offers available".
inputFormat
The input is read from stdin in the following format:
<n> (order details for n orders, one per line) <m> (offer details for m offers, one per line)
Here, <n> is an integer representing the number of orders, followed by n lines where each line contains four fields separated by space:
order_id customer_id order_value order_date
Then, <m> is an integer representing the number of offers, followed by m lines where each line contains three fields separated by space:
customer_id offer_discount offer_validity_date
Notes:
- If the very first integer (n) or the integer for offers (m) is -1, it indicates that the orders or offers list is not provided.
- The date fields follow the format YYYY-MM-DD. You can assume that a valid date string has exactly 10 characters and the proper '-' positions.
outputFormat
The output should be written to stdout:
- If the orders or offers list is not provided, print exactly:
Orders or offers list not provided
- If the orders or offers list is empty (i.e. n == 0 or m == 0), print exactly:
No orders or offers available
- Otherwise, for each valid order, print a single line with the updated order details in the same order as input. Each line should contain:
order_id customer_id updated_order_value order_date
, with the updated order_value computed as \(\max(\text{order_value} - best_offer\, , 0)\).
-1
-1
Orders or offers list not provided