diff --git a/rpgsaga/saga/src/RabbitClass.ts b/rpgsaga/saga/src/Classes/AnimalSuperclass.ts similarity index 75% rename from rpgsaga/saga/src/RabbitClass.ts rename to rpgsaga/saga/src/Classes/AnimalSuperclass.ts index 35c247ad..8c5b5766 100644 --- a/rpgsaga/saga/src/RabbitClass.ts +++ b/rpgsaga/saga/src/Classes/AnimalSuperclass.ts @@ -1,8 +1,9 @@ -export class Rabbit { +export abstract class Animal { private _name: string; - private _age: number; + protected _age: number; private _color: string; private _gender: string; + constructor(name: string, age: number, color: string, gender: string) { this._name = name; this._age = age; @@ -10,18 +11,18 @@ export class Rabbit { this._gender = gender; } + abstract sound(): string; + + abstract set age(age: number); + public set name(name: string) { this._name = name; } - public set age(age: number) { - if (age < 0 || age > 9) { - throw new Error('Invalid age'); - } - this._age = age; - } + public set color(color: string) { - this.color = color; + this._color = color; } + public set gender(gender: string) { if (gender.toLowerCase() === 'male') { this._gender = 'Male'; @@ -31,16 +32,18 @@ export class Rabbit { throw new Error('Invalid gender'); } } - public get age() { - return this._age; - } + + abstract get age(); + public get name() { return this._name; } - public get gender() { - return this._gender; - } + public get color() { return this._color; } + + public get gender() { + return this._gender; + } } diff --git a/rpgsaga/saga/src/Classes/ParrotClass.ts b/rpgsaga/saga/src/Classes/ParrotClass.ts new file mode 100644 index 00000000..c1965340 --- /dev/null +++ b/rpgsaga/saga/src/Classes/ParrotClass.ts @@ -0,0 +1,38 @@ +import { Animal } from './AnimalSuperclass'; + +export class Parrot extends Animal { + private _vocabulary: number; + constructor(name: string, age: number, color: string, gender: string, vocabulary: number) { + super(name, age, color, gender); + this._vocabulary = vocabulary; + } + + public set age(age: number) { + if (age < 0 || age > 52) { + throw new Error('Invalid age'); + } + this.age = age; + } + + public set vocabulary(vocabulary: number) { + if (vocabulary < 0 || vocabulary > 20) { + throw new Error('Impossible vocabulary'); + } + this._vocabulary = vocabulary; + } + + public get vocabulary() { + return this._vocabulary; + } + + public get age() { + return this._age; + } + + sound(): string { + return 'Chik-Chirik'; + } + toString(): string { + return `Parrot named ${this.name}, is ${this.age} years old,${this.gender} and ${this.color}, know ${this.vocabulary} words`; + } +} diff --git a/rpgsaga/saga/src/Classes/RabbitClass.ts b/rpgsaga/saga/src/Classes/RabbitClass.ts new file mode 100644 index 00000000..3bddacf9 --- /dev/null +++ b/rpgsaga/saga/src/Classes/RabbitClass.ts @@ -0,0 +1,38 @@ +import { Animal } from './AnimalSuperclass'; + +export class Rabbit extends Animal { + private _jump: number; + constructor(name: string, age: number, color: string, gender: string, jump: number) { + super(name, age, color, gender); + this._jump = jump; + } + + public set age(age: number) { + if (age < 0 || age > 5) { + throw new Error('Invalid age'); + } + this.age = age; + } + + public set jump(jump: number) { + if (jump < 0 || jump > 2.7) { + throw new Error('Impossible jump'); + } + this._jump = jump; + } + + public get age() { + return this._age; + } + + public get jump() { + return this._jump; + } + + sound(): string { + return 'Purr-Purr'; + } + toString(): string { + return `Rabbit named ${this.name}, is ${this.age} years old,${this.gender} and ${this.color}, can jump ${this.jump} meters`; + } +} diff --git a/rpgsaga/saga/src/Rabbit.ts b/rpgsaga/saga/src/Rabbit.ts index 1594e66f..7a9ddc4d 100644 --- a/rpgsaga/saga/src/Rabbit.ts +++ b/rpgsaga/saga/src/Rabbit.ts @@ -1,6 +1,6 @@ -import { Rabbit } from './RabbitClass'; +import { Rabbit } from './Classes/RabbitClass'; -const rabbitOne = new Rabbit('Vitalik', 3, 'Brown', 'Male'); +const rabbitOne = new Rabbit('Vitalik', 3, 'Brown', 'Male', 1.5); console.log(rabbitOne); -const rabbitTwo = new Rabbit('LuiVitonovich', 4, 'Black', 'Female'); +const rabbitTwo = new Rabbit('LuiVitonovich', 4, 'Black', 'Female', 1); console.log(rabbitTwo); diff --git a/rpgsaga/saga/src/SuperClass.ts b/rpgsaga/saga/src/SuperClass.ts new file mode 100644 index 00000000..84394bd7 --- /dev/null +++ b/rpgsaga/saga/src/SuperClass.ts @@ -0,0 +1,11 @@ +import { Animal } from './Classes/AnimalSuperclass'; +import { Parrot } from './Classes/ParrotClass'; +import { Rabbit } from './Classes/RabbitClass'; + +const animals: Animal[] = [new Parrot('Kez', 2, 'Blue', 'female', 10), new Rabbit('Nastya', 3, 'Brown', 'male', 1.5)]; +export function animalsOutput(){ +animals.forEach(animal => { + console.log(animal.toString()); + console.log(animal.sound()); +}); +} \ No newline at end of file diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 7bc4a71d..66238a97 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1 +1,4 @@ +import { animalsOutput } from "./SuperClass"; console.log('Hello world'); + +animalsOutput() diff --git a/rpgsaga/saga/tests/ParrotClass.spec.ts b/rpgsaga/saga/tests/ParrotClass.spec.ts new file mode 100644 index 00000000..775c108d --- /dev/null +++ b/rpgsaga/saga/tests/ParrotClass.spec.ts @@ -0,0 +1,36 @@ +import { Parrot } from "../src/Classes/ParrotClass"; + +describe('Parrot',()=> { + let parrot: Parrot; + beforeEach(()=>{ + parrot = new Parrot('Sergey',4,'Black','Male',1.5); + }) + it('Should return right info',()=>{ + expect(parrot.name).toBe('Sergey') + expect(parrot.age).toBe(4) + expect(parrot.color).toBe('Black') + expect(parrot.gender).toBe('Male') + expect(parrot.vocabulary).toBe(1.5) + }) + it('Should return Error when age is invalid',()=>{ + expect(()=>{ + parrot.age = -1 + }).toThrow('Invalid age') + expect(()=>{ + parrot.age = 100 + }).toThrow('Invalid age') + }) + it('Should return Error when gender is invalid',()=>{ + expect(()=>{ + parrot.gender='Mechanic' + }).toThrow('Invalid gender') + }) + it('Should return Error when vocabulary has impossible value',()=>{ + expect(()=>{ + parrot.vocabulary = -1 + }).toThrow('Impossible vocabulary') + expect(()=>{ + parrot.vocabulary = 22 + }).toThrow('Impossible vocabulary') + }) +}) \ No newline at end of file diff --git a/rpgsaga/saga/tests/RabbitClass.spec.ts b/rpgsaga/saga/tests/RabbitClass.spec.ts index eb767e27..27dafd0d 100644 --- a/rpgsaga/saga/tests/RabbitClass.spec.ts +++ b/rpgsaga/saga/tests/RabbitClass.spec.ts @@ -1,15 +1,16 @@ -import { Rabbit } from "../src/RabbitClass"; +import { Rabbit } from "../src/Classes/RabbitClass"; describe('Rabbit',()=> { let rabbit: Rabbit; beforeEach(()=>{ - rabbit = new Rabbit('Sergey',4,'Black','Male'); + rabbit = new Rabbit('Sergey',4,'Black','Male',1.5); }) it('Should return right info',()=>{ expect(rabbit.name).toBe('Sergey') expect(rabbit.age).toBe(4) expect(rabbit.color).toBe('Black') expect(rabbit.gender).toBe('Male') + expect(rabbit.jump).toBe(1.5) }) it('Should return Error when age is invalid',()=>{ expect(()=>{ @@ -24,4 +25,12 @@ describe('Rabbit',()=> { rabbit.gender='Mechanic' }).toThrow('Invalid gender') }) + it('Should return Error when jump has impossible value',()=>{ + expect(()=>{ + rabbit.jump = -1 + }).toThrow('Impossible jump') + expect(()=>{ + rabbit.jump = 52 + }).toThrow('Impossible jump') + }) }) \ No newline at end of file