diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 7556a31..ecb3612 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -8,9 +8,73 @@ */ export class Currency{ + private readonly _name : string; + private _value : number; + private readonly _unit : string; + readonly _type: CurrencyType; + get name() { + return this._name; + } + + get value() { + return this._value; + } + + set value(newValue: number) { + if (newValue < 0) { + throw new Error(); + } + this._value = newValue; + } + + get unit() { + return this._unit; + } + + get type() { + return this._type; + } + + constructor(name: string, value: number, unit: string, type?: CurrencyType) { + if (!name || value < 0 || value === undefined || !unit) { + throw new Error(); + } + this._name = name; + this._value = value; + this._unit = unit; + if(type){ + this._type = type + } else if (name in MaterialNames) { + this._type = CurrencyType.Material + } else if(name in CryptoNames){ + this._type = CurrencyType.Crypt + } else if(name in MetalNames){ + this._type = CurrencyType.Metal + } else { + throw new Error() + } + } } export enum CurrencyType { + Material, + Crypt, + Metal, +} + +enum MaterialNames { + Dollar, + Ruble, + ru, +} +enum CryptoNames { + XRP, + Etherium, + alpha, } + +enum MetalNames { + Gold, +} \ No newline at end of file diff --git a/src/task_2/index.ts b/src/task_2/index.ts index c39ed97..9d1193d 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -5,5 +5,35 @@ * XRP * Etherium * Gold -*/ + */ +import {Currency} from "../task_1"; +export class Dollar extends Currency { + constructor(value: number) { + super("Dollar", value, "USD"); + } +} + +export class Ruble extends Currency { + constructor(value: number) { + super("Ruble", value, "RUB"); + } +} + +export class XRP extends Currency { + constructor(value: number) { + super("XRP", value, "XRP"); + } +} + +export class Etherium extends Currency { + constructor(value: number) { + super("Etherium", value, "ETH"); + } +} + +export class Gold extends Currency { + constructor(value: number) { + super("Gold", value, "GRAMM"); + } +} diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 3f5ea67..59f2562 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -3,15 +3,40 @@ * Из хранилища можно снимать валюту с помощью withdraw(Currency) * В хранилище можно вкладывать валюту через deposit(Currency) * Из хранлилища, можно переводить валюту через transfer(Currency, Vault) -*/ + */ import { Currency } from "../task_1"; export class Vault implements ISecureVaultRequisites{ public id: number; public store: Set = new Set() -} + public withdraw(currency: Currency) { + this.store.forEach((valute) => { + if (valute.name === currency.name && valute.value >= currency.value) { + valute.value -= currency.value; + } else { + throw new Error(); + } + }); + } + public deposit(currency: Currency) { + let newCurrency = true; + this.store.forEach((valute) => { + if (valute.name === currency.name) { + valute.value += currency.value; + newCurrency = false; + } + }); + if (newCurrency) { + this.store.add(currency); + } + } + public transfer(currency: Currency, vault: Vault) { + this.withdraw(currency); + vault.deposit(currency); + } +} export interface ISecureVaultRequisites{ id: number -} +} \ No newline at end of file diff --git a/src/task_4/index.ts b/src/task_4/index.ts index e390bde..ebc325d 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -11,17 +11,72 @@ import { Currency } from "../task_1"; import { ISecureVaultRequisites } from "../task_3"; export class SmartContract implements IContract{ + public id: number; + public state: ContractState = ContractState.pending; + public value: Currency; + public receiver: ISecureVaultRequisites; + public sender: ISecureVaultRequisites; + public closeTransfer(): void { + setTimeout(() => { + this.state = ContractState.close; + }, 3000) + } + + public rejectTransfer(): void { + setTimeout(() => { + this.state = ContractState.rejected; + }, 3000) + } + + public signAndTransfer(): void { + this.state = ContractState.transfer; + } } export class BankingContract implements IContract{ + public id: number; + public state: ContractState = ContractState.pending; + public value: Currency; + public receiver: ISecureVaultRequisites; + public sender: ISecureVaultRequisites; + + public closeTransfer(): void { + this.state = ContractState.close; + } + + public signAndTransfer(): void { + this.state = ContractState.transfer; + } + public rejectTransfer(): void { + this.state = ContractState.rejected; + } } export class LogisticContract implements IContract{ + public id: number; + public state: ContractState = ContractState.pending; + public value: Currency; + public receiver: ISecureVaultRequisites; + public sender: ISecureVaultRequisites; -} + public closeTransfer(): void { + setTimeout(() => { + this.state = ContractState.close; + }, 6000) + } + + public rejectTransfer(): void { + setTimeout(() => { + this.state = ContractState.rejected; + }, 6000) + } + public signAndTransfer(): void { + this.state = ContractState.transfer; + } +} export interface IContract{ /** @@ -75,4 +130,4 @@ export enum ContractState{ * Контракт отменен, либо отклонен */ rejected -} +} \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index d93b101..891c8b4 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -7,17 +7,41 @@ * * Хранилища должны быть сохранены в массив vaultStore: Vault[] */ -import { IContract } from "../task_4"; -import { ISecureVaultRequisites } from "../task_3"; +import {IContract} from "../task_4"; +import {ISecureVaultRequisites, Vault} from "../task_3"; + export class BankController{ + private static _controller: BankController; + private vaultStore: Vault[] = []; + public static getController(): BankController { + if (!BankController._controller) { + BankController._controller = new BankController(); + } - public registerVault(): ISecureVaultRequisites{ + return BankController._controller; + } + public registerVault(vault :Vault ): ISecureVaultRequisites{ + this.vaultStore.push(vault); + return vault; } public proceedContract(contract: IContract) { + const sender = this.vaultStore.find((el) => el.id === contract.sender.id); + const receiver = this.vaultStore.find((el) => el.id === contract.receiver.id); + if(sender !== undefined && receiver !== undefined) { + contract.signAndTransfer() + try { + sender.transfer(contract.value, receiver) + contract.closeTransfer; + } catch (e) { + contract.rejectTransfer(); + } + }else { + contract.rejectTransfer(); + throw new Error(); + } } -} - +} \ No newline at end of file