-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
280 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
export abstract class Animal { | ||
|
||
protected _name: string; | ||
protected _age: number; | ||
Check failure on line 4 in rpgsaga/saga/src/Classes/Abstract/Animal.ts
|
||
protected _foodPreference: string; | ||
Check failure on line 5 in rpgsaga/saga/src/Classes/Abstract/Animal.ts
|
||
|
||
constructor(name: string, age: number, foodPreference: string) { | ||
this._name = name; | ||
this._age = age; | ||
this._foodPreference = foodPreference; | ||
} | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('Играет с принесённой вещью'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}); | ||
}); | ||
}); |