from datetime import date
from typing import List
class Orange:
def __init__(self, weight: float, orchard: str, date_picked: date, basket: 'Basket' = None):
self.weight = weight
self.orchard = orchard
self.date_picked = date_picked
self.basket = basket
def __repr__(self):
return (f"Orange(weight={self.weight}, orchard='{self.orchard}', "
f"date_picked={self.date_picked}, basket_location='{self.basket.location if self.basket else None}')")
27 OOP: Fruit
27.1 Orange & Basket (v1)
27.1.1 Orange
27.1.2 Basket
class Basket:
def __init__(self, location: str):
self.location = location
self.oranges: List[Orange] = []
def add_orange(self, orange: Orange):
self.oranges.append(orange)
= self
orange.basket
def __repr__(self):
return f"Basket(location='{self.location}', oranges_count={len(self.oranges)})"
27.1.3 Usage
from datetime import date
# Create a basket
= Basket(location="Storage Room 1")
basket1
# Create oranges
= Orange(weight=1.2, orchard="Orchard A", date_picked=date(2024, 12, 28))
orange1 = Orange(weight=1.4, orchard="Orchard B", date_picked=date(2024, 12, 29))
orange2
# Add oranges to the basket
basket1.add_orange(orange1)
basket1.add_orange(orange2)
# Print details
print(basket1)
print(orange1)
print(orange2)
Basket(location='Storage Room 1', oranges_count=2)
Orange(weight=1.2, orchard='Orchard A', date_picked=2024-12-28, basket_location='Storage Room 1')
Orange(weight=1.4, orchard='Orchard B', date_picked=2024-12-29, basket_location='Storage Room 1')
27.2 Orange & Basket (v2)
27.2.1 Orange
from datetime import date
from typing import List
class Orange:
def __init__(self, weight: float, orchard: str, date_picked: date):
self.weight = weight
self.orchard = orchard
self.date_picked = date_picked
self.basket = None # Initially, the orange is not in any basket.
def pick(self, basket: 'Basket') -> None:
"""Place the orange in a basket."""
self)
basket.add_orange(
def squeeze(self) -> float:
"""Return the juice yield based on the weight of the orange.
Assume 50% of the weight is juice.
"""
return self.weight * 0.5
def __repr__(self):
return (f"Orange(weight={self.weight}, orchard='{self.orchard}', "
f"date_picked={self.date_picked}, basket_location='{self.basket.location if self.basket else None}')")
from dataclasses import dataclass, field
from datetime import date
from typing import List, Optional
@dataclass
class Orange:
float
weight: str
orchard:
date_picked: date'Basket'] = None
basket: Optional[
def pick(self, basket: 'Basket') -> None:
"""Place the orange in a basket."""
self)
basket.add_orange(
def squeeze(self) -> float:
"""Return the juice yield based on the weight of the orange.
Assume 50% of the weight is juice.
"""
return self.weight * 0.5
def __repr__(self):
= self.basket.location if self.basket else None
basket_location return (f"Orange(weight={self.weight}, orchard='{self.orchard}', "
f"date_picked={self.date_picked}, basket_location='{basket_location}')")
27.2.2 Basket
class Basket:
def __init__(self, location: str):
self.location = location
self.oranges: List[Orange] = []
def add_orange(self, orange: Orange) -> None:
"""Add an orange to the basket."""
self.oranges.append(orange)
= self
orange.basket
def sell(self, customer: str) -> None:
"""Sell all oranges in the basket to a customer."""
print(f"Sold {len(self.oranges)} oranges to {customer}.")
self.oranges.clear()
def discard(self) -> None:
"""Discard all oranges in the basket."""
print(f"Discarded {len(self.oranges)} oranges from basket at {self.location}.")
self.oranges.clear()
def __repr__(self):
return f"Basket(location='{self.location}', oranges_count={len(self.oranges)})"
27.2.3 Usage
from datetime import date
# Create a basket
= Basket(location="Storage Room 1")
basket1
# Create oranges
= Orange(weight=1.2, orchard="Orchard A", date_picked=date(2024, 12, 28))
orange1 = Orange(weight=1.4, orchard="Orchard B", date_picked=date(2024, 12, 29))
orange2
# Pick oranges into the basket
orange1.pick(basket1)
orange2.pick(basket1)
# Squeeze juice from one orange
print(f"Juice yield from orange1: {orange1.squeeze()} liters")
# Sell all oranges in the basket
="John Doe")
basket1.sell(customer
# Add and discard oranges
basket1.add_orange(orange1)
basket1.add_orange(orange2) basket1.discard()
Juice yield from orange1: 0.6 liters
Sold 2 oranges to John Doe.
Discarded 2 oranges from basket at Storage Room 1.