Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Дополнительное задание к первому (абстрактные классы и методы, интерфейсы, наследование) #18

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions rpgsaga/saga/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ module.exports = {
selector: 'typeProperty',
format: ['snake_case', 'camelCase'],
},
{
selector: 'classProperty',
format: ['camelCase'],
leadingUnderscore: 'allow',
},
],
},
settings: {
Expand Down
2 changes: 1 addition & 1 deletion rpgsaga/saga/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions rpgsaga/saga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions rpgsaga/saga/src/animal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export abstract class Animal {
protected _mass: number;
public identificator: string; // код животного на бирке, прикрепленной на ухо

constructor(mass: number, animalName?: string) {
this._mass = mass;

if (animalName) {
this.identificator = animalName;
} else {
this.identificator = 'Unknown identificator';
}
}

abstract makeSound(): string;
}
5 changes: 5 additions & 0 deletions rpgsaga/saga/src/enums/carnivoreNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Список хищников, которые могут охотиться на Herbivore
export enum CarnivoreNames {
wolf = 'wolf',
bear = 'bear',
}
9 changes: 9 additions & 0 deletions rpgsaga/saga/src/enums/plantNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Список растений, овощей и фруктов, которые ест PlantEater
export enum PlantNames {
carrot = 'carrot',
grass = 'grass',
apple = 'apple',
pear = 'pear',
cucumber = 'cucumber',
cabbage = 'cabbage',
}
7 changes: 7 additions & 0 deletions rpgsaga/saga/src/interfaces/herbivore.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion rpgsaga/saga/src/phone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ export class Phone {
get year(): number {
return this.aYear;
}
}
}
79 changes: 46 additions & 33 deletions rpgsaga/saga/src/pig.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
export class Pig
{
private _mass: number;
public identificator: string; // Код на бирке у свиньи

constructor(mass: number, identificator: string)
{
this.mass = mass;
this.identificator = identificator;
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) {
super(mass, 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.");
}

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;
}
}
}

getSalo(): number {
// Допускается, что количество получаемого со свиньи сала равно 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}`;
}
}
89 changes: 67 additions & 22 deletions rpgsaga/saga/tests/pig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
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', () => {
const pig = new Pig(500, "DIV69");
expect(pig.mass).toEqual(500);
expect(pig.identificator).toEqual("DIV69");
});
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 methods', () => {
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 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);
});
});

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`,
);
});
});
2 changes: 1 addition & 1 deletion rpgsaga/saga/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down
Loading