Skip to content

Commit

Permalink
//
Browse files Browse the repository at this point in the history
  • Loading branch information
HaQmUser committed Nov 18, 2024
1 parent e621962 commit f535bb1
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
export class Rabbit {
export abstract class Animal {
private _name: string;
private _age: number;
protected _age: number;

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

View workflow job for this annotation

GitHub Actions / saga-ci

Class Property name `_age` must match one of the following formats: camelCase
private _color: string;
private _gender: string;

constructor(name: string, age: number, color: string, gender: string) {
this._name = name;
this._age = age;
this._color = color;
this._gender = gender;
}

abstract sound(): string;

abstract set age(age: number);

public set name(name: string) {
this._name = name;
}
public set age(age: number) {
if (age < 0 || age > 9) {
throw new Error('Invalid age');
}
this._age = age;
}

public set color(color: string) {
this.color = color;
this._color = color;
}

public set gender(gender: string) {
if (gender.toLowerCase() === 'male') {
this._gender = 'Male';
Expand All @@ -31,16 +32,18 @@ export class Rabbit {
throw new Error('Invalid gender');
}
}
public get age() {
return this._age;
}

abstract get age();

public get name() {
return this._name;
}
public get gender() {
return this._gender;
}

public get color() {
return this._color;
}

public get gender() {
return this._gender;
}
}
38 changes: 38 additions & 0 deletions rpgsaga/saga/src/Classes/ParrotClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Animal } from './AnimalSuperclass';

export class Parrot extends Animal {
private _vocabulary: number;
constructor(name: string, age: number, color: string, gender: string, vocabulary: number) {
super(name, age, color, gender);
this._vocabulary = vocabulary;
}

public set age(age: number) {
if (age < 0 || age > 52) {
throw new Error('Invalid age');
}
this.age = age;
}

public set vocabulary(vocabulary: number) {
if (vocabulary < 0 || vocabulary > 20) {
throw new Error('Impossible vocabulary');
}
this._vocabulary = vocabulary;
}

public get vocabulary() {
return this._vocabulary;
}

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

sound(): string {
return 'Chik-Chirik';
}
toString(): string {
return `Parrot named ${this.name}, is ${this.age} years old,${this.gender} and ${this.color}, know ${this.vocabulary} words`;
}
}
38 changes: 38 additions & 0 deletions rpgsaga/saga/src/Classes/RabbitClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Animal } from './AnimalSuperclass';

export class Rabbit extends Animal {
private _jump: number;
constructor(name: string, age: number, color: string, gender: string, jump: number) {
super(name, age, color, gender);
this._jump = jump;
}

public set age(age: number) {
if (age < 0 || age > 5) {
throw new Error('Invalid age');
}
this.age = age;
}

public set jump(jump: number) {
if (jump < 0 || jump > 2.7) {
throw new Error('Impossible jump');
}
this._jump = jump;
}

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

public get jump() {
return this._jump;
}

sound(): string {
return 'Purr-Purr';
}
toString(): string {
return `Rabbit named ${this.name}, is ${this.age} years old,${this.gender} and ${this.color}, can jump ${this.jump} meters`;
}
}
6 changes: 3 additions & 3 deletions rpgsaga/saga/src/Rabbit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rabbit } from './RabbitClass';
import { Rabbit } from './Classes/RabbitClass';

const rabbitOne = new Rabbit('Vitalik', 3, 'Brown', 'Male');
const rabbitOne = new Rabbit('Vitalik', 3, 'Brown', 'Male', 1.5);
console.log(rabbitOne);
const rabbitTwo = new Rabbit('LuiVitonovich', 4, 'Black', 'Female');
const rabbitTwo = new Rabbit('LuiVitonovich', 4, 'Black', 'Female', 1);
console.log(rabbitTwo);
11 changes: 11 additions & 0 deletions rpgsaga/saga/src/SuperClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Animal } from './Classes/AnimalSuperclass';
import { Parrot } from './Classes/ParrotClass';
import { Rabbit } from './Classes/RabbitClass';

