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

Create PR #25

Open
wants to merge 5 commits into
base: Novoselov_Nikolaj_Vitalevich
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
42 changes: 42 additions & 0 deletions rpgsaga/saga/src/classTask/mouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export enum Mode {
ALIVE = "Alive",
DIED = "Died"
}
export class Mouse {
_healthpool: number = 100;
private _age: number;
_color: string;
_mode: Mode = Mode.ALIVE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и извне могу ее убить - поле то публичное

constructor(year: number, color: string) {
this.year = year;
this._color = color;
};
set year(year: number) {
this._age = year > 0 && year < 4 ? year : 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

при неправильном значении бросайте exception

}
get year(): number {
return this._age
}
hit(): string {
this._healthpool -= 10
if (this._healthpool > 0) {
return `Вы ударили мышь, теперь у неё ${this._healthpool} здоровья`
} else {
this._mode = Mode.DIED
return "Мышь сдохла"
}
}
heal(): string {
this._healthpool = 100
return `Теперь здоровье мыши в норме и составляет 100 единиц`
}
paint(color): string {
if (this._color == color) {
return `Зачем вы красите ${this._color} в ${color}`
} else {
this._color = color
return `Теперь мышь имеет ${color} цвет`
}
}

}
26 changes: 10 additions & 16 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { Phone } from './phone';

const first = new Phone('+7900-000 000 (123)', 1990, 'Телефон 1');
first.year = 1998;

first.year = -1998;
first.call('12345');
first.endCall();

const second = new Phone('+799900000', -5);
// second.name = 'Телефон 2';
console.log(second.year);
second.call('12345');
second.endCall();

console.log(first, second, Phone.phoneCount);
import { Animal } from "./polymorphism/animal";
import { Mode } from "./polymorphism/animal";
import { Fox } from "./polymorphism/fox";
import { chinchilla } from "./polymorphism/chinchilla";
let animalArray:object[] = []
const Karina = new Fox(200, "Белая");
const Evgenia = new chinchilla(19, "Чёрная")
animalArray.push(Karina,Evgenia)
console.log(Karina.sound())
console.log(Evgenia.sound())
28 changes: 0 additions & 28 deletions rpgsaga/saga/src/phone.ts

This file was deleted.

16 changes: 16 additions & 0 deletions rpgsaga/saga/src/polymorphism/animal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum Mode {
ALIVE = "Alive",
DIED = "Died"
};
export class Animal {
_mode: Mode = Mode.ALIVE;
_color: string;
_healthpool: number = 100;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и вот это поле публичное

constructor(color: string) {
this._color = color;
};
sound(): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тогда сделайте его абстрактным

return "Мы не можем понять, кто это"
}
};

23 changes: 23 additions & 0 deletions rpgsaga/saga/src/polymorphism/chinchilla.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Animal } from "./animal";
import { Mode } from "./animal";
export class chinchilla extends Animal {
private _age: number;
constructor(year: number, color: string) {
super(color);
this.year = year;
};
set year(year: number) {
this._age = year > 0 && year < 20 ? year : 0;
};
Comment on lines +9 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а вот тут интересно - у вас у всех есть возраст - но нет в базовом классе -> я могу создать животьное у которого не будет этого года - что может привести к неожиданному поведению


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

sound(): string {
return `Фыр-фыр`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

};
toString(): string {
return "Это Шиншила";
};
};
34 changes: 34 additions & 0 deletions rpgsaga/saga/src/polymorphism/fox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Animal } from "./animal";
import { Mode } from "./animal";
export class Fox extends Animal {
private _age: number;
constructor(year: number, color: string) {
super(color);
this.year = year;
};
set year(year: number) {
this._age = year > 0 && year < 25 ? year : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

при неправильном годе - бросайте exception

кроме того вы можете определить базовое поведение и хранить сколько лет живет то или иное животное - и исходя из этого сделать этот метод вообще в базовом классе

};

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

sound(): string {
return `Миу`;
};

toString(): string {
return "Это Лиса";
};

hit(): string {
this._healthpool -= 10;
if (this._healthpool > 0) {
return `Вы ударили лису, теперь у неё ${this._healthpool} здоровья`
} else {
this._healthpool = 100;
return "У лисы осталось 0 здоровья, однако боги исцелили её";
};
};
};
7 changes: 0 additions & 7 deletions rpgsaga/saga/tests/example.spec.ts

This file was deleted.

40 changes: 40 additions & 0 deletions rpgsaga/saga/tests/mouse.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Mouse } from '../src/classTask/mouse';
import { Mode } from "../src/classTask/mouse";

xdescribe('Testing Mouse constructor', () => {
it('Mouse should be created with incorrect age', () => {
const mouse = new Mouse(6, "Красный");
expect(mouse.year).toEqual(0);
expect(mouse._healthpool).toEqual(100);
expect(mouse._color).toEqual("Красный");
expect(mouse._mode).toEqual(Mode.ALIVE);
});
it("Mouse should be created", () => {
const mouse = new Mouse(2, "Чёрный");
expect(mouse.year).toEqual(2);
expect(mouse._healthpool).toEqual(100);
expect(mouse._color).toEqual("Чёрный");
expect(mouse._mode).toEqual(Mode.ALIVE);
});
});

xdescribe('Testing Mouse methods', () => {
it("Method hit", () => {
const mouse = new Mouse(2, "Чёрный");
for (let index = 1; index < 10; index++) {
mouse.hit()
expect(mouse._healthpool).toEqual(100 - index * 10)
};
});
it("Method heal", () => {
const mouse = new Mouse(2, "Чёрный");
mouse.heal();
expect(mouse._healthpool).toEqual(100)
});
it("Method color with same color and not", () => {
const mouse = new Mouse(2, "Чёрный");
expect(mouse.paint("Красный")).toEqual("Теперь мышь имеет Красный цвет");
expect(mouse.paint("Красный")).toEqual("Зачем вы красите Красный в Красный");
})
});

46 changes: 0 additions & 46 deletions rpgsaga/saga/tests/phone.spec.ts

This file was deleted.

41 changes: 41 additions & 0 deletions rpgsaga/saga/tests/polymorphism.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import { Animal } from "../src/polymorphism/animal";
import { Mode } from "../src/polymorphism/animal";
import { Fox } from "../src/polymorphism/fox";
import { chinchilla } from "../src/polymorphism/chinchilla";
import { generateKeyPair } from "crypto";

describe('Testing Animals constructor', () => {
it('Animals should be created with incorrect age', () => {
const Evgenia = new chinchilla(200, "Белая");
const Karina = new Fox(200, "Чёрная");
expect(Evgenia.year).toEqual(0)
expect(Karina.year).toEqual(0)
expect(Evgenia._color).toEqual("Белая")
expect(Karina._color).toEqual("Чёрная")
});

it("Animals should be created", () => {
const Evgenia = new chinchilla(19, "Белая");
const Karina = new Fox(19, "Чёрная");
expect(Evgenia.year).toEqual(19)
expect(Karina.year).toEqual(19)
});
});

describe('Testing Animals methods', () => {
it("Method toString", () => {
const Evgenia = new chinchilla(19, "Белая");
const Karina = new Fox(19, "Чёрная");
expect(`${Evgenia}`).toEqual("Это Шиншила");
expect(`${Karina}`).toEqual("Это Лиса");
});
it("Method sound", () => {
const Evgenia = new chinchilla(19, "Белая");
const Karina = new Fox(19, "Чёрная");
expect(Karina.sound()).toEqual("Миу");
expect(Evgenia.sound()).toEqual("Фыр-фыр");
});
it("Method color with same color and not", () => {
})
});