From 3377228690d94d651128b5c723ab0c8b0485152f Mon Sep 17 00:00:00 2001 From: Leandro Lima Date: Sun, 24 May 2026 18:21:57 -0300 Subject: [PATCH 1/2] Solution --- src/figuresClasses.ts | 98 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 6 deletions(-) diff --git a/src/figuresClasses.ts b/src/figuresClasses.ts index 753711299..b0b971153 100644 --- a/src/figuresClasses.ts +++ b/src/figuresClasses.ts @@ -1,11 +1,97 @@ -export interface Figure {} +type ShapeType = 'triangle' | 'circle' | 'rectangle'; +type ColorType = 'red' | 'green' | 'blue'; -export class Triangle implements Figure {} +export interface Figure { + shape: ShapeType; + color: ColorType; + getArea(): number; +} + +export class Triangle implements Figure { + shape: ShapeType = 'triangle'; + + color: ColorType; + + a: number; + + b: number; + + c: number; + + constructor(color: ColorType, a: number, b: number, c: number) { + if (a <= 0 || b <= 0 || c <= 0) { + throw new Error('Sides must be greater than 0'); + } + + if (a >= b + c || b >= a + c || c >= a + b) { + throw new Error("Sides can't form a triangle"); + } + + this.color = color; + this.a = a; + this.b = b; + this.c = c; + } + + getArea(): number { + const p: number = (this.a + this.b + this.c) / 2; + const area: number = Math.sqrt( + p * (p - this.a) * (p - this.b) * (p - this.c), + ); + + return Math.floor(area * 100) / 100; + } +} + +export class Circle implements Figure { + shape: ShapeType = 'circle'; + + color: ColorType; -export class Circle implements Figure {} + radius: number; -export class Rectangle implements Figure {} + constructor(color: ColorType, radius: number) { + if (radius <= 0) { + throw new Error('Radius must be greater than 0'); + } + + this.color = color; + this.radius = radius; + } + + getArea(): number { + const area: number = Math.PI * this.radius * this.radius; + + return Math.floor(area * 100) / 100; + } +} + +export class Rectangle implements Figure { + shape: ShapeType = 'rectangle'; + + color: ColorType; + + width: number; + + height: number; + + constructor(color: ColorType, width: number, height: number) { + if (width <= 0 || height <= 0) { + throw new Error('Width and height must be greater than 0'); + } + + this.color = color; + this.width = width; + this.height = height; + } + + getArea(): number { + const area: number = this.width * this.height; + + return Math.floor(area * 100) / 100; + } +} -export function getInfo(figure): string { - return typeof figure; +export function getInfo(figure: Figure): string { + return `A ${figure.color} ${figure.shape} - ${figure.getArea()}`; } From 849fec6b3d61c68e3d5c74cc73dd57410d2a1f8a Mon Sep 17 00:00:00 2001 From: Leandro Lima Date: Sun, 24 May 2026 18:27:49 -0300 Subject: [PATCH 2/2] New solution --- src/figuresClasses.ts | 54 +++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/src/figuresClasses.ts b/src/figuresClasses.ts index b0b971153..eabb7f702 100644 --- a/src/figuresClasses.ts +++ b/src/figuresClasses.ts @@ -10,27 +10,19 @@ export interface Figure { export class Triangle implements Figure { shape: ShapeType = 'triangle'; - color: ColorType; - - a: number; - - b: number; - - c: number; - - constructor(color: ColorType, a: number, b: number, c: number) { + constructor( + public color: ColorType, + public a: number, + public b: number, + public c: number, + ) { if (a <= 0 || b <= 0 || c <= 0) { throw new Error('Sides must be greater than 0'); } if (a >= b + c || b >= a + c || c >= a + b) { - throw new Error("Sides can't form a triangle"); + throw new Error(`sides ${a}, ${b} and ${c} can't form a triangle`); } - - this.color = color; - this.a = a; - this.b = b; - this.c = c; } getArea(): number { @@ -46,17 +38,13 @@ export class Triangle implements Figure { export class Circle implements Figure { shape: ShapeType = 'circle'; - color: ColorType; - - radius: number; - - constructor(color: ColorType, radius: number) { + constructor( + public color: ColorType, + public radius: number, + ) { if (radius <= 0) { throw new Error('Radius must be greater than 0'); } - - this.color = color; - this.radius = radius; } getArea(): number { @@ -69,20 +57,14 @@ export class Circle implements Figure { export class Rectangle implements Figure { shape: ShapeType = 'rectangle'; - color: ColorType; - - width: number; - - height: number; - - constructor(color: ColorType, width: number, height: number) { + constructor( + public color: ColorType, + public width: number, + public height: number, + ) { if (width <= 0 || height <= 0) { throw new Error('Width and height must be greater than 0'); } - - this.color = color; - this.width = width; - this.height = height; } getArea(): number { @@ -93,5 +75,7 @@ export class Rectangle implements Figure { } export function getInfo(figure: Figure): string { - return `A ${figure.color} ${figure.shape} - ${figure.getArea()}`; + const info: string = `${figure.color} ${figure.shape}`; + + return `A ${info} - ${figure.getArea()}`; }