const animals: Animal[] = [new Parrot('Kez', 2, 'Blue', 'female', 10), new Rabbit('Nastya', 3, 'Brown', 'male', 1.5)];
export function animalsOutput(){

Check failure on line 6 in rpgsaga/saga/src/SuperClass.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Insert `·`
animals.forEach(animal => {

Check failure on line 7 in rpgsaga/saga/src/SuperClass.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Insert `··`
console.log(animal.toString());

Check failure on line 8 in rpgsaga/saga/src/SuperClass.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Insert `··`
console.log(animal.sound());

Check failure on line 9 in rpgsaga/saga/src/SuperClass.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Insert `··`
});

Check failure on line 10 in rpgsaga/saga/src/SuperClass.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Insert `··`
}

Check failure on line 11 in rpgsaga/saga/src/SuperClass.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Insert `⏎`
3 changes: 3 additions & 0 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import { animalsOutput } from "./SuperClass";

Check failure on line 1 in rpgsaga/saga/src/index.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Expected 1 empty line after import statement not followed by another import

Check failure on line 1 in rpgsaga/saga/src/index.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Replace `"./SuperClass"` with `'./SuperClass'`
console.log('Hello world');

animalsOutput()

Check failure on line 4 in rpgsaga/saga/src/index.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Insert `;`
36 changes: 36 additions & 0 deletions rpgsaga/saga/tests/ParrotClass.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Parrot } from "../src/Classes/ParrotClass";

describe('Parrot',()=> {
let parrot: Parrot;
beforeEach(()=>{
parrot = new Parrot('Sergey',4,'Black','Male',1.5);
})
it('Should return right info',()=>{
expect(parrot.name).toBe('Sergey')
expect(parrot.age).toBe(4)
expect(parrot.color).toBe('Black')
expect(parrot.gender).toBe('Male')
expect(parrot.vocabulary).toBe(1.5)
})
it('Should return Error when age is invalid',()=>{
expect(()=>{
parrot.age = -1
}).toThrow('Invalid age')
expect(()=>{
parrot.age = 100
}).toThrow('Invalid age')
})
it('Should return Error when gender is invalid',()=>{
expect(()=>{
parrot.gender='Mechanic'
}).toThrow('Invalid gender')
})
it('Should return Error when vocabulary has impossible value',()=>{
expect(()=>{
parrot.vocabulary = -1
}).toThrow('Impossible vocabulary')
expect(()=>{
parrot.vocabulary = 22
}).toThrow('Impossible vocabulary')
})
})
13 changes: 11 additions & 2 deletions rpgsaga/saga/tests/RabbitClass.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Rabbit } from "../src/RabbitClass";
import { Rabbit } from "../src/Classes/RabbitClass";

describe('Rabbit',()=> {
let rabbit: Rabbit;
beforeEach(()=>{
rabbit = new Rabbit('Sergey',4,'Black','Male');
rabbit = new Rabbit('Sergey',4,'Black','Male',1.5);
})
it('Should return right info',()=>{
expect(rabbit.name).toBe('Sergey')
expect(rabbit.age).toBe(4)
expect(rabbit.color).toBe('Black')
expect(rabbit.gender).toBe('Male')
expect(rabbit.jump).toBe(1.5)
})
it('Should return Error when age is invalid',()=>{
expect(()=>{
Expand All @@ -24,4 +25,12 @@ describe('Rabbit',()=> {
rabbit.gender='Mechanic'
}).toThrow('Invalid gender')
})
it('Should return Error when jump has impossible value',()=>{
expect(()=>{
rabbit.jump = -1
}).toThrow('Impossible jump')
expect(()=>{
rabbit.jump = 52
}).toThrow('Impossible jump')
})
})

0 comments on commit f535bb1

Please sign in to comment.