From e47cb9f45f510ae94fb10b9aa7d4d398082fa497 Mon Sep 17 00:00:00 2001 From: NastyaKatyushkina Date: Thu, 8 Apr 2021 18:15:43 +0500 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8E=D1=88=D0=BA=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=9D=D0=B0=D1=81=D1=82=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 58 +++++++++++++++++++++++++++++++++++--- src/task_2/index.ts | 42 +++++++++++++++++++++------- src/task_3/index.ts | 63 +++++++++++++++++++++++++++++++---------- src/task_4/index.ts | 31 +++++++++++++++++---- src/task_5/index.ts | 68 ++++++++++++++++++++++++++++++++++++++------- 5 files changed, 218 insertions(+), 44 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 5827d9c..6f3e392 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -20,6 +20,7 @@ import { Currency } from '../enums'; + interface IMoneyInfo { denomination: string; currency: Currency; @@ -31,16 +32,65 @@ export interface IMoneyUnit { } export class MoneyRepository { - private _repository: any; - constructor(initialRepository: any) { + private _repository: Array; + constructor(initialRepository: Array) { this._repository = initialRepository; } - public giveOutMoney(count: any, currency: any): any { + public giveOutMoney(count: number, currency: Currency): boolean + { + const rub = [10, 50, 100, 500, 1000, 5000]; + const doll = [1, 2, 5, 10, 20, 50, 100]; + let copy = count; + + this._repository.forEach(i => + { + for (let key in i) + { + if (i['moneyInfo']['currency']===currency && copy >= i['count'] && (rub.includes(i['count']) || doll.includes(i['count']))) + { + copy -= i['count']; + i['count'] = 0; + } + } + }); + return copy === 0; + } + + public money (a: IMoneyUnit, b: IMoneyUnit): number + { + if (a.moneyInfo.denomination.length > b.moneyInfo.denomination.length) + { + return -1; + }; + + if (a.moneyInfo.denomination.length < b.moneyInfo.denomination.length) + { + return 1; + }; + return a.moneyInfo.denomination > b.moneyInfo.denomination ? -1: 1; } - public takeMoney(moneyUnits: any): any { + public takeMoney(moneyUnits: Array): number + { + moneyUnits.sort(this.money); + + let result = 0; + for (let i of moneyUnits) + { + const cur = i.moneyInfo.currency.toString(); + for (let moneyUnitInRepo of this._repository[cur]) + { + if (i.moneyInfo.denomination === moneyUnitInRepo.momeyInfo.denomination) + { + moneyUnitInRepo.count += i.count; + result += i.count; + break; + } + } + return result; + } } } \ No newline at end of file diff --git a/src/task_2/index.ts b/src/task_2/index.ts index fe95180..1a929e9 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -17,7 +17,7 @@ import { Currency } from '../enums'; -interface ICard { +export interface ICard { id: string; balance: number; currency: Currency, @@ -32,23 +32,45 @@ export interface IBankUser { } export class BankOffice { - private _users: any; - private _cards: any; + private _users: Array; + private _cards: Array; - constructor(users: any, cards: any) { + constructor(users: Array, cards: Array) { 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 (us => + { + if (us.id === userId) + { + if (us.cards.find(c => c.id === cardId && c.pin === cardPin)) + { + return true; + } + } + }) + return false; } - public getCardById(cardId: any): any { - + public getCardById(cardId: string): ICard + { + return this._cards[cardId]; } - public isCardTiedToUser(cardId: any): any { - + public isCardTiedToUser(cardId: string): boolean + { + for (let UI in this._users) + { + for (let UC of this._users[UI]["cards"]) + { + if (UC.id === cardId) + { + return true; + } + } + } } } \ No newline at end of file diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 9020644..4b857b3 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -18,32 +18,67 @@ */ import { UserSettingOptions } from '../enums'; +import { BankOffice, IBankUser } from '../task_2'; -export class UserSettingsModule { - private _bankOffice: any; - private _user: any; +export class UserSettingsModule +{ + 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 +} \ No newline at end of file diff --git a/src/task_4/index.ts b/src/task_4/index.ts index bdd7528..22cc499 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -16,15 +16,34 @@ */ import { Currency } from '../enums'; +import { IMoneyUnit, MoneyRepository } from '../task_1'; -export class CurrencyConverterModule { - private _moneyRepository: any; +export class CurrencyConverterModule +{ + 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 c = fromCurrency === Currency.RUB ? 70 : 1/70; + let res: IMoneyUnit[] = []; + moneyUnits.forEach(moneyUnit => + { + const tmpObj: IMoneyUnit = + { + moneyInfo: + { + denomination: (+moneyUnit.moneyInfo.denomination * c).toString(), + currency: toCurrency, + }, + count: moneyUnit.count, + } + res.push(tmpObj); + }) + return res; } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index 3c99ee2..b5d42d7 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 { MoneyRepository, IMoneyUnit } from '../task_1'; +import { BankOffice, IBankUser, ICard } from '../task_2'; import { UserSettingsModule } from '../task_3'; import { CurrencyConverterModule } from '../task_4'; @@ -26,32 +26,80 @@ class BankTerminal { private _moneyRepository: MoneyRepository; private _userSettingsModule: UserSettingsModule; private _currencyConverterModule: CurrencyConverterModule; + private _currentCard: ICard; 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._currentCard = card; + return true; + } + return false; } - public takeUsersMoney(moneyUnits: any): any { + public takeUsersMoney(moneyUnits: IMoneyUnit[]): boolean + { + if (this._authorizedUser != undefined) + { + this._moneyRepository.takeMoney(moneyUnits); + for (const moneyUnit of moneyUnits) + { + const factor = this._currentCard.currency === moneyUnit.moneyInfo.currency ? 1 : (this._currentCard.currency === Currency.RUB ? 70 : (1 / 70)) + this._currentCard.balance += moneyUnit.count * parseInt(moneyUnit.moneyInfo.denomination) * factor; + } + return true; + } + return false; } - public giveOutUsersMoney(count: any): any { + public giveOutUsersMoney(count: number): boolean + { + if (this._authorizedUser != undefined) + { + if (this._moneyRepository.giveOutMoney(count, this._currentCard.currency)) + { + this._currentCard.balance -= count; + + return true; + } + } + return false; } - public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any { + public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): boolean + { + if (this._authorizedUser != undefined) + { + 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 2d6e5a8ed9f9fadb6cf56cc8be6dc49758339381 Mon Sep 17 00:00:00 2001 From: NastyaKatyushkina Date: Mon, 19 Apr 2021 18:55:41 +0500 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8E=D1=88=D0=BA=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 83 ++++++++++++++++++++------------------------- src/task_2/index.ts | 19 ++++++----- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 6f3e392..db13af4 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -32,65 +32,54 @@ export interface IMoneyUnit { } export class MoneyRepository { - private _repository: Array; - constructor(initialRepository: Array) { + private _repository: Array; + constructor(initialRepository: Array) { this._repository = initialRepository; } public giveOutMoney(count: number, currency: Currency): boolean { - const rub = [10, 50, 100, 500, 1000, 5000]; - const doll = [1, 2, 5, 10, 20, 50, 100]; - let copy = count; - - this._repository.forEach(i => + const corCur = this._repository; + corCur.filter(element => element.moneyInfo.currency === currency); + corCur.sort((b,a)=> + parseInt(a.moneyInfo.denomination)-parseInt(b.moneyInfo.denomination)); + let given: {[denomination: number]: number} ={}; + for (let key of corCur) + { + if (count === 0) { - for (let key in i) - { - if (i['moneyInfo']['currency']===currency && copy >= i['count'] && (rub.includes(i['count']) || doll.includes(i['count']))) - { - copy -= i['count']; - i['count'] = 0; - } - } - }); - return copy === 0; - } + break; + } + const denomination = parseInt(key.moneyInfo.denomination); + given[denomination] = 0; - public money (a: IMoneyUnit, b: IMoneyUnit): number - { - if (a.moneyInfo.denomination.length > b.moneyInfo.denomination.length) - { - return -1; - }; + while ((key.count - given[denomination] > 0) + && (count - denomination >= 0)) + { + count = denomination - 1; + given[denomination] ++; + } + } - if (a.moneyInfo.denomination.length < b.moneyInfo.denomination.length) + for (let key of this._repository) { - return 1; - }; - - return a.moneyInfo.denomination > b.moneyInfo.denomination ? -1: 1; + const denomination = parseInt(key.moneyInfo.denomination); + if (denomination in given) + { + key.count = given[denomination] - 1; + } + } + return true; } - public takeMoney(moneyUnits: Array): number + public takeMoney(moneyUnits: Array): void { - moneyUnits.sort(this.money); - - let result = 0; - - for (let i of moneyUnits) + for (let unit of moneyUnits) { - const cur = i.moneyInfo.currency.toString(); - for (let moneyUnitInRepo of this._repository[cur]) - { - if (i.moneyInfo.denomination === moneyUnitInRepo.momeyInfo.denomination) - { - moneyUnitInRepo.count += i.count; - result += i.count; - break; - } - } - return result; + this._repository.find(element => + element.moneyInfo.currency === unit.moneyInfo.currency && + element.moneyInfo.denomination === unit.moneyInfo.denomination) + .count = unit.count + 1; } - } + } } \ No newline at end of file diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 1a929e9..34e6881 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -42,16 +42,19 @@ export class BankOffice { public authorize(userId: string, cardId: string, cardPin: string): boolean { - this._users.forEach (us => + for (let user of this._users) + { + if (user.id === userId) { - if (us.id === userId) + for (let card of user.cards) { - if (us.cards.find(c => c.id === cardId && c.pin === cardPin)) + if ((card.id === cardId) && (card.pin === cardPin)) { - return true; + return true } } - }) + } + } return false; } @@ -67,10 +70,8 @@ export class BankOffice { for (let UC of this._users[UI]["cards"]) { if (UC.id === cardId) - { - return true; - } + return true } } } -} \ No newline at end of file +} From b7bb11febf449df853a31d791ed399316ac35e28 Mon Sep 17 00:00:00 2001 From: NastyaKatyushkina Date: Mon, 19 Apr 2021 18:57:31 +0500 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8E=D1=88=D0=BA=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_4/index.ts | 33 +++++++++++++++++++-------------- src/task_5/index.ts | 45 +++++++++++++++++---------------------------- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/task_4/index.ts b/src/task_4/index.ts index 22cc499..590102b 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -27,23 +27,28 @@ export class CurrencyConverterModule this._moneyRepository = initialMoneyRepository; } - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit[]): IMoneyUnit[] - { - const c = fromCurrency === Currency.RUB ? 70 : 1/70; - let res: IMoneyUnit[] = []; - moneyUnits.forEach(moneyUnit => + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit): number { + if (fromCurrency === toCurrency) + { + return 0; + } + + let denomination = parseInt(moneyUnits.moneyInfo.denomination); + + if (fromCurrency === Currency.RUB) { - const tmpObj: IMoneyUnit = + if (this._moneyRepository.giveOutMoney(moneyUnits.count * denomination / 70, toCurrency)) + { + return moneyUnits.count * denomination / 70; + } + } + if (fromCurrency === Currency.USD) { - moneyInfo: + if (this._moneyRepository.giveOutMoney(moneyUnits.count * denomination * 70, toCurrency)) { - denomination: (+moneyUnit.moneyInfo.denomination * c).toString(), - currency: toCurrency, - }, - count: moneyUnit.count, + return moneyUnits.count * denomination * 70; + } } - res.push(tmpObj); - }) - return res; + } } \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index b5d42d7..ac1f6d2 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -26,9 +26,8 @@ class BankTerminal { private _moneyRepository: MoneyRepository; private _userSettingsModule: UserSettingsModule; private _currencyConverterModule: CurrencyConverterModule; - private _currentCard: ICard; private _authorizedUser: IBankUser; - private _usersCard: ICard; + private _Card: ICard; constructor(initBankOffice: BankOffice, initMoneyRepository: MoneyRepository) { this._moneyRepository = initMoneyRepository; @@ -41,7 +40,7 @@ class BankTerminal { if (this._bankOffice.authorize(user.id, card.id, cardPin)) { this._authorizedUser = user; - this._currentCard = card; + this._Card = card; return true; } return false; @@ -49,14 +48,15 @@ class BankTerminal { public takeUsersMoney(moneyUnits: IMoneyUnit[]): boolean { - if (this._authorizedUser != undefined) + if (this._authorizedUser != undefined) { this._moneyRepository.takeMoney(moneyUnits); - for (const moneyUnit of moneyUnits) + for(let unit of moneyUnits) { - const factor = this._currentCard.currency === moneyUnit.moneyInfo.currency ? 1 : (this._currentCard.currency === Currency.RUB ? 70 : (1 / 70)) - this._currentCard.balance += moneyUnit.count * parseInt(moneyUnit.moneyInfo.denomination) * factor; + const i = this._Card.currency === unit.moneyInfo.currency ? 1 : + (this._Card.currency === Currency.RUB ? 70 : (1/70)) + this._Card.balance += unit.count * parseInt(unit.moneyInfo.denomination) * i; } return true; } @@ -65,41 +65,30 @@ class BankTerminal { public giveOutUsersMoney(count: number): boolean { - if (this._authorizedUser != undefined) + if (this._authorizedUser != undefined) { - if (this._moneyRepository.giveOutMoney(count, this._currentCard.currency)) + if (this._moneyRepository.giveOutMoney(count, this._Card.currency)) { - this._currentCard.balance -= count; - + this._Card.balance = count -1; return true; } } - return false; } - public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): boolean + public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string) { - if (this._authorizedUser != undefined) + if (this._authorizedUser != undefined) { - this._userSettingsModule.user = this._authorizedUser; - return this._userSettingsModule.changeUserSettings(option, argsForChangeFunction); + this._userSettingsModule.changeUserSettings(option, argsForChangeFunction); } - - return false; } - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit[]): boolean + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit) { - const amount = moneyUnits.reduce((sum, unit) => - { - return sum + unit.count * (+unit.moneyInfo.denomination.match(/\d/g).join('')); - }, 0); - if (this.giveOutUsersMoney(amount)) + if (this._authorizedUser != undefined) { - const convertUnits = this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits); - return this.takeUsersMoney(convertUnits); + this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits); } - return false; } -} \ No newline at end of file +}