-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathready_state.py
37 lines (30 loc) · 1.33 KB
/
ready_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from vending_machine_state import VendingMachineState
from product import Product
from coin import Coin
from note import Note
class ReadyState(VendingMachineState):
def __init__(self, vending_machine):
self.vending_machine = vending_machine
def select_product(self, product: Product):
print("Product already selected. Please make payment.")
def insert_coin(self, coin: Coin):
self.vending_machine.add_coin(coin)
print(f"Coin inserted: {coin.name}")
self.check_payment_status()
def insert_note(self, note: Note):
self.vending_machine.add_note(note)
print(f"Note inserted: {note.name}")
self.check_payment_status()
def dispense_product(self):
print("Please make payment first.")
def return_change(self):
change = self.vending_machine.total_payment - self.vending_machine.selected_product.price
if change > 0:
print(f"Change returned: ${change:.2f}")
self.vending_machine.reset_payment()
else:
print("No change to return.")
self.vending_machine.set_state(self.vending_machine.idle_state)
def check_payment_status(self):
if self.vending_machine.total_payment >= self.vending_machine.selected_product.price:
self.vending_machine.set_state(self.vending_machine.dispense_state)