Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11,556 changes: 6,235 additions & 5,321 deletions rpgsaga/saga/package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions rpgsaga/saga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
"scripts": {
"dev": "ts-node --script-mode src/index.ts",
"test": "jest",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"lint": "eslint --fix \"{src,apps,libs,test}/**/*.ts\"",
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix"
},
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"@eslint/compat": "^1.2.0",
"@types/jest": "^29.5.13",
"@types/node": "^22.7.4",
"@typescript-eslint/eslint-plugin": "^8.8.0",
"@typescript-eslint/parser": "^8.8.0",
"eslint": "^9.12.0",
"eslint": "^9.16.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-prettier": "^5.2.1",
Expand Down
34 changes: 33 additions & 1 deletion rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
console.log('Hello world');
import { taskA, taskB } from './lab1';
import { Phone } from './lab2';
import { SmartPhone } from './lab3';

console.log('Зубков Данила | 2/280 | 6 Вариант');

console.log('Лабораторная работа с мат. функциями');
console.log('Ответы для задачи A:');
console.log(taskA(0.2, 2.2, 0.4));
console.log('Ответы для задачи B:');
console.log(taskB([0.1, 0.9, 1.2, 1.5, 2.3]));

console.log('Лабораторная работа с классами: телефон');
try {
const phone = new Phone('123-456-789', 'Apple', 'iPhone 15', 75);
console.log(`The phone number is ${phone.number}`);
console.log(`The model is ${phone.model}`);
phone.model = 'iPhone 12';
console.log(`The model is set to ${phone.model}`);
phone.call('098-765-533');
phone.call('098-765-534');
} catch (err) {
console.error(`Error creating phone: ${err.message}`);
}

console.log('Лабораторная работа с классами и супер классами: устройство, телефон и смартфон');
try {
const phone = new SmartPhone('Samsung', 'Galaxy S21', 80);
console.log(`The phone brand is ${phone.brand}`);
console.log(`The model is ${phone.model}`);
} catch (err) {
console.error(`Error creating phone: ${err.message}`);
}
23 changes: 23 additions & 0 deletions rpgsaga/saga/src/lab1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function solution(x: number): number {
if (x >= 1) {
return Math.pow(1.2, x) - Math.pow(x, 1.2);
} else {
return Math.acos(x);
}
}

export function taskA(start: number, end: number, step: number): number[] {
const answers: number[] = [];
for (let i = start; i < end; i += step) {
answers.push(solution(i));
}
return answers;
}

export function taskB(numbers: number[]): number[] {
const answers: number[] = [];
for (const num of numbers) {
answers.push(solution(num));
}
return answers;
}
78 changes: 78 additions & 0 deletions rpgsaga/saga/src/lab2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
export class Phone {
private _number: string;
private _brand: string;
private _model: string;
private _battery: number;

constructor(number: string, brand: string, model: string, battery: number) {
this._number = number;
this._brand = brand;
this._model = model;
this._battery = battery;
}

get number(): string {
return this._number;
}

get brand(): string {
return this._brand;
}

get model(): string {
return this._model;
}

get battery(): number {
return this._battery;
}

private validateNonEmpty(value: string): void {
if (value.length === 0) {
throw new Error('Value cannot be empty');
}
}

set number(number: string) {
this.validateNonEmpty(number);
this._number = number;
}

set brand(brand: string) {
this.validateNonEmpty(brand);
this._brand = brand;
}

set model(model: string) {
this.validateNonEmpty(model);
this._model = model;
}

set battery(battery: number) {
if (battery < 0 || battery > 100) {
throw new Error('Battery must be between 0 and 100');
}
this._battery = battery;
}

public chargeBattery(amount: number) {
if (amount < 0) {
throw new Error('Charge amount must be positive');
}
this.battery = Math.min(this.battery + amount, 100);
console.log(`Battery charged. Current level: ${this.battery}%`);
}

public call(target: string) {
if (this.battery <= 5) {
console.log('Low battery level to make a call');
return;
}
console.log(`Calling number ${target}. Current battery level: ${this.battery}%`);
this.battery -= 5;
}

public getInfo(): string {
return `Phone Info:\nNumber: ${this._number}\nBrand: ${this._brand}\nModel: ${this._model}\nBattery Level: ${this._battery}%`;
}
}
108 changes: 108 additions & 0 deletions rpgsaga/saga/src/lab3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
abstract class Device {
protected pbrand: string;
protected pmodel: string;
protected pbattery: number;

constructor(brand: string, model: string, battery: number) {
this.pbrand = brand;
this.pmodel = model;
this.pbattery = battery;
}

get brand(): string {
return this.pbrand;
}

get model(): string {
return this.pmodel;
}

get battery(): number {
return this.pbattery;
}

set battery(battery: number) {
if (battery < 0 || battery > 100) {
throw new Error('Battery must be between 0 and 100');
}
this.pbattery = battery;
}

public chargeBattery(amount: number): void {
if (amount < 0) {
throw new Error('Charge amount must be positive');
}
this.pbattery = Math.min(this.battery + amount, 100);
console.log(`Battery charged. Current level: ${this.pbattery}%`);
}

abstract getDeviceInfo(): string;
}

