#K62552. Store Checkout Simulator
Store Checkout Simulator
Store Checkout Simulator
This problem simulates a basic checkout process in a store. You are required to implement a system that can add items, remove items, calculate the total cost of a given list of items, add discount codes, and apply discounts to given totals.
The discount is applied by reducing the total cost by a percentage. For example, a discount of 10% on a total cost of \(T\) will compute the new cost as \(T - T \times \frac{10}{100}\).
Your program will receive a sequence of commands and should output the results of the calculate_cost
and apply_discount
commands accordingly.
inputFormat
The first line of the input contains an integer \(n\), which indicates the number of commands to process. Each of the following \(n\) lines contains a command in one of the following formats:
add_item <item_name> <price>
— Adds an item with the specified price to the store. Price is a positive float.remove_item <item_name>
— Removes the item if it exists.calculate_cost <item_name_1> <item_name_2> ... <item_name_k>
— Calculates the total cost for the given list of items. If an item has not been added, it is skipped. Print the resulting total cost.add_discount <discount_code> <percentage>
— Adds a discount code with a given percentage (an integer between 1 and 100).apply_discount <total_cost> <discount_code>
— Applies the discount to the given total cost if the code exists, and prints the discounted total. If the discount code is invalid, print the original total cost.
All outputs should be printed on separate lines as they occur.
outputFormat
For every calculate_cost
and apply_discount
command, output the result on a separate line. The output should be a floating-point number representing the calculated cost (after discount if applicable).
7
add_item apple 1.0
add_item banana 0.5
calculate_cost apple banana
add_discount WELCOME10 10
apply_discount 1.5 WELCOME10
remove_item banana
calculate_cost apple banana
1.5
1.35
1.0
</p>