|
| 1 | +// Wrong way |
| 2 | +// interface Shape { |
| 3 | +// type: string; |
| 4 | +// } |
| 5 | + |
| 6 | +// class ShapeCalculator { |
| 7 | +// calculateArea(shape: Shape): number { |
| 8 | +// if (shape.type === 'circle') { |
| 9 | +// return Math.PI * Math.pow((shape as Circle).radius, 2); |
| 10 | +// } else if (shape.type === 'square') { |
| 11 | +// return Math.pow((shape as Square).side, 2); |
| 12 | +// } else { |
| 13 | +// throw new Error('Shape not supported'); |
| 14 | +// } |
| 15 | +// } |
| 16 | +// } |
| 17 | + |
| 18 | +// const calculator = new ShapeCalculator(); |
| 19 | +// console.log(calculator.calculateArea({ type: 'circle', radius: 5 } as Circle)); |
| 20 | +// console.log(calculator.calculateArea({ type: 'square', side: 4 } as Square)); |
| 21 | + |
| 22 | +// Correct way |
| 23 | +abstract class ShapeBase { |
| 24 | + abstract calculateArea(): number; |
| 25 | +} |
| 26 | + |
| 27 | +class Circle extends ShapeBase { |
| 28 | + radius: number; |
| 29 | + |
| 30 | + constructor(radius: number) { |
| 31 | + super(); |
| 32 | + this.radius = radius; |
| 33 | + } |
| 34 | + |
| 35 | + calculateArea(): number { |
| 36 | + return Math.PI * Math.pow(this.radius, 2); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class Square extends ShapeBase { |
| 41 | + side: number; |
| 42 | + |
| 43 | + constructor(side: number) { |
| 44 | + super(); |
| 45 | + this.side = side; |
| 46 | + } |
| 47 | + |
| 48 | + calculateArea(): number { |
| 49 | + return Math.pow(this.side, 2); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function printArea(shape: ShapeBase): void { |
| 54 | + console.log(`Area: ${shape.calculateArea()}`); |
| 55 | +} |
| 56 | + |
| 57 | +const circle = new Circle(5); |
| 58 | +const square = new Square(4); |
| 59 | +printArea(circle); |
| 60 | +printArea(square); |
| 61 | + |
| 62 | +// ** Extra Exercise ** // |
| 63 | +abstract class Operation { |
| 64 | + abstract calculate(a: number, b: number): number; |
| 65 | +} |
| 66 | + |
| 67 | +class Addition extends Operation { |
| 68 | + calculate(a: number, b: number): number { |
| 69 | + return a + b; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class Subtraction extends Operation { |
| 74 | + calculate(a: number, b: number): number { |
| 75 | + return a - b; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class Multiplication extends Operation { |
| 80 | + calculate(a: number, b: number): number { |
| 81 | + return a * b; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class Division extends Operation { |
| 86 | + calculate(a: number, b: number): number { |
| 87 | + if (b === 0) throw new Error('Division by zero is not allowed'); |
| 88 | + return a / b; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class Pow extends Operation { |
| 93 | + calculate(a: number, b: number): number { |
| 94 | + return Math.pow(a, b); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +class Calculator { |
| 99 | + private operations: { [key: string]: Operation }; |
| 100 | + |
| 101 | + constructor() { |
| 102 | + this.operations = {}; |
| 103 | + } |
| 104 | + |
| 105 | + addOperation(name: string, operation: Operation): void { |
| 106 | + this.operations[name] = operation; |
| 107 | + } |
| 108 | + |
| 109 | + calculate(name: string, a: number, b: number): number { |
| 110 | + const operation = this.operations[name]; |
| 111 | + if (!operation) throw new Error(`Operation "${name}" not supported`); |
| 112 | + return operation.calculate(a, b); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +const calculator2 = new Calculator(); |
| 117 | +calculator2.addOperation('addition', new Addition()); |
| 118 | +calculator2.addOperation('subtraction', new Subtraction()); |
| 119 | +calculator2.addOperation('multiplication', new Multiplication()); |
| 120 | +calculator2.addOperation('division', new Division()); |
| 121 | +calculator2.addOperation('pow', new Pow()); |
| 122 | + |
| 123 | +console.log(calculator2.calculate('addition', 2, 10)); |
| 124 | +console.log(calculator2.calculate('subtraction', 2, 10)); |
| 125 | +console.log(calculator2.calculate('multiplication', 2, 10)); |
| 126 | +console.log(calculator2.calculate('division', 10, 5)); |
| 127 | +console.log(calculator2.calculate('pow', 10, 2)); |
0 commit comments