|
| 1 | +// Ejercicio |
| 2 | + |
| 3 | +/* Incorrecto */ |
| 4 | + |
| 5 | +class Animal { |
| 6 | + constructor(sound) { |
| 7 | + this.sound = sound |
| 8 | + } |
| 9 | + |
| 10 | + makeSound () { |
| 11 | + switch (this.sound) { |
| 12 | + case 'meau': |
| 13 | + console.log(`The animal makes meau!!!`); |
| 14 | + break; |
| 15 | + case 'woof': |
| 16 | + console.log(`The animal makes woof!!!`); |
| 17 | + break; |
| 18 | + default: |
| 19 | + console.log('Please incert an animal sound!'); |
| 20 | + break; |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | +const animal = new Animal('meau') |
| 25 | +animal.makeSound() |
| 26 | + |
| 27 | +/* Correcto */ |
| 28 | + |
| 29 | +class Person { |
| 30 | + constructor(name, occupation) { |
| 31 | + this.name = name |
| 32 | + this.occupation = occupation |
| 33 | + } |
| 34 | + |
| 35 | + getOccupation() { |
| 36 | + return this.occupation.getOccupation() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class PersonOccupation { |
| 41 | + getOccupation() {} |
| 42 | +} |
| 43 | + |
| 44 | +class IndustrialEngineer extends PersonOccupation { |
| 45 | + getOccupation() { |
| 46 | + return 'I am an industrial engineer.' |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const engineer = new Person('Yojan', new IndustrialEngineer()) |
| 51 | +console.log(`Hola mi nombre es ${engineer.name} soy ${engineer.getOccupation()}`) |
| 52 | + |
| 53 | +// Extra |
| 54 | + |
| 55 | +class Operation { |
| 56 | + constructor() { |
| 57 | + if (new.target === Operation) { |
| 58 | + throw new TypeError("No puedes instanciar la clase abstracta 'Operation' directamente") |
| 59 | + } |
| 60 | + } |
| 61 | + execute() { |
| 62 | + throw new Error("Método 'execute()' debe ser implementado en la clase hija") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class Addition extends Operation{ |
| 67 | + constructor(a, b) { |
| 68 | + super() |
| 69 | + this.a = a |
| 70 | + this.b = b |
| 71 | + } |
| 72 | + execute() { |
| 73 | + return this.a + this.b |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class Subtract extends Operation{ |
| 78 | + constructor(a, b) { |
| 79 | + super() |
| 80 | + this.a = a |
| 81 | + this.b = b |
| 82 | + } |
| 83 | + execute() { |
| 84 | + return this.a - this.b |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class Multiply extends Operation{ |
| 89 | + constructor(a, b) { |
| 90 | + super() |
| 91 | + this.a = a |
| 92 | + this.b = b |
| 93 | + } |
| 94 | + execute() { |
| 95 | + return this.a * this.b |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class Divide extends Operation{ |
| 100 | + constructor(a, b) { |
| 101 | + super() |
| 102 | + this.a = a |
| 103 | + this.b = b |
| 104 | + } |
| 105 | + execute() { |
| 106 | + return this.a / this.b |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +class Calculator { |
| 111 | + constructor() { |
| 112 | + this.operations = {} |
| 113 | + } |
| 114 | + addOperation(name, operation) { |
| 115 | + this.operations[name] = operation |
| 116 | + } |
| 117 | + |
| 118 | + calculate(name) { |
| 119 | + if (!this.operations[name]) { |
| 120 | + throw new Error(`Operación ${name} no es valida`) |
| 121 | + } |
| 122 | + return this.operations[name].execute() |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +const calculadora = new Calculator() |
| 127 | +calculadora.addOperation('addition', new Addition(10, 2)) |
| 128 | +calculadora.addOperation('subtraction', new Subtract(5, 2)) |
| 129 | +calculadora.addOperation('multiplication', new Multiply(9, 2)) |
| 130 | + |
| 131 | +console.log(calculadora.calculate('subtraction')) |
| 132 | +console.log(calculadora.calculate('addition')) |
| 133 | +console.log(calculadora.calculate('multiplication')) |
0 commit comments