Skip to content

Commit

Permalink
severe polymorphism
Browse files Browse the repository at this point in the history
  • Loading branch information
holenyt committed Dec 7, 2024
1 parent 3750c04 commit 9aa009f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 30 deletions.
10 changes: 10 additions & 0 deletions rpgsaga/saga/src/Customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Person } from "./Person";

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

View workflow job for this annotation

GitHub Actions / saga-ci

Replace `"./Person"` with `'./Person'`

export class Customer extends Person {
toString() {

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

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `··`
return 'У Customer toString() переопределён';

Check failure on line 5 in rpgsaga/saga/src/Customer.ts

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `····`
}

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

View workflow job for this annotation

GitHub Actions / saga-ci

Replace `····` with `··`
come(): void {

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

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `··`
console.log(`${this.name} пришёл домой`);

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

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `····`
}

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

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `··`
}

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

View workflow job for this annotation

GitHub Actions / saga-ci

Insert `⏎`
46 changes: 25 additions & 21 deletions rpgsaga/saga/src/Employee.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
export class Employee {
private _name: string;
private _age: number;
import { Person } from "./Person";

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

View workflow job for this annotation

GitHub Actions / saga-ci

Replace `"./Person"` with `'./Person'`

export class Employee extends Person {
private _salary: number;
private _yearsOfWorkExperience: number = 0;

constructor(name: string, age: number, salary: number = 15000) {
this.name = name;
this.age = age;
constructor(name: string, age: number, salary: number = 15000, isSick: boolean = false) {
super(name, age, isSick);
this.salary = salary;
}

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

View workflow job for this annotation

GitHub Actions / saga-ci

Delete `⏎`

public get name(): string {
return this._name;
}
public set name(name: string) {
this._name = name;
}
public get age(): number {
return this._age;
}
public set age(age: number) {
if (age < 0) {
throw new Error('Age cannot be negative.');
}
this._age = age;
}

public get salary(): number {
return this._salary;
}
Expand All @@ -33,4 +19,22 @@ export class Employee {
}
this._salary = salary;
}

come() {
console.log(`${this.name} пришёл на работу`);
}
toString(): void {
console.log('У Employee toString() переопределён')
}


addYearsOfWorkExperience(years: number): void {
if (years < 0) {
throw new Error('You cannot add negative numbers');
}
this._yearsOfWorkExperience+=years;
}
getYearsOfWorkExperience(): number {
return this._yearsOfWorkExperience;
}
}
37 changes: 37 additions & 0 deletions rpgsaga/saga/src/Person.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export abstract class Person {
private _name: string;
private _age: number;
private _sick: boolean;

constructor(name: string, age: number, isSick: boolean = false) {
this._name = name;
this.age = age;
this._sick = isSick;
}

abstract come(): void;
abstract toString(): void;

public get sick(): boolean {
return this._sick;
}
public set sick(isSick: boolean) {
this._sick = isSick;
}

public get name(): string {
return this._name;
}
public set name(name: string) {
this._name = name;
}
public get age(): number {
return this._age;
}
public set age(age: number) {
if (age < 0) {
throw new Error('Age cannot be negative.');
}
this._age = age;
}
}
35 changes: 26 additions & 9 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Customer } from './Customer';
import { Employee } from './Employee';
import { Person } from './Person';

const func = (x: number, a: number = 4.1, b: number = 2.7): number =>
(b * Math.sqrt(x) - a * log(x, 5)) / log(Math.abs(x - 1), 10);
Expand Down Expand Up @@ -26,18 +28,33 @@ const taskB = (xs: number[]): number[] => {

export { func, log, taskA, taskB };

const a: Employee = new Employee('Ivan', 17);
const b: Employee = new Employee('Vlad', 18);
const c: Employee = new Employee('Mikhail', 17, 20000);
const employee1: Employee = new Employee('Ivan', 17);
const employee2: Employee = new Employee('Vlad', 18, 16000, true);
const employee3: Employee = new Employee('Mikhail', 17, 20000);

console.log(a);
console.log(b);
console.log(c);
b.age = 10;
console.log(b);
const customer1: Customer = new Customer('test', 123);

let array: Person[] = [];

array.push(employee1);
array.push(employee2);
array.push(customer1);
for (const a of array) {
a.come();
}


console.log(employee1);
console.log(employee2);
console.log(employee3);
employee2.age = 10;
console.log(employee2);

customer1.toString();
employee3.toString();

try {
c.age = -10;
employee3.age = -10;
} catch (e) {
console.log(e.message);
}
Expand Down
17 changes: 17 additions & 0 deletions rpgsaga/saga/tests/customer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Customer } from "../src/Customer";

describe('test log', () => {
it('should return true', () => {
const customer1 = new Customer('Roman', 32);
expect(customer1.toString()).toEqual('У Customer toString() переопределён');
})
it('should return true', () => {
const customer1 = new Customer('Artem', 50);
expect(customer1.name).toEqual('Artem');
})
it('should return true', () => {
const customer1 = new Customer('Andrey', 32);
customer1.age = 40;
expect(customer1.age).toEqual(40);
})
});

0 comments on commit 9aa009f

Please sign in to comment.