diff --git a/rpgsaga/saga/src/pig.ts b/rpgsaga/saga/src/pig.ts index 5abdd3b..e821e5c 100644 --- a/rpgsaga/saga/src/pig.ts +++ b/rpgsaga/saga/src/pig.ts @@ -1,12 +1,61 @@ -export class Pig +// Список растений, овощей и фруктов, которые ест PlantEater +export enum plantNames { - private _mass: number; - public identificator: string; // Код на бирке у свиньи - - constructor(mass: number, identificator: string) + carrot = "carrot", + grass = "grass", + apple = "apple", + pear = "pear", + cucumber = "cucumber", + cabbage = "cabbage", +} + +// Список хищников, которые могут охотиться на Herbivore +export enum carnivoreNames +{ + wolf = "wolf", + bear = "bear", +} + +abstract class Animal +{ + protected _mass: number; + public identificator: string; // код животного на бирке, прикрепленной на ухо + + /* + В Typescript (и в JS) нет возможности сделать перегрузку конструкторов, + поэтому сделала лишь один с опциональными параметрами. + */ + constructor(animalName ?: string) + { + if (animalName) + { + this.identificator = animalName; + } + else + { + this.identificator = "Unknown identificator"; + } + } + + abstract makeSound(): string; +} + +interface PlantEater +{ + eatPlant(plantName: plantNames): string; +} + +interface Herbivore +{ + runFromCarnivore(carnivoreName: carnivoreNames): string; +} + +export class Pig extends Animal implements PlantEater, Herbivore +{ + constructor(mass: number, identificator ?: string) { + super(identificator); this.mass = mass; - this.identificator = identificator; } get mass(): number @@ -31,4 +80,25 @@ export class Pig // Допускается, что количество получаемого со свиньи сала равно 20% от её массы тела return this._mass * 0.2; } + + // Текстовое представление класса + public toString(): string + { + return `The pig id is ${this.identificator} and it's mass is ${this.mass}`; + } + + makeSound(): string + { + return "Oink-oink"; + } + + eatPlant(plantName: plantNames): string + { + return `Pig "${this.identificator}" is peacefully eating a ${plantName}.` + } + + runFromCarnivore(carnivoreName: carnivoreNames): string + { + return `Pig "${this.identificator}" successfully ran away from a ${carnivoreName}`; + } } \ No newline at end of file diff --git a/rpgsaga/saga/tests/pig.spec.ts b/rpgsaga/saga/tests/pig.spec.ts index f7a83e0..f33c539 100644 --- a/rpgsaga/saga/tests/pig.spec.ts +++ b/rpgsaga/saga/tests/pig.spec.ts @@ -1,4 +1,4 @@ -import { Pig } from '../src/pig'; +import { Pig, plantNames, carnivoreNames } from '../src/pig'; describe('Testing pig constructor', () => { it('Pig should be created', () => { @@ -6,6 +6,11 @@ describe('Testing pig constructor', () => { expect(pig.mass).toEqual(500); expect(pig.identificator).toEqual("DIV69"); }); + it('Pig with empty identificator should be created', () => { + const pig = new Pig(500); + expect(pig.mass).toEqual(500); + expect(pig.identificator).toEqual("Unknown identificator"); + }); it('Pig with invalid mass', () => { expect(() => new Pig(10, "RIP38")).toThrow(Error("Pig's mass should be 50 kg or bigger.")); }); @@ -28,7 +33,7 @@ describe ('Testing pig setters and getters', () => { }) }); -describe('Testing pig methods', () => { +describe('Testing getSalo method', () => { it('Should give right amount of сало', () => { const pig = new Pig(100, "ADB69"); expect(pig.getSalo()).toEqual(20); @@ -41,4 +46,23 @@ describe('Testing pig methods', () => { const pig = new Pig(100.5, "ADB69"); expect(pig.getSalo()).toBeCloseTo(20.1); }); +}); + +describe('Testing Pig methods', () => { + it('Should return corrent text representation of Pig class', () => { + const pig = new Pig(100, "ADB69"); + expect(pig.toString()).toEqual(`The pig id is ${pig.identificator} and it's mass is ${pig.mass}`); + }); + it('Should return corrent sound', () => { + const pig = new Pig(100, "ADG283"); + expect(pig.makeSound()).toEqual("Oink-oink"); + }); + it('Should return corrent message about eating plant', () => { + const pig = new Pig(100, "ADJ820"); + expect(pig.eatPlant(plantNames.cabbage)).toEqual(`Pig "${pig.identificator}" is peacefully eating a cabbage.`); + }); + it('Should successfully run away from carnivore', () => { + const pig = new Pig(100, "ADJ820"); + expect(pig.runFromCarnivore(carnivoreNames.wolf)).toEqual(`Pig "${pig.identificator}" successfully ran away from a wolf`); + }); }); \ No newline at end of file