From 3435d7aa31750343869f80a6d7a029b262357f6c Mon Sep 17 00:00:00 2001 From: Demonage Date: Wed, 11 Oct 2023 17:01:29 +0300 Subject: [PATCH 1/5] Added tests for getters and setters --- rpgsaga/saga/tests/pig.spec.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rpgsaga/saga/tests/pig.spec.ts b/rpgsaga/saga/tests/pig.spec.ts index e0ea1a8..f7a83e0 100644 --- a/rpgsaga/saga/tests/pig.spec.ts +++ b/rpgsaga/saga/tests/pig.spec.ts @@ -11,6 +11,23 @@ describe('Testing pig constructor', () => { }); }); +describe ('Testing pig setters and getters', () => { + it('Normal mass', () => { + const pig = new Pig(50, "ID823"); + expect(pig.mass).toEqual(50); + }) + it('Normal mass (float)', () => { + const pig = new Pig(50.283, "ID823"); + expect(pig.mass).toEqual(50.283); + }) + it('Less than normal mass', () => { + expect(() => new Pig(25, "ID823")).toThrow(Error("Pig's mass should be 50 kg or bigger.")); + }) + it('Negative mass', () => { + expect(() => new Pig(-5, "ID823")).toThrow(Error("Pig's mass should be 50 kg or bigger.")); + }) +}); + describe('Testing pig methods', () => { it('Should give right amount of сало', () => { const pig = new Pig(100, "ADB69"); From ab377102282b8eba1a37c4b36a91d8dfa264098f Mon Sep 17 00:00:00 2001 From: Demonage Date: Wed, 11 Oct 2023 19:43:16 +0300 Subject: [PATCH 2/5] Made abstract class, interfaces, inheritance... --- rpgsaga/saga/src/pig.ts | 82 +++++++++++++++++++++++++++++++--- rpgsaga/saga/tests/pig.spec.ts | 28 +++++++++++- 2 files changed, 102 insertions(+), 8 deletions(-) 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 From 2d816aaf5a4c103b061402b2d07d469c73491836 Mon Sep 17 00:00:00 2001 From: Demonage Date: Mon, 20 Nov 2023 18:01:31 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=87=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpgsaga/saga/.eslintrc.js | 5 ++ rpgsaga/saga/package-lock.json | 2 +- rpgsaga/saga/package.json | 7 +- rpgsaga/saga/src/pig.ts | 142 +++++++++++++-------------------- rpgsaga/saga/tests/pig.spec.ts | 117 +++++++++++++-------------- rpgsaga/saga/yarn.lock | 2 +- 6 files changed, 126 insertions(+), 149 deletions(-) diff --git a/rpgsaga/saga/.eslintrc.js b/rpgsaga/saga/.eslintrc.js index 3613fdf..37ca077 100644 --- a/rpgsaga/saga/.eslintrc.js +++ b/rpgsaga/saga/.eslintrc.js @@ -117,6 +117,11 @@ module.exports = { selector: 'typeProperty', format: ['snake_case', 'camelCase'], }, + { + selector: 'classProperty', + format: ['camelCase'], + leadingUnderscore: 'allow', + }, ], }, settings: { diff --git a/rpgsaga/saga/package-lock.json b/rpgsaga/saga/package-lock.json index b300792..1e19228 100644 --- a/rpgsaga/saga/package-lock.json +++ b/rpgsaga/saga/package-lock.json @@ -13,7 +13,7 @@ "@types/node": "^16.11.0", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", - "eslint": "^7.28.0", + "eslint": "^7.32.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.23.4", "eslint-plugin-prettier": "^3.4.0", diff --git a/rpgsaga/saga/package.json b/rpgsaga/saga/package.json index 8082796..9590d66 100644 --- a/rpgsaga/saga/package.json +++ b/rpgsaga/saga/package.json @@ -11,21 +11,20 @@ }, "author": "", "license": "ISC", - "dependencies": {}, "devDependencies": { "@types/jest": "^27.0.3", "@types/node": "^16.11.0", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", - "eslint": "^7.28.0", + "eslint": "^7.32.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.23.4", "eslint-plugin-prettier": "^3.4.0", "jest": "^27.2.5", "prettier": "^2.4.1", + "ts-jest": "^27.1.2", "ts-node": "^10.3.0", - "typescript": "^4.4.4", - "ts-jest": "^27.1.2" + "typescript": "^4.4.4" }, "jest": { "preset": "ts-jest" diff --git a/rpgsaga/saga/src/pig.ts b/rpgsaga/saga/src/pig.ts index e821e5c..58ce6c6 100644 --- a/rpgsaga/saga/src/pig.ts +++ b/rpgsaga/saga/src/pig.ts @@ -1,104 +1,76 @@ // Список растений, овощей и фруктов, которые ест PlantEater -export enum plantNames -{ - carrot = "carrot", - grass = "grass", - apple = "apple", - pear = "pear", - cucumber = "cucumber", - cabbage = "cabbage", +export enum PlantNames { + carrot = 'carrot', + grass = 'grass', + apple = 'apple', + pear = 'pear', + cucumber = 'cucumber', + cabbage = 'cabbage', } // Список хищников, которые могут охотиться на Herbivore -export enum carnivoreNames -{ - wolf = "wolf", - bear = "bear", +export enum CarnivoreNames { + wolf = 'wolf', + bear = 'bear', } -abstract class Animal -{ - protected _mass: number; - public identificator: string; // код животного на бирке, прикрепленной на ухо +abstract class Animal { + protected _mass: number; + public identificator: string; // код животного на бирке, прикрепленной на ухо - /* - В Typescript (и в JS) нет возможности сделать перегрузку конструкторов, - поэтому сделала лишь один с опциональными параметрами. - */ - constructor(animalName ?: string) - { - if (animalName) - { - this.identificator = animalName; - } - else - { - this.identificator = "Unknown identificator"; - } + constructor(animalName?: string) { + if (animalName) { + this.identificator = animalName; + } else { + this.identificator = 'Unknown identificator'; } + } - abstract makeSound(): string; + abstract makeSound(): string; } -interface PlantEater -{ - eatPlant(plantName: plantNames): string; +interface Herbivore { + runFromCarnivore(carnivoreName: CarnivoreNames): string; + eatPlant(plantName: PlantNames): string; } -interface Herbivore -{ - runFromCarnivore(carnivoreName: carnivoreNames): string; -} +export class Pig extends Animal implements Herbivore { + constructor(mass: number, identificator?: string) { + super(identificator); + this.mass = mass; + } -export class Pig extends Animal implements PlantEater, Herbivore -{ - constructor(mass: number, identificator ?: string) - { - super(identificator); - this.mass = mass; - } - - get mass(): number - { - return this._mass; - } - - set mass(value: number) - { - if (value >= 50) - { - this._mass = value; - } - else - { - throw new Error("Pig's mass should be 50 kg or bigger."); - } - } - - getSalo(): number - { - // Допускается, что количество получаемого со свиньи сала равно 20% от её массы тела - return this._mass * 0.2; - } + get mass(): number { + return this._mass; + } - // Текстовое представление класса - public toString(): string - { - return `The pig id is ${this.identificator} and it's mass is ${this.mass}`; + set mass(value: number) { + if (value >= 50) { + this._mass = value; + } else { + throw new Error("Pig's mass should be 50 kg or bigger."); } + } - makeSound(): string - { - return "Oink-oink"; - } + getSalo(): number { + // Допускается, что количество получаемого со свиньи сала равно 20% от её массы тела + return this._mass * 0.2; + } - eatPlant(plantName: plantNames): string - { - return `Pig "${this.identificator}" is peacefully eating a ${plantName}.` - } + // Текстовое представление класса + public toString(): string { + return `The pig id is ${this.identificator} and it's mass is ${this.mass}`; + } - runFromCarnivore(carnivoreName: carnivoreNames): string - { - return `Pig "${this.identificator}" successfully ran away from a ${carnivoreName}`; - } -} \ No newline at end of file + 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}`; + } +} diff --git a/rpgsaga/saga/tests/pig.spec.ts b/rpgsaga/saga/tests/pig.spec.ts index f33c539..0a9c939 100644 --- a/rpgsaga/saga/tests/pig.spec.ts +++ b/rpgsaga/saga/tests/pig.spec.ts @@ -1,68 +1,69 @@ -import { Pig, plantNames, carnivoreNames } from '../src/pig'; +import { Pig, PlantNames, CarnivoreNames } from '../src/pig'; describe('Testing pig constructor', () => { - it('Pig should be created', () => { - const pig = new Pig(500, "DIV69"); - 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.")); - }); + it('Pig should be created', () => { + const pig = new Pig(500, 'DIV69'); + 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.")); + }); }); -describe ('Testing pig setters and getters', () => { - it('Normal mass', () => { - const pig = new Pig(50, "ID823"); - expect(pig.mass).toEqual(50); - }) - it('Normal mass (float)', () => { - const pig = new Pig(50.283, "ID823"); - expect(pig.mass).toEqual(50.283); - }) - it('Less than normal mass', () => { - expect(() => new Pig(25, "ID823")).toThrow(Error("Pig's mass should be 50 kg or bigger.")); - }) - it('Negative mass', () => { - expect(() => new Pig(-5, "ID823")).toThrow(Error("Pig's mass should be 50 kg or bigger.")); - }) +describe('Testing pig setters and getters', () => { + it('Normal mass', () => { + const pig = new Pig(50, 'ID823'); + expect(pig.mass).toEqual(50); + }); + it('Normal mass (float)', () => { + const pig = new Pig(50.283, 'ID823'); + expect(pig.mass).toEqual(50.283); + }); + it('Less than normal mass', () => { + expect(() => new Pig(25, 'ID823')).toThrow(Error("Pig's mass should be 50 kg or bigger.")); + }); + it('Negative mass', () => { + expect(() => new Pig(-5, 'ID823')).toThrow(Error("Pig's mass should be 50 kg or bigger.")); + }); }); describe('Testing getSalo method', () => { - it('Should give right amount of сало', () => { - const pig = new Pig(100, "ADB69"); - expect(pig.getSalo()).toEqual(20); - }); - it('Should be able to return float values', () => { - const pig = new Pig(101, "ADB69"); - expect(pig.getSalo()).toBeCloseTo(20.2); - }); - it('Should be able to work with float values', () => { - const pig = new Pig(100.5, "ADB69"); - expect(pig.getSalo()).toBeCloseTo(20.1); - }); + it('Should give right amount of сало', () => { + const pig = new Pig(100, 'ADB69'); + expect(pig.getSalo()).toEqual(20); + }); + it('Should be able to return float values', () => { + const pig = new Pig(101, 'ADB69'); + expect(pig.getSalo()).toBeCloseTo(20.2); + }); + it('Should be able to work with float values', () => { + 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 + 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`); + }); +}); diff --git a/rpgsaga/saga/yarn.lock b/rpgsaga/saga/yarn.lock index 1820d0a..2a88958 100644 --- a/rpgsaga/saga/yarn.lock +++ b/rpgsaga/saga/yarn.lock @@ -1504,7 +1504,7 @@ eslint-visitor-keys@^3.0.0: resolved "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.0.0.tgz" integrity sha512-mJOZa35trBTb3IyRmo8xmKBZlxf+N7OnUl4+ZhJHs/r+0770Wh/LEACE2pqMGMe27G/4y8P2bYGk4J70IC5k1Q== -eslint@*, "eslint@^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8", "eslint@^6.0.0 || ^7.0.0 || ^8.0.0", eslint@^7.28.0, eslint@>=5, eslint@>=5.0.0, eslint@>=7.0.0: +eslint@*, "eslint@^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8", "eslint@^6.0.0 || ^7.0.0 || ^8.0.0", eslint@^7.32.0, eslint@>=5, eslint@>=5.0.0, eslint@>=7.0.0: version "7.32.0" resolved "https://registry.npmjs.org/eslint/-/eslint-7.32.0.tgz" integrity sha512-VHZ8gX+EDfz+97jGcgyGCyRia/dPOd6Xh9yPv8Bl1+SoaIwD+a/vlrOmGRUyOYu7MwUhc7CxqeaDZU13S4+EpA== From cf362248c43d6df206105b5018ed9e39d3370a84 Mon Sep 17 00:00:00 2001 From: Demonage Date: Mon, 20 Nov 2023 18:11:41 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=A0=D0=B0=D1=81=D0=BA=D0=B8=D0=B4=D0=B0?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B,=20enum'?= =?UTF-8?q?=D1=8B=20=D0=B8=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=D1=8B=20=D0=BF=D0=BE=20=D0=BE=D1=82=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D1=8B=D0=BC=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpgsaga/saga/src/animal.ts | 14 +++++++++ rpgsaga/saga/src/enums/carnivoreNames.ts | 5 +++ rpgsaga/saga/src/enums/plantNames.ts | 9 ++++++ rpgsaga/saga/src/interfaces/herbivore.ts | 7 +++++ rpgsaga/saga/src/phone.ts | 2 +- rpgsaga/saga/src/pig.ts | 39 +++--------------------- 6 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 rpgsaga/saga/src/animal.ts create mode 100644 rpgsaga/saga/src/enums/carnivoreNames.ts create mode 100644 rpgsaga/saga/src/enums/plantNames.ts create mode 100644 rpgsaga/saga/src/interfaces/herbivore.ts diff --git a/rpgsaga/saga/src/animal.ts b/rpgsaga/saga/src/animal.ts new file mode 100644 index 0000000..8533a1d --- /dev/null +++ b/rpgsaga/saga/src/animal.ts @@ -0,0 +1,14 @@ +export abstract class Animal { + protected _mass: number; + public identificator: string; // код животного на бирке, прикрепленной на ухо + + constructor(animalName?: string) { + if (animalName) { + this.identificator = animalName; + } else { + this.identificator = 'Unknown identificator'; + } + } + + abstract makeSound(): string; +} diff --git a/rpgsaga/saga/src/enums/carnivoreNames.ts b/rpgsaga/saga/src/enums/carnivoreNames.ts new file mode 100644 index 0000000..3f500cd --- /dev/null +++ b/rpgsaga/saga/src/enums/carnivoreNames.ts @@ -0,0 +1,5 @@ +// Список хищников, которые могут охотиться на Herbivore +export enum CarnivoreNames { + wolf = 'wolf', + bear = 'bear', +} diff --git a/rpgsaga/saga/src/enums/plantNames.ts b/rpgsaga/saga/src/enums/plantNames.ts new file mode 100644 index 0000000..20cd9a0 --- /dev/null +++ b/rpgsaga/saga/src/enums/plantNames.ts @@ -0,0 +1,9 @@ +// Список растений, овощей и фруктов, которые ест PlantEater +export enum PlantNames { + carrot = 'carrot', + grass = 'grass', + apple = 'apple', + pear = 'pear', + cucumber = 'cucumber', + cabbage = 'cabbage', +} diff --git a/rpgsaga/saga/src/interfaces/herbivore.ts b/rpgsaga/saga/src/interfaces/herbivore.ts new file mode 100644 index 0000000..b5ec9b9 --- /dev/null +++ b/rpgsaga/saga/src/interfaces/herbivore.ts @@ -0,0 +1,7 @@ +import { CarnivoreNames } from '../enums/carnivoreNames'; +import { PlantNames } from '../enums/plantNames'; + +export interface Herbivore { + runFromCarnivore(carnivoreName: CarnivoreNames): string; + eatPlant(plantName: PlantNames): string; +} diff --git a/rpgsaga/saga/src/phone.ts b/rpgsaga/saga/src/phone.ts index d310d29..f49d2da 100644 --- a/rpgsaga/saga/src/phone.ts +++ b/rpgsaga/saga/src/phone.ts @@ -25,4 +25,4 @@ export class Phone { get year(): number { return this.aYear; } -} \ No newline at end of file +} diff --git a/rpgsaga/saga/src/pig.ts b/rpgsaga/saga/src/pig.ts index 58ce6c6..34b4992 100644 --- a/rpgsaga/saga/src/pig.ts +++ b/rpgsaga/saga/src/pig.ts @@ -1,38 +1,7 @@ -// Список растений, овощей и фруктов, которые ест PlantEater -export enum PlantNames { - 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; // код животного на бирке, прикрепленной на ухо - - constructor(animalName?: string) { - if (animalName) { - this.identificator = animalName; - } else { - this.identificator = 'Unknown identificator'; - } - } - - abstract makeSound(): string; -} - -interface Herbivore { - runFromCarnivore(carnivoreName: CarnivoreNames): string; - eatPlant(plantName: PlantNames): string; -} +import { Animal } from './animal'; +import { Herbivore } from './interfaces/herbivore'; +import { PlantNames } from './enums/plantNames'; +import { CarnivoreNames } from './enums/carnivoreNames'; export class Pig extends Animal implements Herbivore { constructor(mass: number, identificator?: string) { From 43094629dd6a16b065e0fe30177bf82918e0be75 Mon Sep 17 00:00:00 2001 From: Demonage Date: Mon, 20 Nov 2023 18:30:41 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=B2=D1=81=D0=B5=20=D0=B7=D0=B0=D0=BC=D0=B5?= =?UTF-8?q?=D1=87=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpgsaga/saga/src/animal.ts | 4 +++- rpgsaga/saga/src/pig.ts | 8 +++++--- rpgsaga/saga/tests/pig.spec.ts | 9 ++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/rpgsaga/saga/src/animal.ts b/rpgsaga/saga/src/animal.ts index 8533a1d..c1701b2 100644 --- a/rpgsaga/saga/src/animal.ts +++ b/rpgsaga/saga/src/animal.ts @@ -2,7 +2,9 @@ export abstract class Animal { protected _mass: number; public identificator: string; // код животного на бирке, прикрепленной на ухо - constructor(animalName?: string) { + constructor(mass: number, animalName?: string) { + this._mass = mass; + if (animalName) { this.identificator = animalName; } else { diff --git a/rpgsaga/saga/src/pig.ts b/rpgsaga/saga/src/pig.ts index 34b4992..3307728 100644 --- a/rpgsaga/saga/src/pig.ts +++ b/rpgsaga/saga/src/pig.ts @@ -5,7 +5,9 @@ import { CarnivoreNames } from './enums/carnivoreNames'; export class Pig extends Animal implements Herbivore { constructor(mass: number, identificator?: string) { - super(identificator); + super(mass, identificator); + + // У свиньи есть ограничение по весу, так что снова переопределяем массу с помощью сеттера this.mass = mass; } @@ -36,10 +38,10 @@ export class Pig extends Animal implements Herbivore { } eatPlant(plantName: PlantNames): string { - return `Pig "${this.identificator}" is peacefully eating a ${plantName}.`; + return `Pig '${this.identificator}' is peacefully eating a ${plantName}.`; } runFromCarnivore(carnivoreName: CarnivoreNames): string { - return `Pig "${this.identificator}" successfully ran away from a ${carnivoreName}`; + return `Pig '${this.identificator}' successfully ran away from a ${carnivoreName}`; } } diff --git a/rpgsaga/saga/tests/pig.spec.ts b/rpgsaga/saga/tests/pig.spec.ts index 0a9c939..58a6895 100644 --- a/rpgsaga/saga/tests/pig.spec.ts +++ b/rpgsaga/saga/tests/pig.spec.ts @@ -1,4 +1,6 @@ -import { Pig, PlantNames, CarnivoreNames } from '../src/pig'; +import { Pig } from '../src/pig'; +import { CarnivoreNames } from '../src/enums/carnivoreNames'; +import { PlantNames } from '../src/enums/plantNames'; describe('Testing pig constructor', () => { it('Pig should be created', () => { @@ -63,7 +65,8 @@ describe('Testing Pig methods', () => { }); 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`); + expect(pig.runFromCarnivore(CarnivoreNames.wolf)).toEqual( + `Pig '${pig.identificator}' successfully ran away from a wolf`, + ); }); });