|
| 1 | +# ╔═════════════════════════════════════╗ |
| 2 | +# ║ Autor: Kenys Alvarado ║ |
| 3 | +# ║ GitHub: https://github.com/Kenysdev ║ |
| 4 | +# ║ 2024 - Python ║ |
| 5 | +# ╚═════════════════════════════════════╝ |
| 6 | + |
| 7 | +# ----------------------------------- |
| 8 | +# * ENUMERACIONES |
| 9 | +# ----------------------------------- |
| 10 | +# - Una enumeración en Python es un conjunto de nombres simbólicos(miembros) |
| 11 | +# que están vinculados a valores únicos. |
| 12 | +# - se puede iterar y acceder. |
| 13 | + |
| 14 | +# Mas info: https://docs.python.org/3/library/enum.html |
| 15 | + |
| 16 | +""" |
| 17 | + * EJERCICIO 1: |
| 18 | + * Empleando tu lenguaje, explora la definición del tipo de dato |
| 19 | + * que sirva para definir enumeraciones (Enum). |
| 20 | + * Crea un Enum que represente los días de la semana del lunes |
| 21 | + * al domingo, en ese orden. Con ese enumerado, crea una operación |
| 22 | + * que muestre el nombre del día de la semana dependiendo del número entero |
| 23 | + * utilizado (del 1 al 7). |
| 24 | +""" |
| 25 | + |
| 26 | +from enum import Enum |
| 27 | + |
| 28 | +class Weekday(Enum): |
| 29 | + MONDAY = 1 |
| 30 | + TUESDAY = 2 |
| 31 | + WEDNESDAY = 3 |
| 32 | + THURSDAY = 4 |
| 33 | + FRIDAY = 5 |
| 34 | + SATURDAY = 6 |
| 35 | + SUNDAY = 7 |
| 36 | + |
| 37 | +def get_day(num: int) -> str: |
| 38 | + try: |
| 39 | + day = Weekday(num) |
| 40 | + return day.name |
| 41 | + except ValueError: |
| 42 | + return "'o'" |
| 43 | + |
| 44 | +def get_num_day(day: str) -> int: |
| 45 | + try: |
| 46 | + return Weekday[day].value |
| 47 | + except KeyError: |
| 48 | + return 0 |
| 49 | + |
| 50 | +print(get_day(7)) |
| 51 | +print(get_day(3)) |
| 52 | +print(get_day(8)) |
| 53 | + |
| 54 | +print(get_num_day("TUESDAY")) |
| 55 | +print(get_num_day("FRIDAY")) |
| 56 | +print(get_num_day("abc")) |
| 57 | + |
| 58 | +""" |
| 59 | + * EJERCICIO 2: |
| 60 | + * Crea un pequeño sistema de gestión del estado de pedidos. |
| 61 | + * Implementa una clase que defina un pedido con las siguientes características: |
| 62 | + * - El pedido tiene un identificador y un estado. |
| 63 | + * - El estado es un Enum con estos valores: PENDIENTE, ENVIADO, ENTREGADO y CANCELADO. |
| 64 | + * - Implementa las funciones que sirvan para modificar el estado: |
| 65 | + * - Pedido enviado |
| 66 | + * - Pedido cancelado |
| 67 | + * - Pedido entregado |
| 68 | + * (Establece una lógica, por ejemplo, no se puede entregar si no se ha enviado, etc...) |
| 69 | + * - Implementa una función para mostrar un texto descriptivo según el estado actual. |
| 70 | + * - Crea diferentes pedidos y muestra cómo se interactúa con ellos. |
| 71 | +""" |
| 72 | + |
| 73 | +print("\nEJERCICIO 2:") |
| 74 | + |
| 75 | +class Status(Enum): |
| 76 | + PENDING = 1 |
| 77 | + SHIPPED = 2 |
| 78 | + DELIVERED = 3 |
| 79 | + CANCELED = 4 |
| 80 | + |
| 81 | +class Order: |
| 82 | + def __init__(self, identifier): |
| 83 | + self.identifier = identifier |
| 84 | + self._status = Status.PENDING |
| 85 | + |
| 86 | + def send(self): |
| 87 | + print("\nEnviar:") |
| 88 | + if self._status == Status.PENDING: |
| 89 | + self._status = Status.SHIPPED |
| 90 | + print(self.info()) |
| 91 | + else: |
| 92 | + print(f"invalid operation, ", self.info()) |
| 93 | + |
| 94 | + def cancelled(self): |
| 95 | + print("\nCancelar:") |
| 96 | + if self._status == Status.PENDING: |
| 97 | + self._status = Status.CANCELED |
| 98 | + print(self.info()) |
| 99 | + else: |
| 100 | + print(f"invalid operation, ", self.info()) |
| 101 | + |
| 102 | + def delivered(self): |
| 103 | + print("\nEntregar:") |
| 104 | + if self._status == Status.SHIPPED: |
| 105 | + self._status = Status.DELIVERED |
| 106 | + print(self.info()) |
| 107 | + else: |
| 108 | + print(f"invalid operation, ", self.info()) |
| 109 | + |
| 110 | + def info(self): |
| 111 | + return (f"{self.identifier} is {self._status.name}") |
| 112 | + |
| 113 | +# "Creación de pedidos" |
| 114 | +libro1 = Order("libro1") |
| 115 | +libro2 = Order("libro2") |
| 116 | +libro3 = Order("libro2") |
| 117 | + |
| 118 | +# Positivas: |
| 119 | +print("\n-----\nOperaciones exitosas:\n-----") |
| 120 | +libro1.send() |
| 121 | +libro1.delivered() |
| 122 | +libro2.cancelled() |
| 123 | + |
| 124 | +# negativas: |
| 125 | +print("\n-----\nOperaciones invalidas:\n-----") |
| 126 | +libro3.delivered() |
| 127 | +libro2.cancelled() |
| 128 | +libro1.send() |
| 129 | + |
0 commit comments