Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@babel/preset-env": "^7.24.8",
"@babel/preset-typescript": "^7.24.7",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.9.5",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "latest",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.10",
Expand Down
98 changes: 93 additions & 5 deletions src/figuresClasses.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,99 @@
export interface Figure {}
export interface Figure {
shape: 'triangle' | 'circle' | 'rectangle';
color: 'red' | 'green' | 'blue';
getArea(): number;
}

export class Triangle implements Figure {
shape: 'triangle';

color: 'red' | 'green' | 'blue';

a: number;

b: number;

c: number;

constructor(
color: 'red' | 'green' | 'blue',
a: number,
b: number,
c: number,
) {
if (a <= 0 || b <= 0 || c <= 0) {
throw new Error('The sides of a triangle must be positive numbers.');
}

if (Math.max(a, b, c) >= a + b + c - Math.max(a, b, c)) {
throw new Error(
'Sides 1, 2, and 3 cannot form a triangle.The longest' +
'side must be less than the sum of the other two sides',
);
}
this.shape = '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 Triangle implements Figure {}
export class Circle implements Figure {
shape: 'circle';

export class Circle implements Figure {}
color: 'red' | 'green' | 'blue';

export class Rectangle implements Figure {}
radius: number;

constructor(color: 'red' | 'green' | 'blue', radius: number) {
if (radius <= 0) {
throw new Error('The radius of a circle must be a positive number');
}

this.shape = 'circle';
this.color = color;
this.radius = radius;
}

getArea(): number {
return Math.floor(Math.PI * this.radius * this.radius * 100) / 100;
}
}

export class Rectangle implements Figure {
shape: 'rectangle';

color: 'red' | 'green' | 'blue';

width: number;

height: number;

constructor(color: 'red' | 'green' | 'blue', width: number, height: number) {
if (width <= 0 || height <= 0) {
throw new Error(
'The width and height of a rectangle must be positive numbers',
);
}
this.shape = 'rectangle';
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;
return `A ${figure.color} ${figure.shape} - ${figure.getArea()}`;
}
Loading