49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
class Circle {
|
|
constructor(public readonly radius: number) {}
|
|
}
|
|
|
|
class Square {
|
|
constructor(public readonly length: number) {}
|
|
}
|
|
|
|
// Imaginons qu'on ajoute une autre forme ...
|
|
class Triangle {
|
|
constructor(public readonly length: number) {}
|
|
}
|
|
|
|
class AreaCalculator {
|
|
constructor(private readonly shapes: object[]) {}
|
|
|
|
public sum(): number {
|
|
let result = 0;
|
|
|
|
for (const shape of this.shapes) {
|
|
if (shape instanceof Circle) {
|
|
result += Math.PI * Math.pow(shape.radius, 2);
|
|
} else if (shape instanceof Square) {
|
|
result += Math.pow(shape.length, 2);
|
|
} else if (shape instanceof Triangle) {
|
|
result += Math.pow(shape.length, 2); // Calcul aire triangle
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
class SumConsoleOutputter {
|
|
constructor(private readonly calculator: AreaCalculator) {}
|
|
|
|
public output(): void {
|
|
const sum = this.calculator.sum();
|
|
console.log(`Somme des aires : ${sum}`);
|
|
}
|
|
}
|
|
|
|
const calculator = new AreaCalculator([new Circle(5), new Square(4)]);
|
|
const outputter = new SumConsoleOutputter(calculator);
|
|
|
|
outputter.output(); // affiche la somme des aires
|
|
|
|
export default {};
|