export class Phones extends Device {
private _number: string;

constructor(number: string, brand: string, model: string, battery: number) {
super(brand, model, battery);
this._number = number;
}

get number(): string {
return this._number;
}

set number(number: string) {
this.validateNonEmpty(number);
this._number = number;
}

private validateNonEmpty(value: string): void {
if (value.length === 0) {
throw new Error('Value cannot be empty');
}
}

public call(target: string): void {
if (this.battery <= 5) {
console.log('Low battery level to make a call');
return;
}
console.log(`Calling number ${target}. Current battery level: ${this.battery}%`);
this.battery -= 5;
}

public getDeviceInfo(): string {
return `Phone Info:\nBrand: ${this.pbrand}\nModel: ${this.pmodel}\nBattery Level: ${this.battery}%\nPhone Number: ${this.number}`;
}

public toString(): string {
return `Phone: ${this.pbrand} ${this.pmodel}, Number: ${this._number}`;
}
}

export class SmartPhone extends Device {
private _apps: string[];

constructor(brand: string, model: string, battery: number) {
super(brand, model, battery);
this._apps = [];
}

public installApp(appName: string): void {
if (this._apps.includes(appName)) {
console.log(`Application "${appName}" is already installed.`);
return;
}
this._apps.push(appName);
console.log(`Application "${appName}" installed successfully.`);
}

public getDeviceInfo(): string {
const appsList = this._apps.length > 0 ? this._apps.join(', ') : 'No apps installed';
return `Smartphone Info:\nBrand: ${this.pbrand}\nModel: ${this.pmodel}\nBattery Level: ${this.battery}%\nInstalled Apps: ${appsList}`;
}

public toString(): string {
return `Smartphone: ${this.pbrand} ${this.pmodel}`;
}
}
7 changes: 0 additions & 7 deletions rpgsaga/saga/tests/example.spec.ts

This file was deleted.

46 changes: 46 additions & 0 deletions rpgsaga/saga/tests/lab1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { solution, taskA, taskB } from '../src/lab1';

describe('Solution', () => {
it('should return correct value for solution(2)', () => {
const res = solution(2);
expect(res).toBeCloseTo(Math.pow(1.2, 2) - Math.pow(2, 1.2), 5);
});

it('should return correct value for solution(0)', () => {
const res = solution(0);
expect(res).toBeCloseTo(Math.acos(0), 5);
});

it('should return correct value for solution(0.5)', () => {
const res = solution(0.5);
expect(res).toBeCloseTo(Math.acos(0.5), 5);
});

it('should return correct array for taskA(0.2, 2.2, 0.4)', () => {
const expected = [];
for (let i = 0.2; i < 2.2; i += 0.4) {
expected.push(solution(i));
}
const res = taskA(0.2, 2.2, 0.4);
expect(res).toEqual(expected);
});

it('should return correct values for taskB([0.1, 0.9, 1.2])', () => {
const input = [0.1, 0.9, 1.2];
const expected = input.map(x => solution(x));
const res = taskB(input);
expect(res).toEqual(expected);
});

it('should return an empty array for taskB([])', () => {
const res = taskB([]);
expect(res).toEqual([]);
});

it('should handle negative values in the input array for taskB([-0.5, -0.2])', () => {
const input = [-0.5, -0.2];
const expected = input.map(x => solution(x));
const res = taskB(input);
expect(res).toEqual(expected);
});
});
Loading
Loading