OOP
Q1: Vending Machine
In this question you'll create a vending machine that only outputs a single product and provides change when needed.
Create a class called
VendingMachine
that represents a vending machine for some product. AVendingMachine
object returns strings describing its interactions. Remember to match exactly the strings in the doctests -- including punctuation and spacing!Fill in the
VendingMachine
class, adding attributes and methods as appropriate, such that its behavior matches the following doctests:
According to the wiki, a vending machine is an automated machine that provides items to consumers after cash, or other forms of payment are inserted into the machine or otherwise made
这里说的 vending machine 其实也就是常见的自动售货机, 我们要为其写一个类实现应该有的功能. 这里的要求更为简单, 因为我们要实现的自动售货机只有一样东西可以卖, 而且每次只会买一件. 注意下面要实现函数的逻辑即可:
-
restock
, 补充商品, 这个比较简单 -
add_funds
- 商品有库存的时候才会记住你付了多少钱
- 商品没有库存的时候直接退款给你
-
vend
- 商品有库存的时候
- 你支付的钱
- 你支付的钱
- 商品有库存的时候