Loading...
For each class, define the attributes (data) it will hold and the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
class Drink:
def __init__(self, name, price):
self.name = name
self.price = price
@property
def price(self):
return self.price
@property
def name(self):
return self.name
class VendingMachineRow:
def init(self, drinkInformation: List[tuple(string,int,drinkQuantity)]):
self.[
Drink(drinkName,drinkPrice)
for drinkName,drinkPrice,drinkQuantity
in drinkInformation
]
class VendingMachine:
def init(self, drinksLayout):
self.quantityCounter = {}
self.paymentStrategies = [
CoinsPayment(),
CreditCardPayment()
]
for row in drinksLayout:
for drinkName,drinkPrice,drinkQuantity in row:
self.quantityCounter[drinkName] += drinkQuantity
self.vendingMachine = [
VendingMachineRow(drinksLayout[i])
for i in range(len(drinksLayout))
]
self.paymentsManager = PaymentsManager()
def placeOrder(self, row, col, paidAmount, payChoice):
drink = self.vendingMachine[row][col]
if self.quantityCounter[drink.Name]:
if self.paymentStrategies[payChoice].processPayment(paidAmount, drink.drinkPrice) >= 0:
self.quantityCounter[drink.Name] -= 1
return drink
def restock(self, restockLayout: List[list[row,col,amount]]):
for row,col,amount in restockLayout:
drink = self.vendingMachine[row][col]
self.quantityCounter[drink.drinkName] += amount
class PaymentsManager:
def init(self):
def processPayment(self, paidAmount, cost):
class CreditCardPayment(PaymentsManager):
def processPayment(self, paidAmount, cost):
# pay by credit card
class CoinsPayment(PaymentsManager):
def processPayment(self, paidAmount, cost):
# pay by coin
For payments, use strategy method to process different types of payment methods
Break down vending machine as a whole into row of drinks