From d248edf8771de6b039357df4576ab99253c95443 Mon Sep 17 00:00:00 2001 From: Yaroslav Komarov Date: Mon, 5 Apr 2021 12:25:40 +0500 Subject: [PATCH 1/3] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=201=20-=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 46 +++++++++++++++++++++++++++++----- src/task_2/index.ts | 33 +++++++++++++++++-------- src/task_3/index.ts | 44 ++++++++++++++++++++++++--------- src/task_4/index.ts | 21 +++++++++++++--- src/task_5/index.ts | 60 +++++++++++++++++++++++++++++++++++---------- 5 files changed, 159 insertions(+), 45 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 5827d9c..afc26e3 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -20,7 +20,7 @@ import { Currency } from '../enums'; -interface IMoneyInfo { +interface IMoneyInfo { denomination: string; currency: Currency; } @@ -31,16 +31,50 @@ export interface IMoneyUnit { } export class MoneyRepository { - private _repository: any; - constructor(initialRepository: any) { + private _repository: IMoneyUnit[]; + private static readonly _usNominal: number[] = [100, 50, 20, 10, 5, 2, 1]; + private static readonly _ruNominal: number[] = [5000, 2000, 1000, 500, 200, 100, 50]; + constructor(initialRepository: IMoneyUnit[]) { this._repository = initialRepository; } - public giveOutMoney(count: any, currency: any): any { - + private static _splitAmount(value: number, currency: Currency): [string, number][] { + let result: [string, number][] = []; + const denominations: number[] = currency === Currency.RUB ? this._ruNominal : this._usNominal; + denominations.forEach(nominal => { + if (nominal < value){ + const count: number = Math.floor(value / nominal); + result.push([`${nominal}${Currency[currency]}`, count]); + value -= nominal * count; + } + }) + return result; } - public takeMoney(moneyUnits: any): any { + public giveOutMoney(count: number, currency: Currency): boolean { + const nominalCountPair = MoneyRepository._splitAmount(count, currency); + nominalCountPair.forEach(pair => { + if (!this._repository.find(unit => unit.moneyInfo.denomination === pair[0] && unit.count >= pair[1])) { + return false; + } + }) + return true; + } + public takeMoney(moneyUnits: IMoneyUnit[]) { + moneyUnits.forEach(unit1 => { + let flag: boolean = true; + for(const unit2 of this._repository){ + const isEqualNominal = unit1.moneyInfo.denomination === unit2.moneyInfo.denomination; + const isEqualCurrency = unit1.moneyInfo.currency === unit2.moneyInfo.currency; + if (isEqualNominal && isEqualCurrency) { + unit2.count += unit1.count; + flag = false; + } + } + if (flag) { + this._repository.push(unit1); + } + }) } } \ No newline at end of file diff --git a/src/task_2/index.ts b/src/task_2/index.ts index fe95180..e72908d 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -16,8 +16,9 @@ */ import { Currency } from '../enums'; +import { IMoneyUnit, MoneyRepository } from '../task_1'; -interface ICard { +export interface ICard { id: string; balance: number; currency: Currency, @@ -32,23 +33,35 @@ export interface IBankUser { } export class BankOffice { - private _users: any; - private _cards: any; + private _users: IBankUser[]; + private _cards: ICard[]; - constructor(users: any, cards: any) { + constructor(users: IBankUser[], cards: ICard[]) { this._users = users; this._cards = cards; } - public authorize(userId: any, cardId: any, cardPin: any): any { - + public authorize(userId: string, cardId: string, cardPin: string): boolean { + this._users.forEach(user => { + if (user.id === userId){ + if (user.cards.find(card => card.id === cardId && card.pin === cardPin)){ + return true; + } + } + }) + return false; } - public getCardById(cardId: any): any { - + public getCardById(cardId: string): ICard { + return this._cards.find(card => card.id === cardId); } - public isCardTiedToUser(cardId: any): any { - + public isCardTiedToUser(cardId: string): boolean { + for(const user of this._users) { + if (user.cards.find(card => card.id === cardId)) { + return true; + } + } + return false; } } \ No newline at end of file diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 9020644..dd890a5 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -18,32 +18,52 @@ */ import { UserSettingOptions } from '../enums'; +import { BankOffice, IBankUser } from '../task_2'; export class UserSettingsModule { - private _bankOffice: any; - private _user: any; + private _bankOffice: BankOffice; + private _user: IBankUser; - public set user(user: any) { + public set user(user: IBankUser) { this._user = user; } - constructor(initialBankOffice: any) { + constructor(initialBankOffice: BankOffice) { this._bankOffice = initialBankOffice; } - private changeUserName(newName: any): any { - + private changeUserName(newName: string): boolean { + if (this._user) { + this._user.name = newName; + return true; + } + return false; } - private changeUserSurname(newSurname: any): any { - + private changeUserSurname(newSurname: string): boolean { + if (this._user) { + this._user.surname = newSurname; + return true; + } + return false; } - private registerForUserNewCard(newCardId: any): any { - + private registerForUserNewCard(newCardId: string): boolean { + const newCard = this._bankOffice.getCardById(newCardId); + if (this._user && newCard && !this._bankOffice.isCardTiedToUser(newCardId)) { + this._user.cards.push(newCard); + return true; + } + return false; } - public changeUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any { - + public changeUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean { + if (option === UserSettingOptions.name){ + return this.changeUserName(argsForChangeFunction); + } else if (option === UserSettingOptions.surname){ + return this.changeUserSurname(argsForChangeFunction); + } else { + return this.registerForUserNewCard(argsForChangeFunction); + } } } \ No newline at end of file diff --git a/src/task_4/index.ts b/src/task_4/index.ts index bdd7528..a06b23e 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -16,15 +16,28 @@ */ import { Currency } from '../enums'; +import { IMoneyUnit, MoneyRepository } from '../task_1'; export class CurrencyConverterModule { - private _moneyRepository: any; + private _moneyRepository: MoneyRepository; - constructor(initialMoneyRepository: any) { + constructor(initialMoneyRepository: MoneyRepository) { this._moneyRepository = initialMoneyRepository; } - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: any): any { - + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit[]): IMoneyUnit[] { + const coeff = fromCurrency === Currency.RUB ? 70 : 1/70; + let result: IMoneyUnit[] = []; + moneyUnits.forEach(moneyUnit => { + const tmpObj: IMoneyUnit = { + moneyInfo: { + denomination: (+moneyUnit.moneyInfo.denomination * coeff).toString(), + currency: toCurrency, + }, + count: moneyUnit.count, + } + result.push(tmpObj); + }) + return result; } } \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index 3c99ee2..366de07 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -16,8 +16,8 @@ */ import { Currency, UserSettingOptions } from '../enums'; -import { MoneyRepository } from '../task_1'; -import { BankOffice, IBankUser } from '../task_2'; +import { IMoneyUnit, MoneyRepository } from '../task_1'; +import { BankOffice, IBankUser, ICard } from '../task_2'; import { UserSettingsModule } from '../task_3'; import { CurrencyConverterModule } from '../task_4'; @@ -27,31 +27,65 @@ class BankTerminal { private _userSettingsModule: UserSettingsModule; private _currencyConverterModule: CurrencyConverterModule; private _authorizedUser: IBankUser; + private _usersCard: ICard; - constructor(initBankOffice: any, initMoneyRepository: any) { + constructor(initBankOffice: BankOffice, initMoneyRepository: MoneyRepository) { this._moneyRepository = initMoneyRepository; this._bankOffice = initBankOffice; this._userSettingsModule = new UserSettingsModule(initBankOffice); this._currencyConverterModule = new CurrencyConverterModule(initMoneyRepository); } - public authorizeUser(user: any, card: any, cardPin: any): any { - + public authorizeUser(user: IBankUser, card: ICard, cardPin: string): boolean { + if (this._bankOffice.authorize(user.id, card.id, cardPin)) { + this._authorizedUser = user; + this._usersCard = card; + return true; + } + return false; } - public takeUsersMoney(moneyUnits: any): any { - + public takeUsersMoney(moneyUnits: IMoneyUnit[]): boolean { + if (this._authorizedUser) { + this._moneyRepository.takeMoney(moneyUnits); + const amount = moneyUnits.reduce((sum, unit) => { + return sum + unit.count * (+unit.moneyInfo.denomination.match(/\d/g).join('')); + }, 0); + this._usersCard.balance += amount; + return true; + } + return false } - public giveOutUsersMoney(count: any): any { - + public giveOutUsersMoney(count: number): boolean { + if (this._authorizedUser && this._usersCard.balance >= count) { + if (this._moneyRepository.giveOutMoney(count, this._usersCard.currency)) { + this._usersCard.balance -= count; + return true; + } + } + return false; } - public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any { - + public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean { + if (this._authorizedUser) { + if (this._userSettingsModule.changeUserSettings(option, argsForChangeFunction)) { + return true; + } + this._userSettingsModule.user = this._authorizedUser; + return this._userSettingsModule.changeUserSettings(option, argsForChangeFunction); + } + return false; } - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: any): any { - + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit[]): boolean { + const amount = moneyUnits.reduce((sum, unit) => { + return sum + unit.count * (+unit.moneyInfo.denomination.match(/\d/g).join('')); + }, 0); + if (this.giveOutUsersMoney(amount)){ + const convertUnits = this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits); + return this.takeUsersMoney(convertUnits); + } + return false; } } \ No newline at end of file From bdc2787ab70917e48d0f2f5ed66e04d1d8d0edb6 Mon Sep 17 00:00:00 2001 From: Yaroslav Komarov Date: Fri, 16 Apr 2021 18:43:03 +0500 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=201,?= =?UTF-8?q?=202,=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 59 +++++++++++++++++++++++++++++---------------- src/task_2/index.ts | 4 +-- src/task_4/index.ts | 17 ++++--------- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index afc26e3..8591350 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -32,33 +32,50 @@ export interface IMoneyUnit { export class MoneyRepository { private _repository: IMoneyUnit[]; - private static readonly _usNominal: number[] = [100, 50, 20, 10, 5, 2, 1]; - private static readonly _ruNominal: number[] = [5000, 2000, 1000, 500, 200, 100, 50]; constructor(initialRepository: IMoneyUnit[]) { this._repository = initialRepository; } - private static _splitAmount(value: number, currency: Currency): [string, number][] { - let result: [string, number][] = []; - const denominations: number[] = currency === Currency.RUB ? this._ruNominal : this._usNominal; - denominations.forEach(nominal => { - if (nominal < value){ - const count: number = Math.floor(value / nominal); - result.push([`${nominal}${Currency[currency]}`, count]); - value -= nominal * count; - } - }) - return result; + private _sortRepository() { + this._repository.sort((a, b) => { + const tmpA = Number(a.moneyInfo.denomination.match(/\d/g).join()); + const tmpB = Number(b.moneyInfo.denomination.match(/\d/g).join()); + return tmpA > tmpB ? -1 : 1; + }); + } + + private _removeFromRepos(pairArray: [number, IMoneyUnit][]) { + for(const pair of pairArray) { + pair[1].count -= pair[0]; + } } - public giveOutMoney(count: number, currency: Currency): boolean { - const nominalCountPair = MoneyRepository._splitAmount(count, currency); - nominalCountPair.forEach(pair => { - if (!this._repository.find(unit => unit.moneyInfo.denomination === pair[0] && unit.count >= pair[1])) { - return false; + public giveOutMoney(count: number, currency: Currency): IMoneyUnit[] { + this._sortRepository(); + //Промежуточное хранилище для того, чтобы фиксировать денеж. ед, которые надо будет удалить, если удасться "жадно" набрать сумму из хранилища + const countDenominationsPair: [number, IMoneyUnit][] = []; + const result: IMoneyUnit[] = []; + for(const unit of this._repository) { + const denomination = Number(unit.moneyInfo.denomination.match(/\d/g).join()); + if(denomination <= count) { + const tmpCount = Math.floor(count / denomination); + const val = tmpCount <= unit.count ? tmpCount : unit.count; + count -= val === tmpCount ? val * denomination : unit.count * denomination; + countDenominationsPair.push([val, unit]); + result.push({ + count: val, + moneyInfo: { + denomination: unit.moneyInfo.denomination, + currency: unit.moneyInfo.currency + }}); + if (count === 0) { + //Ниже следует метод для изъятия из хранилища купюр. + this._removeFromRepos(countDenominationsPair); + return result; + } } - }) - return true; + } + return null; } public takeMoney(moneyUnits: IMoneyUnit[]) { @@ -77,4 +94,4 @@ export class MoneyRepository { } }) } -} \ No newline at end of file +} diff --git a/src/task_2/index.ts b/src/task_2/index.ts index e72908d..114b95f 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -42,13 +42,13 @@ export class BankOffice { } public authorize(userId: string, cardId: string, cardPin: string): boolean { - this._users.forEach(user => { + for(const user of this._users) { if (user.id === userId){ if (user.cards.find(card => card.id === cardId && card.pin === cardPin)){ return true; } } - }) + } return false; } diff --git a/src/task_4/index.ts b/src/task_4/index.ts index a06b23e..287ca16 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -27,17 +27,10 @@ export class CurrencyConverterModule { public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit[]): IMoneyUnit[] { const coeff = fromCurrency === Currency.RUB ? 70 : 1/70; - let result: IMoneyUnit[] = []; - moneyUnits.forEach(moneyUnit => { - const tmpObj: IMoneyUnit = { - moneyInfo: { - denomination: (+moneyUnit.moneyInfo.denomination * coeff).toString(), - currency: toCurrency, - }, - count: moneyUnit.count, - } - result.push(tmpObj); - }) - return result; + const count = moneyUnits.reduce((accumulator, unit) => { + return accumulator + unit.count * +unit.moneyInfo.denomination; + }, 0) * coeff; + + return this._moneyRepository.giveOutMoney(count, toCurrency); } } \ No newline at end of file From 5ec1606f401af3d12792532a4bd0fffd990901dd Mon Sep 17 00:00:00 2001 From: Yaroslav Komarov Date: Fri, 16 Apr 2021 18:48:43 +0500 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 8591350..fa10b94 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -50,6 +50,16 @@ export class MoneyRepository { } } + private _createMoneyUnitFromUnit(count: number, unit: IMoneyUnit): IMoneyUnit { + return { + moneyInfo: { + denomination: unit.moneyInfo.denomination, + currency: unit.moneyInfo.currency, + }, + count: count, + } + } + public giveOutMoney(count: number, currency: Currency): IMoneyUnit[] { this._sortRepository(); //Промежуточное хранилище для того, чтобы фиксировать денеж. ед, которые надо будет удалить, если удасться "жадно" набрать сумму из хранилища @@ -62,12 +72,7 @@ export class MoneyRepository { const val = tmpCount <= unit.count ? tmpCount : unit.count; count -= val === tmpCount ? val * denomination : unit.count * denomination; countDenominationsPair.push([val, unit]); - result.push({ - count: val, - moneyInfo: { - denomination: unit.moneyInfo.denomination, - currency: unit.moneyInfo.currency - }}); + result.push(this._createMoneyUnitFromUnit(val, unit)); if (count === 0) { //Ниже следует метод для изъятия из хранилища купюр. this._removeFromRepos(countDenominationsPair);