Skip to content

Instantly share code, notes, and snippets.

@Fluffik3666
Created May 19, 2025 19:22
Show Gist options
  • Save Fluffik3666/1e43a8f528b61442dc15c048333a6bd4 to your computer and use it in GitHub Desktop.
Save Fluffik3666/1e43a8f528b61442dc15c048333a6bd4 to your computer and use it in GitHub Desktop.
Coffee
class Coffee:
def __init__(self, amount_ml=0, capacity_ml=250):
self.amount_ml = amount_ml
self.capacity_ml = capacity_ml
@property
def is_empty(self):
return self.amount_ml <= 0
@property
def is_full(self):
return self.amount_ml >= self.capacity_ml
def consume(self, amount_ml):
if amount_ml <= 0:
raise ValueError("Amount to consume must be positive")
actual_amount = min(amount_ml, self.amount_ml)
self.amount_ml -= actual_amount
return actual_amount
class CoffeeCup:
def __init__(self, capacity_ml=250):
self.capacity_ml = capacity_ml
self.coffee = Coffee(amount_ml=0, capacity_ml=capacity_ml)
def fill(self, amount_ml=None):
if amount_ml is None:
amount_ml = self.capacity_ml - self.coffee.amount_ml
if amount_ml <= 0:
raise ValueError("Fill amount must be positive")
space_left = self.capacity_ml - self.coffee.amount_ml
actual_amount = min(amount_ml, space_left)
self.coffee.amount_ml += actual_amount
return actual_amount
def drink(self, amount_ml):
return self.coffee.consume(amount_ml)
def check_and_refill(self, default_fill_amount=None):
if self.coffee.is_empty:
return self.fill(default_fill_amount)
return 0
class CoffeeMachine:
def __init__(self, coffee_supply_ml=1000):
self.coffee_supply_ml = coffee_supply_ml
def brew(self, cup, amount_ml=None):
if self.coffee_supply_ml <= 0:
raise RuntimeError("Coffee machine is out of coffee")
if amount_ml is None:
amount_ml = cup.capacity_ml - cup.coffee.amount_ml
amount_ml = min(amount_ml, self.coffee_supply_ml)
filled_amount = cup.fill(amount_ml)
self.coffee_supply_ml -= filled_amount
return filled_amount
def refill_if_empty(cup, machine, amount_ml=None):
if cup.coffee.is_empty:
return machine.brew(cup, amount_ml)
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment