|
| 1 | +#19 { Retos para Programadores } ENUMERACIONES |
| 2 | + |
| 3 | +# Bibliography reference |
| 4 | +# Professional JavaScript for web developers by Matt Frisbie [Frisbie, Matt] (z-lib.org) |
| 5 | +#Python Notes for Professionals. 800+ pages of professional hints and tricks (GoalKicker.com) (Z-Library) |
| 6 | +# Additionally, I use GPT as a reference and sometimes to correct or generate proper comments. |
| 7 | + |
| 8 | +""" |
| 9 | + * EJERCICIO: |
| 10 | + * Empleando tu lenguaje, explora la definición del tipo de dato |
| 11 | + * que sirva para definir enumeraciones (Enum). |
| 12 | + * Crea un Enum que represente los días de la semana del lunes |
| 13 | + * al domingo, en ese orden. Con ese enumerado, crea una operación |
| 14 | + * que muestre el nombre del día de la semana dependiendo del número entero |
| 15 | + * utilizado (del 1 al 7). |
| 16 | + * |
| 17 | + * DIFICULTAD EXTRA (opcional): |
| 18 | + * Crea un pequeño sistema de gestión del estado de pedidos. |
| 19 | + * Implementa una clase que defina un pedido con las siguientes características: |
| 20 | + * - El pedido tiene un identificador y un estado. |
| 21 | + * - El estado es un Enum con estos valores: PENDIENTE, ENVIADO, ENTREGADO y CANCELADO. |
| 22 | + * - Implementa las funciones que sirvan para modificar el estado: |
| 23 | + * - Pedido enviado |
| 24 | + * - Pedido cancelado |
| 25 | + * - Pedido entregado |
| 26 | + * (Establece una lógica, por ejemplo, no se puede entregar si no se ha enviado, etc...) |
| 27 | + * - Implementa una función para mostrar un texto descriptivo según el estado actual. |
| 28 | + * - Crea diferentes pedidos y muestra cómo se interactúa con ellos. |
| 29 | +
|
| 30 | +""" |
| 31 | + |
| 32 | +# Function to simulate console.log |
| 33 | +log = print |
| 34 | +# Days of the week as a constant dictionary |
| 35 | +class DaysOfWeek: |
| 36 | + MONDAY = 'Monday' |
| 37 | + TUESDAY = 'Tuesday' |
| 38 | + WEDNESDAY = 'Wednesday' |
| 39 | + THURSDAY = 'Thursday' |
| 40 | + FRIDAY = 'Friday' |
| 41 | + SATURDAY = 'Saturday' |
| 42 | + SUNDAY = 'Sunday' |
| 43 | + |
| 44 | +# Function to show the day based on input |
| 45 | +def show_day(day): |
| 46 | + w_days = [DaysOfWeek.MONDAY, DaysOfWeek.TUESDAY, DaysOfWeek.WEDNESDAY, |
| 47 | + DaysOfWeek.THURSDAY, DaysOfWeek.FRIDAY, DaysOfWeek.SATURDAY, |
| 48 | + DaysOfWeek.SUNDAY] |
| 49 | + return w_days[day - 1] if 1 <= day <= 7 else 'You entered an invalid day, try between 1 and 7' |
| 50 | + |
| 51 | +log(DaysOfWeek.MONDAY) # Monday |
| 52 | +log(show_day(3)) # Wednesday |
| 53 | + |
| 54 | +# Extra Difficulty Exercise |
| 55 | + |
| 56 | +# Enum simulation |
| 57 | +class OrderStatus: |
| 58 | + PENDING = 'Pending' |
| 59 | + SHIPPED = 'Shipped' |
| 60 | + DELIVERED = 'Delivered' |
| 61 | + CANCELED = 'Canceled' |
| 62 | + |
| 63 | +# Order class |
| 64 | +class Order: |
| 65 | + def __init__(self, id): |
| 66 | + self.id = id |
| 67 | + self.state = OrderStatus.PENDING |
| 68 | + |
| 69 | + def set_state(self, state): |
| 70 | + self.state = getattr(OrderStatus, state) |
| 71 | + |
| 72 | + def get_state(self): |
| 73 | + return self.state |
| 74 | + |
| 75 | + def ship_order(self): |
| 76 | + if self.state.lower() == OrderStatus.PENDING.lower(): |
| 77 | + log('The order is Shipped') |
| 78 | + self.set_state('SHIPPED') |
| 79 | + else: |
| 80 | + log(f'Cannot ship. Current state: {self.state}') |
| 81 | + |
| 82 | + def deliver_order(self): |
| 83 | + if self.state.lower() == OrderStatus.SHIPPED.lower(): |
| 84 | + log('The order is Delivered') |
| 85 | + self.set_state('DELIVERED') |
| 86 | + else: |
| 87 | + log(f'Cannot deliver. The order state is: {self.state}') |
| 88 | + |
| 89 | + def cancel_order(self): |
| 90 | + if self.state.lower() == OrderStatus.DELIVERED.lower(): |
| 91 | + log('Cannot cancel. The order has already been delivered.') |
| 92 | + log('The order is Canceled') |
| 93 | + self.set_state('CANCELED') |
| 94 | + |
| 95 | + def describe_order(self): |
| 96 | + log(f'Order ID: {self.id}, Current State: {self.state}') |
| 97 | + |
| 98 | +# Creating order instances |
| 99 | +order15 = Order('001') |
| 100 | +order16 = Order('002') |
| 101 | +order17 = Order('003') |
| 102 | + |
| 103 | +order16.ship_order() # The order is Shipped |
| 104 | +order15.deliver_order() # Cannot deliver. The order state is: Pending |
| 105 | +order17.ship_order() # The order is Shipped |
| 106 | + |
| 107 | +order16.deliver_order() # The order is Delivered |
| 108 | +order15.ship_order() # The order is Shipped |
| 109 | +order17.cancel_order() # The order is Canceled |
| 110 | + |
| 111 | +log(order16.get_state()) # Delivered |
| 112 | +log(order15.get_state()) # Shipped |
| 113 | +log(order17.get_state()) # Canceled |
| 114 | + |
| 115 | +order16.ship_order() # Cannot ship. Current state: Delivered |
0 commit comments