14788 - Vending Machine   

Description

The vending machine in Hsin Dorm is stuck. The vending machine owner does not want to deal with it anymore, and let you tinker with it. You, being a smart CS student, will create a VendingMachine class so that the vending machine can run better than before.

Given this code:

class VendingMachine:
    class Item:
        def __init__(self, price: int, stock: int):
            self.price = price
            self.stock = stock

    def __init__(self, balance: int):
        self.items = {} 
        self.balance = balance
        self.initial_balance = balance
    
    # Continue the class

# ----- input output handler -----
line = input().split()
T = int(line[0])
initial_balance = int(line[1])
machine = VendingMachine(initial_balance)
for _ in range(T):
    parts = input().split()
    cmd = parts[0]
    if cmd == "BUY":
        name, qty = parts[1], int(parts[2])
        machine.buy(name, qty)
    elif cmd == "RESTOCK":
        name, price, qty = parts[1], int(parts[2]), int(parts[3])
        machine.restock(name, price, qty)
print(machine)

 

You will need to implement VendingMachine class so that it has 2 methods:
1) BUY

  • Customer buys {qty} of {name}. If a customer buys more than the current available stock, print no stock.
  • Customer will NOT try to buy something that has never been stocked.

2) RESTOCK

  • You restock {name} by {qty} and update the price with {price}. This will cost (price x qty / 2) of your balance. If you are trying to restock more than you can afford, print you dont have money

You can create any additional functions that you think might be needed.

> Note: the grammar error at dont (not don't) is on purpose

Input

The first line consists of 2 integers:
- The first integer T will be the number of operations on that day
- The second integer N will be the initial balance you get.  

The next T line will be one of these commands in this format:
BUY {name} {quantity} where {name} is the name of the item that the customer buys and
    {quantity} is how many items the customer buys
RESTOCK {name} {price} {quantity} where {name} is the name of the item that you want to restock, {price} is the price that you want to sell that item and {quantity} is how much you restock that item

Output

Output the profit you get after all T operations done. (Profit = Final Balance - Initial Balance)

Sample Input  Download

Sample Output  Download

Tags




Discuss