From d5b6b11415331346d86c47d81ed40e01d41c1caa Mon Sep 17 00:00:00 2001 From: chlodav <153681219+chlodav@users.noreply.github.com> Date: Sat, 23 May 2026 22:21:32 +0300 Subject: [PATCH 1/2] add task solution --- src/figuresClasses.ts | 100 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 6 deletions(-) diff --git a/src/figuresClasses.ts b/src/figuresClasses.ts index 753711299..0fa9c2042 100644 --- a/src/figuresClasses.ts +++ b/src/figuresClasses.ts @@ -1,11 +1,99 @@ -export interface Figure {} +export type FigureColor = 'red' | 'green' | 'blue'; -export class Triangle implements Figure {} +export interface Figure { + shape: 'triangle' | 'circle' | 'rectangle'; + color: FigureColor; + getArea(): number; +} + +function validatePositiveLength(value: number, name: string): void { + if (value <= 0) { + throw new Error(`${name} must be greater than 0`); + } +} + +export class Triangle implements Figure { + shape: 'triangle' = 'triangle'; + + color: FigureColor; + + a: number; + + b: number; + + c: number; + + constructor(color: FigureColor, a: number, b: number, c: number) { + validatePositiveLength(a, 'a'); + validatePositiveLength(b, 'b'); + validatePositiveLength(c, 'c'); + + const sortedSides = [a, b, c].sort((x, y) => y - x); + const longestSide = sortedSides[0]; + const sumOfOtherSides = sortedSides[1] + sortedSides[2]; + + if (longestSide >= sumOfOtherSides) { + 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 { + const s = (this.a + this.b + this.c) / 2; + const area = Math.sqrt(s * (s - this.a) * (s - this.b) * (s - this.c)); + + return Math.floor(area * 100) / 100; + } +} -export class Circle implements Figure {} +export class Circle implements Figure { + shape: 'circle' = 'circle'; -export class Rectangle implements Figure {} + color: FigureColor; + + radius: number; + + constructor(color: FigureColor, radius: number) { + validatePositiveLength(radius, 'radius'); + + this.color = color; + this.radius = radius; + } + + getArea(): number { + const area = Math.PI * this.radius * this.radius; + + return Math.floor(area * 100) / 100; + } +} + +export class Rectangle implements Figure { + shape: 'rectangle' = 'rectangle'; + + color: FigureColor; + + width: number; + + height: number; + + constructor(color: FigureColor, width: number, height: number) { + validatePositiveLength(width, 'width'); + validatePositiveLength(height, 'height'); + + this.color = color; + this.width = width; + this.height = height; + } + + getArea(): number { + return Math.floor(this.width * this.height * 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 71e87629c27fcee76d730a311bb50d5b574d2ac9 Mon Sep 17 00:00:00 2001 From: chlodav <153681219+chlodav@users.noreply.github.com> Date: Sat, 23 May 2026 22:26:24 +0300 Subject: [PATCH 2/2] add task solution --- src/figuresClasses.ts | 48 ++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/src/figuresClasses.ts b/src/figuresClasses.ts index 0fa9c2042..7956f26d4 100644 --- a/src/figuresClasses.ts +++ b/src/figuresClasses.ts @@ -15,15 +15,12 @@ function validatePositiveLength(value: number, name: string): void { export class Triangle implements Figure { shape: 'triangle' = 'triangle'; - color: FigureColor; - - a: number; - - b: number; - - c: number; - - constructor(color: FigureColor, a: number, b: number, c: number) { + constructor( + public color: FigureColor, + public a: number, + public b: number, + public c: number, + ) { validatePositiveLength(a, 'a'); validatePositiveLength(b, 'b'); validatePositiveLength(c, 'c'); @@ -35,11 +32,6 @@ export class Triangle implements Figure { if (longestSide >= sumOfOtherSides) { 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 { @@ -53,15 +45,11 @@ export class Triangle implements Figure { export class Circle implements Figure { shape: 'circle' = 'circle'; - color: FigureColor; - - radius: number; - - constructor(color: FigureColor, radius: number) { + constructor( + public color: FigureColor, + public radius: number, + ) { validatePositiveLength(radius, 'radius'); - - this.color = color; - this.radius = radius; } getArea(): number { @@ -74,19 +62,13 @@ export class Circle implements Figure { export class Rectangle implements Figure { shape: 'rectangle' = 'rectangle'; - color: FigureColor; - - width: number; - - height: number; - - constructor(color: FigureColor, width: number, height: number) { + constructor( + public color: FigureColor, + public width: number, + public height: number, + ) { validatePositiveLength(width, 'width'); validatePositiveLength(height, 'height'); - - this.color = color; - this.width = width; - this.height = height; } getArea(): number {