Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 76 additions & 6 deletions src/figuresClasses.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
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';

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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checklist item #3 violation: The error message could be more informative. Consider including the actual side values, e.g., 'Triangle with sides ${a}, ${b}, and ${c} cannot be formed: the longest side must be less than the sum of the other two sides'

throw new Error(`sides ${a}, ${b} and ${c} can't form a triangle`);
}
}

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';

export class Circle implements Figure {}
constructor(
public color: ColorType,
public radius: number,
) {
if (radius <= 0) {
throw new Error('Radius must be greater than 0');
}
}

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';

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');
}
}

getArea(): number {
const area: number = this.width * this.height;

return Math.floor(area * 100) / 100;
}
}

export class Rectangle implements Figure {}
export function getInfo(figure: Figure): string {
const info: string = `${figure.color} ${figure.shape}`;

export function getInfo(figure): string {
return typeof figure;
return `A ${info} - ${figure.getArea()}`;
}
Loading