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
81 changes: 75 additions & 6 deletions src/figuresClasses.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,80 @@
export interface Figure {}
export type Shape = 'triangle' | 'circle' | 'rectangle';
export type Color = 'red' | 'green' | 'blue';
export interface Figure {
shape: Shape;
color: Color;
getArea(): number;
}

export class Triangle implements Figure {
readonly shape: Shape = 'triangle';

constructor(
public readonly color: Color,
private readonly a: number,
private readonly b: number,
private readonly c: number,
) {
if (a <= 0 || b <= 0 || c <= 0) {
throw new Error('The side length is less than or equal to zero.');
}

const maxSide = Math.max(a, b, c);
const sumLengthTwoSides = a + b + c - maxSide;

if (maxSide >= sumLengthTwoSides) {
throw new Error('Length of the longer side: invalid value');
Comment on lines +25 to +26
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to checklist item #3, error messages should be detailed. Consider: "The longest side must be less than the sum of the other two sides" instead of the generic "invalid value".

}
}

getArea(): number {
const s: number = (this.a + this.b + this.c) / 2;

return (
Math.floor(
Math.sqrt(s * (s - this.a) * (s - this.b) * (s - this.c)) * 100,
) / 100
);
}
}

export class Circle implements Figure {
readonly shape: Shape = 'circle';

export class Triangle implements Figure {}
constructor(
public readonly color: Color,
private readonly radius: number,
) {
if (this.radius <= 0) {
throw new Error('The radius is equal to zero.');
Comment on lines +48 to +49
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The condition checks this.radius <= 0 but the message says "equal to zero". For consistency, consider: "The radius must be greater than zero" to cover both zero and negative cases.

}
}

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

export class Circle implements Figure {}
export class Rectangle implements Figure {
readonly shape: Shape = 'rectangle';

constructor(
public readonly color: Color,
private readonly width: number,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Typo: 'hight' should be 'height' to match the task description. This typo propagates to lines 65 and 71.

private readonly hight: number,
) {
if (this.width <= 0 || this.hight <= 0) {
throw new Error('The side length is less than or equal to zero.');
}
}

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

export class Rectangle implements Figure {}
export function getInfo(figure: Figure): string {
const calcArea = figure.getArea();

export function getInfo(figure): string {
return typeof figure;
return `A ${figure.color} ${figure.shape} - ${calcArea}`;
}
Loading