Skip to content

Commit

Permalink
Abstract Class Created
Browse files Browse the repository at this point in the history
  • Loading branch information
Purp1ee committed Dec 29, 2024
1 parent c0d5106 commit eeedfc2
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 63 deletions.
54 changes: 54 additions & 0 deletions rpgsaga/saga/src/Classes/Abstract/Animal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export abstract class Animal {

Check failure on line 2 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `⏎··`
protected _name: string;

Check failure on line 3 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Class Property name `_name` must match one of the following formats: camelCase
protected _age: number;

Check failure on line 4 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Replace `····` with `··`

Check failure on line 4 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Class Property name `_age` must match one of the following formats: camelCase
protected _foodPreference: string;

Check failure on line 5 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `··`

Check failure on line 5 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Class Property name `_foodPreference` must match one of the following formats: camelCase

constructor(name: string, age: number, foodPreference: string) {

Check failure on line 7 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Replace `····` with `··`
this._name = name;

Check failure on line 8 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `····`
this._age = age;

Check failure on line 9 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Replace `········` with `····`
this._foodPreference = foodPreference;

Check failure on line 10 in rpgsaga/saga/src/Classes/Abstract/Animal.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `····`
}

public get foodPreference(): string {
return this._foodPreference;
}

public set foodPreference(newFoodPreference: string) {
this._foodPreference = newFoodPreference;
}

public get age(): number {
return this._age;
}

public set age(newAge: number) {
if (newAge >= 0) {
this._age = newAge;
} else {
throw new Error('Недопустимый возраст');
}
}

public get name(): string {
return this._name;
}

public set name(newName: string) {
this._name = newName;
}

toString(): string {
return `${this._name} ${this._age} ${this._foodPreference}`;
}

toNumber(): number {
return this._age;
}

print(): string {
return `name: ${this._name}\nage: ${this._age}`;
}

abstract printFoodPreference(): string;
}
46 changes: 46 additions & 0 deletions rpgsaga/saga/src/Classes/Animals/Dog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Animal } from "../Abstract/Animal";

export class Dog extends Animal{

printFoodPreference(): string {
throw new Error("Method not implemented.");
}

private _breed: string;

constructor(name: string, breed: string, age: number) {
super(name, age, breed)
this._breed = breed;
}

toString(): string {
return `${this._name} ${this._breed}`;
}

print(): string {
return `name: ${this.name} breed: ${this.breed} age: ${this.age}`;
}

public get breed(): string {
return this._breed;
}

public set breed(newBreed: string) {
if (newBreed === 'Retriever' || newBreed === 'Dachshund' || newBreed === 'Sheepdog') {
this._breed = newBreed;
} else {
throw new Error('Некорректная порода');
}
}

bark(): void {
console.log(`${this._name} гавкает!`);
}
fetch(item: string) {
console.log(`Несет ${item}!`);
return fetch;
}
play(): void {
console.log('Играет с принесённой вещью');
}
}
12 changes: 12 additions & 0 deletions rpgsaga/saga/src/Classes/Animals/Giraffe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Animal } from "../Abstract/Animal";

export class Giraffe extends Animal {

constructor(name: string, age: number, foodPreference: string) {
super(name, age, foodPreference)
}

printFoodPreference(): string {
return `Giraffe eat ${this.foodPreference}`
}
}
12 changes: 12 additions & 0 deletions rpgsaga/saga/src/Classes/Animals/Lion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Animal } from "../Abstract/Animal";

export class Lion extends Animal {

constructor(name: string, age: number, foodPreference: string) {
super(name, age, foodPreference)
}

printFoodPreference(): string {
return `Lion eat ${this.foodPreference}`
}
}
62 changes: 0 additions & 62 deletions rpgsaga/saga/src/dog.ts

This file was deleted.

11 changes: 11 additions & 0 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { taskA, taskB, output } from './functionNum3';
import { Animal } from './Classes/Abstract/Animal';
import { Lion } from './Classes/Animals/Lion';
import { Giraffe } from './Classes/Animals/Giraffe';

const taskARes = taskA(2, 0.95, 1.25, 2.75, 0.3);
console.log(output('A', taskARes));

const taskBRes = taskB(2, 0.95, []);
console.log(output('B', taskBRes));

const giraffe: Animal = new Giraffe('Ashley', 5, 'Plants');
const lion: Animal = new Lion('Pookie', 7, 'Meat');

const arrayOfAnimals: Animal[] = [giraffe, lion];
for(const animal of arrayOfAnimals) {
console.log(animal.printFoodPreference());
}
2 changes: 1 addition & 1 deletion rpgsaga/saga/tests/dog.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dog } from '../src/dog';
import { Dog } from '../src/Classes/Animals/Dog';

