|
| 1 | + |
| 2 | +#Region Abstraction Example |
| 3 | +# Coffee beans are imported based on harvest seasons: |
| 4 | +# - Columbia (Apr-Jul: months 4-7) |
| 5 | +# - Sumatra (Aug-Nov: months 8-11) |
| 6 | +# - South Africa (Jan, Feb, Dec: months 1, 2, 12) |
| 7 | +# - No import in March (month 3) |
| 8 | + |
| 9 | +from abc import ABC, abstractmethod |
| 10 | + |
| 11 | +class BaseService(ABC): |
| 12 | + @abstractmethod |
| 13 | + def ship_from(self) -> str: pass |
| 14 | + |
| 15 | + |
| 16 | +class SumatraService(BaseService): |
| 17 | + def ship_from(self) -> str: |
| 18 | + return 'importing coffee from Sumatra' |
| 19 | + |
| 20 | +class ColumbiaService(BaseService): |
| 21 | + def ship_from(self) -> str: |
| 22 | + return 'importing coffee from Columbia' |
| 23 | + |
| 24 | +class SouthAfricaService(BaseService): |
| 25 | + def ship_from(self) -> str: |
| 26 | + return 'importing coffee from SouthAfrica' |
| 27 | + |
| 28 | +class NoShipment(BaseService): |
| 29 | + def ship_from(self) -> str: |
| 30 | + return 'No shipment this month' |
| 31 | + |
| 32 | +class Shipment: |
| 33 | + @staticmethod |
| 34 | + def shipment_method(month: int) -> BaseService: |
| 35 | + if 4 <= month <= 7: |
| 36 | + return ColumbiaService() |
| 37 | + elif 8 < month <= 11: |
| 38 | + return SumatraService() |
| 39 | + elif month in [1, 2, 12]: |
| 40 | + return SouthAfricaService() |
| 41 | + elif month == 3: |
| 42 | + return NoShipment() |
| 43 | + else: |
| 44 | + raise ValueError('Month must be between 1 and 12.') |
| 45 | + |
| 46 | +def main(): |
| 47 | + try: |
| 48 | + month = int(input('Enter the import month (1-12): ')) |
| 49 | + service = Shipment.shipment_method(month) |
| 50 | + print(service.ship_from()) |
| 51 | + except ValueError as e: |
| 52 | + print(f'Error: {e}') |
| 53 | + |
| 54 | +main() |
0 commit comments