#C14700. Shopping Cart Operations
Shopping Cart Operations
Shopping Cart Operations
You are required to implement a shopping cart system that supports several operations. The shopping cart should allow you to add items, remove items, update item quantities, calculate the total price, apply discounts, and display the current cart contents.
The supported commands are:
- ADD name price quantity: Add an item with the given name, price (a floating‐point number) and quantity (an integer) to the cart. If the item already exists, increase its quantity accordingly.
- REMOVE name: Remove the item with the given name from the cart.
- UPDATE name quantity: Update the quantity of the given item. If the updated quantity is 0 or less, the item is removed from the cart.
- TOTAL: Print the total price of all items in the cart. Note that the total price is computed as \(\text{total} = \sum_{i} (price_i \times quantity_i)\).
- DISCOUNT d: Apply a discount of d percent on the total price and print the discounted price. The formula is given by: $$total_{discounted} = total \times \left(1-\frac{d}{100}\right).$$
- DISPLAY: Display the cart items. For each item, print a line in the format:
name: $price x quantity
(price printed with one decimal place). Then, print a final line:Total price: $X
whereX
is the total price formatted to 2 decimal places. If the cart is empty, only print the final line with total price as $0.00.
The program will receive a sequence of commands from standard input. Process each command and produce the corresponding output to standard output.
inputFormat
The input begins with an integer N (1 ≤ N ≤ 1000) on a single line, representing the number of commands. The following N lines each contain one of the commands described above. Parameters are separated by spaces.
outputFormat
For each command that produces an output (TOTAL, DISCOUNT, DISPLAY), print the corresponding result to standard output. There is no output for commands that modify the cart (such as ADD, REMOVE, and UPDATE).
## sample4
ADD Apple 1.0 3
ADD Apple 1.0 2
TOTAL
DISPLAY
5.0
Apple: $1.0 x 5
Total price: $5.00
</p>