describe('Dog class methods tests', () => {
it('Constuctor test', () => {
Expand Down
72 changes: 72 additions & 0 deletions rpgsaga/saga/tests/giraffeTest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Giraffe } from "../src/Classes/Animals/Giraffe";

describe('Animal class methods tests', () => {
it('Constructor test', () => {
let newGiraffe = new Giraffe('TestAnimal', 5, 'Pie');

expect(newGiraffe.name == 'TestAnimal');
expect(newGiraffe.age == 5);
expect(newGiraffe.foodPreference == 'Pie');
});

describe('Get methods tests', () => {
let newGiraffe = new Giraffe('TestAnimal', 5, 'Pie');

it('Name get test', () => {
expect(newGiraffe.name == 'TestAnimal');
});

it('Age get test', () => {
expect(newGiraffe.age == 5);
});

it('Food preference get test', () => {
expect(newGiraffe.foodPreference == 'Pie');
});
});

describe('Set methods tests', () => {
let newGiraffe = new Giraffe('TestAnimal', 5, 'Pie');

it('Name test', () => {
newGiraffe.name = 'Ashley';
expect(newGiraffe.name == 'Ashley');
});

it('Age test', () => {
newGiraffe.age = 10;
expect(newGiraffe.age == 10);
});

it('Age negative test', () => {
expect(() => {
newGiraffe.age = -1;
}).toThrow(Error('Недопустимый возраст'));
});

it('Food preference test', () => {
newGiraffe.foodPreference = 'Chips';
expect(newGiraffe.foodPreference == 'Chips');
});
});

describe('Other methods tests', () => {
let newGiraffe = new Giraffe('Ashley', 7, 'Plants');

it('Should return name, age and food preference', () => {
expect(newGiraffe.toString()).toEqual(`${newGiraffe.name} ${newGiraffe.age} ${newGiraffe.foodPreference}`);
});

it('Should return age as a number', () => {
expect(newGiraffe.toNumber()).toEqual(newGiraffe.age);
});

it('Should return all properties', () => {
expect(newGiraffe.print() == `name : ${newGiraffe.name}\nage: ${newGiraffe.age}\nfoodPreference: ${newGiraffe.foodPreference}`);
});

it('Should return food preference', () => {
expect(newGiraffe.printFoodPreference()).toEqual(`Giraffe eat ${newGiraffe.foodPreference}`)
});
});
});
72 changes: 72 additions & 0 deletions rpgsaga/saga/tests/lionTest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Lion } from "../src/Classes/Animals/Lion";

describe('Animal class methods tests', () => {
it('Constructor test', () => {
let newLion = new Lion('TestAnimal', 8, 'Meat');

expect(newLion.name == 'TestAnimal');
expect(newLion.age == 8);
expect(newLion.foodPreference == 'Meat');
});

describe('Get methods tests', () => {
let newLion = new Lion('TestAnimal', 8, 'Meat');

it('Name get test', () => {
expect(newLion.name == 'TestAnimal');
});

it('Age get test', () => {
expect(newLion.age == 8);
});

it('Food preference get test', () => {
expect(newLion.foodPreference == 'Meat');
});
});

describe('Set methods tests', () => {
let newLion = new Lion('TestAnimal', 8, 'Meat');

it('Name test', () => {
newLion.name = 'Tigri';
expect(newLion.name == 'Tigri');
});

it('Age test', () => {
newLion.age = 10;
expect(newLion.age == 10);
});

it('Age negative test', () => {
expect(() => {
newLion.age = -1;
}).toThrow(Error('Недопустимый возраст'));
});

it('Food preference test', () => {
newLion.foodPreference = 'Blueberry';
expect(newLion.foodPreference == 'Blueberry');
});
});

describe('Other methods tests', () => {
let newLion = new Lion('Tigri', 8, 'Meat');

it('Should return name, age and food preference', () => {
expect(newLion.toString()).toEqual(`${newLion.name} ${newLion.age} ${newLion.foodPreference}`);
});

it('Should return age as a number', () => {
expect(newLion.toNumber()).toEqual(newLion.age);
});

it('Should return all properties', () => {
expect(newLion.print() == `name : ${newLion.name}\nage: ${newLion.age}\nfoodPreference: ${newLion.foodPreference}`);
});

it('Should return food preference', () => {
expect(newLion.printFoodPreference()).toEqual(`Lion eat ${newLion.foodPreference}`)
});
});
});

0 comments on commit eeedfc2

Please sign in to comment.