Skip to content

Commit

Permalink
Made abstract class, interfaces, inheritance...
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneWWolf committed Oct 11, 2023
1 parent 3435d7a commit ab37710
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
82 changes: 76 additions & 6 deletions rpgsaga/saga/src/pig.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`;
}
}
28 changes: 26 additions & 2 deletions rpgsaga/saga/tests/pig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Pig } 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."));
});
Expand All @@ -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);
Expand All @@ -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`);
});
});

0 comments on commit ab37710

Please sign in to comment.