27  OOP: Fruit

27.1 Orange & Basket (v1)

Class Diagramme: Orange & Basket

27.1.1 Orange

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.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)
        orange.basket = self

    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
basket1 = Basket(location="Storage Room 1")

# Create oranges
orange1 = Orange(weight=1.2, orchard="Orchard A", date_picked=date(2024, 12, 28))
orange2 = Orange(weight=1.4, orchard="Orchard B", date_picked=date(2024, 12, 29))

# 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)

Class Diagramme: Orange & Basket

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."""
        basket.add_orange(self)

    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:
    weight: float
    orchard: str
    date_picked: date
    basket: Optional['Basket'] = None

    def pick(self, basket: 'Basket') -> None:
        """Place the orange in a basket."""
        basket.add_orange(self)

    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):
        basket_location = self.basket.location if self.basket else None
        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)
        orange.basket = self

    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
basket1 = Basket(location="Storage Room 1")

# Create oranges
orange1 = Orange(weight=1.2, orchard="Orchard A", date_picked=date(2024, 12, 28))
orange2 = Orange(weight=1.4, orchard="Orchard B", date_picked=date(2024, 12, 29))

# 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
basket1.sell(customer="John Doe")

# 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.