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
74 changes: 68 additions & 6 deletions src/figuresClasses.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
export interface Figure {}
type Shape = `triangle` | `circle` | `rectangle`;
type Color = 'red' | 'green' | 'blue';

export class Triangle implements Figure {}
export interface Figure {
shape: Shape;
color: Color;
getArea(): number;
}

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

constructor(
public color: Color,
public a: number,
public b: number,
public c: number,
) {
if (a <= 0 || b <= 0 || c <= 0) {
throw new Error('can `t be less then 0');
}

if (a + b <= c || a + c <= b || b + c <= a) {
throw new Error('wrong triangle size');
}
}

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 {}
export class Circle implements Figure {
shape: Shape = 'circle';

export class Rectangle implements Figure {}
constructor(
public color: Color,
public radius: number,
) {
if (radius <= 0) {
throw new Error('can `t be less then 0');
}
}

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

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

constructor(
public color: Color,
public width: number,
public height: number,
) {
if (width <= 0 || height <= 0) {
throw new Error('can `t be less then 0');
}
}

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();
}
Loading