Simulated Coffee Machine with OOP!
Add GIF demo
This commit is contained in:
parent
2cb3f14710
commit
f462cc5ae0
9 changed files with 167 additions and 2 deletions
39
Day 16/oop-coffee-machine/money_machine.py
Normal file
39
Day 16/oop-coffee-machine/money_machine.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
class MoneyMachine:
|
||||
|
||||
CURRENCY = "$"
|
||||
|
||||
COIN_VALUES = {
|
||||
"quarters": 0.25,
|
||||
"dimes": 0.10,
|
||||
"nickles": 0.05,
|
||||
"pennies": 0.01
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.profit = 0
|
||||
self.money_received = 0
|
||||
|
||||
def report(self):
|
||||
"""Prints the current profit"""
|
||||
print(f"Money: {self.CURRENCY}{self.profit}")
|
||||
|
||||
def process_coins(self):
|
||||
"""Returns the total calculated from coins inserted."""
|
||||
print("Please insert coins.")
|
||||
for coin in self.COIN_VALUES:
|
||||
self.money_received += int(input(f"How many {coin}?: ")) * self.COIN_VALUES[coin]
|
||||
return self.money_received
|
||||
|
||||
def make_payment(self, cost):
|
||||
"""Returns True when payment is accepted, or False if insufficient."""
|
||||
self.process_coins()
|
||||
if self.money_received >= cost:
|
||||
change = round(self.money_received - cost, 2)
|
||||
print(f"Here is {self.CURRENCY}{change} in change.")
|
||||
self.profit += cost
|
||||
self.money_received = 0
|
||||
return True
|
||||
else:
|
||||
print("Sorry that's not enough money. Money refunded.")
|
||||
self.money_received = 0
|
||||
return False
|
||||
Loading…
Add table
Add a link
Reference in a new issue