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 {}
export interface Figure {
shape: 'triangle' | 'circle' | 'rectangle';
color: 'red' | 'green' | 'blue';
getArea(): number;
}

type Color = 'red' | 'green' | 'blue';

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

constructor(
public color: Color,
public a: number,
public b: number,
public c: number,
) {
if (a <= 0 || b <= 0 || c <= 0) {
throw new Error('All triangle sides must be greater than 0');
}

const max = Math.max(a, b, c);
const sum = a + b + c;

if (max >= sum - max) {
throw new Error(
'Triangle sides cannot form a valid triangle: longest side must be less than sum of other two sides',

Check failure on line 27 in src/figuresClasses.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 109. Maximum allowed is 80
);
}
}

getArea(): number {
const p = (this.a + this.b + this.c) / 2;
const area = Math.sqrt(p * (p - this.a) * (p - this.b) * (p - this.c));

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

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

constructor(
public color: Color,
public radius: number,
) {
if (radius <= 0) {
throw new Error('Circle radius must be greater than 0');
}
}

getArea(): number {
const s = Math.PI * this.radius ** 2;

export class Triangle implements Figure {}
return Math.floor(s * 100) / 100;
}
}

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

export class Circle implements Figure {}
constructor(
public color: Color,
public width: number,
public height: number,
) {
if (width <= 0 || height <= 0) {
throw new Error('Rectangle width and height must be greater than 0');
}
}

export class Rectangle implements Figure {}
getArea(): number {
const s = this.width * this.height;

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

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