|
| 1 | +//#19 ENUMERACIONES |
| 2 | +//I use GPT as a reference for concepts |
| 3 | +/* |
| 4 | + * EJERCICIO: |
| 5 | + * Empleando tu lenguaje, explora la definición del tipo de dato |
| 6 | + * que sirva para definir enumeraciones (Enum). |
| 7 | + * Crea un Enum que represente los días de la semana del lunes |
| 8 | + * al domingo, en ese orden. Con ese enumerado, crea una operación |
| 9 | + * que muestre el nombre del día de la semana dependiendo del número entero |
| 10 | + * utilizado (del 1 al 7). |
| 11 | + * |
| 12 | + * DIFICULTAD EXTRA (opcional): |
| 13 | + * Crea un pequeño sistema de gestión del estado de pedidos. |
| 14 | + * Implementa una clase que defina un pedido con las siguientes características: |
| 15 | + * - El pedido tiene un identificador y un estado. |
| 16 | + * - El estado es un Enum con estos valores: PENDIENTE, ENVIADO, ENTREGADO y CANCELADO. |
| 17 | + * - Implementa las funciones que sirvan para modificar el estado: |
| 18 | + * - Pedido enviado |
| 19 | + * - Pedido cancelado |
| 20 | + * - Pedido entregado |
| 21 | + * (Establece una lógica, por ejemplo, no se puede entregar si no se ha enviado, etc...) |
| 22 | + * - Implementa una función para mostrar un texto descriptivo según el estado actual. |
| 23 | + * - Crea diferentes pedidos y muestra cómo se interactúa con ellos. |
| 24 | + */ |
| 25 | + |
| 26 | +/* In JavaScript, Enums (short for "enumerations") are not a native feature of the language like in other programming languages (for example, TypeScript, C#, Java). However, you can simulate the behavior of enums using objects. |
| 27 | +
|
| 28 | +What are Enums? |
| 29 | +Enums are a way to define a set of named constants. They are used to represent a group of related values, making the code more readable and easier to maintain. For example, you might have an enum for the days of the week, the statuses of an order, or colors. */ |
| 30 | + |
| 31 | +window.addEventListener('load', ()=>{ |
| 32 | + const body = document.querySelector('body'); |
| 33 | + const title = document.createElement('h1'); |
| 34 | + |
| 35 | + body.style.setProperty('background', '#000'); |
| 36 | + body.style.setProperty('text-align', 'center'); |
| 37 | + |
| 38 | + title.textContent = 'Retosparaprogramadores #19.'; |
| 39 | + title.style.setProperty('font-size', '3.5vmax'); |
| 40 | + title.style.setProperty('color', '#fff'); |
| 41 | + title.style.setProperty('line-height', '100vh'); |
| 42 | + |
| 43 | + body.appendChild(title); |
| 44 | + |
| 45 | + setTimeout(()=>{ |
| 46 | + alert('Retosparaprogramadores #19. Please open the Browser Developer Tools.'); |
| 47 | + }, 2000); |
| 48 | + log( 'Retosparaprogramadores #19'); |
| 49 | +}); |
| 50 | + |
| 51 | +let log = console.log; |
| 52 | + |
| 53 | +const DaysOfWeek = { |
| 54 | + MONDAY: 'Monday', |
| 55 | + TUESDAY: 'Tuesday', |
| 56 | + WEDNESDAY: 'Wednesday', |
| 57 | + THURSDAY: 'Thursday', |
| 58 | + FRIDAY: 'Friday', |
| 59 | + SATURDAY: 'Saturday', |
| 60 | + SUNDAY: 'Sunday' |
| 61 | +}; |
| 62 | + |
| 63 | +Object.freeze(DaysOfWeek); |
| 64 | + |
| 65 | +const showDay = (day)=>{ |
| 66 | + const w_days = Object.keys(DaysOfWeek); |
| 67 | + return (w_days[day - 1])? DaysOfWeek[w_days[day - 1]] : 'You enter a invalid day, try between 1 and 7'; |
| 68 | +} |
| 69 | + |
| 70 | +log(DaysOfWeek.MONDAY); // Monday |
| 71 | +log(showDay(3)) // Wednesday |
| 72 | +//Extra Dificulty Exercise |
| 73 | + |
| 74 | +//Enum simulation |
| 75 | +const OrderStatus = { |
| 76 | + PENDING: 'Pending', |
| 77 | + SHIPPED: 'Shipped', |
| 78 | + DELIVERED: 'Delivered', |
| 79 | + CANCELED: 'Canceled' |
| 80 | +}; |
| 81 | + |
| 82 | +Object.freeze(OrderStatus); |
| 83 | + |
| 84 | +class Order{ |
| 85 | + constructor(id){ |
| 86 | + this.id = id; |
| 87 | + this.state = OrderStatus.PENDING; |
| 88 | + } |
| 89 | + |
| 90 | + setState(state){ |
| 91 | + this.state = OrderStatus[state]; |
| 92 | + } |
| 93 | + |
| 94 | + getState(){ |
| 95 | + return this.state; |
| 96 | + } |
| 97 | + |
| 98 | + shipOrder(){ |
| 99 | + if(this.state.toLowerCase() == OrderStatus.PENDING.toLowerCase()){ |
| 100 | + log('The order is Shipped'); |
| 101 | + this.setState('SHIPPED'); |
| 102 | + }else { |
| 103 | + log(`Cannot ship. Current state: ${this.state}`); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + deliverOrder(){ |
| 108 | + if(this.state.toLowerCase() == OrderStatus.SHIPPED.toLowerCase()){ |
| 109 | + log('The order is Delivered'); |
| 110 | + this.setState('DELIVERED'); |
| 111 | + }else{ |
| 112 | + log(`Cannot deliver. The order state is: ${this.state}`); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + cancelOrder(){ |
| 117 | + if(this.state.toLowerCase() == OrderStatus.DELIVERED.toLowerCase()){ |
| 118 | + log('Cannot cancel. The order has already been delivered.') |
| 119 | + } |
| 120 | + log('The order is Canceled'); |
| 121 | + this.setState('CANCELED'); |
| 122 | + } |
| 123 | + |
| 124 | + describeOrder() { |
| 125 | + log(`Order ID: ${this.id}, Current State: ${this.state}`); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +let order15 = new Order('001'); |
| 130 | +let order16 = new Order('002'); |
| 131 | +let order17 = new Order('003'); |
| 132 | + |
| 133 | +order16.shipOrder(); // The order is Shipped |
| 134 | +order15.deliverOrder(); // Cannot Deliver. The order state is: Pending |
| 135 | +order17.shipOrder(); // The order is Shipped |
| 136 | + |
| 137 | +order16.deliverOrder(); // The order is Delivered |
| 138 | +order15.shipOrder(); // The order is Shipped |
| 139 | +order17.cancelOrder(); // The order is Canceled |
| 140 | + |
| 141 | +log(order16.getState()); // Delivered |
| 142 | +log(order15.getState()); // Shipped |
| 143 | +log(order17.getState()); // Canceled |
| 144 | + |
| 145 | +order16.shipOrder() // Cannot ship. Current state: Delivered |
| 146 | + |
0 commit comments