From 4aea920a93b769f1af238c9d652969d051cd2560 Mon Sep 17 00:00:00 2001 From: evgeniy Date: Thu, 8 Apr 2021 19:26:43 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B9=D0=BB=D0=BE?= =?UTF-8?q?=D0=B2=D1=81=D0=BA=D0=B8=D0=B9=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 30 +++++++++++++++++++++++------- src/task_2/index.ts | 44 +++++++++++++++++++++++++++++++++----------- src/task_3/index.ts | 37 ++++++++++++++++++++++++++++--------- src/task_4/index.ts | 12 +++++++++--- src/task_5/index.ts | 37 +++++++++++++++++++++++++++---------- 5 files changed, 120 insertions(+), 40 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 5827d9c..3cc2e15 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -31,16 +31,32 @@ 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 { + let counter = count; + this._repository.forEach(point =>{ + for (let pin in point) { + + if (point[currency] === currency && counter >= point[count]) { + counter = counter - point[count]; + point[count] = 0; + } + } + }); + + if (counter === 0) { + return true + } else { + return false + } } - public takeMoney(moneyUnits: any): any { - + public takeMoney(moneyUnits: IMoneyUnit): void { + this._repository.push(moneyUnits); } -} \ No newline at end of file +} + diff --git a/src/task_2/index.ts b/src/task_2/index.ts index fe95180..3cd56c5 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -1,3 +1,4 @@ +import { ICard } from './index'; /** Задача 1 - BankOffice * Имеется класс BankOffice. Который должен хранить пользователей и банковские карты. * Пользователи банка могу иметь карту, а могут не иметь. @@ -17,7 +18,7 @@ import { Currency } from '../enums'; -interface ICard { +export interface ICard { id: string; balance: number; currency: Currency, @@ -28,27 +29,48 @@ export interface IBankUser { id: string; name: string; surname: string; - cards: Array; + cards?: Array; } 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 { + for (let users of this._users) { + if (users.id === userId) { + if (users.cards.find(card => card.id === cardId && card.pin === cardPin)){ + return true; + } + } + } + return false; + } } - public getCardById(cardId: any): any { + public getCardById(cardId: string): ICards { + for (let card of this._cards) { + if (card.id === cardId) { + return card; + } + } + return null; } - public isCardTiedToUser(cardId: any): any { + public isCardTiedToUser(cardId: string): boolean { + for (let user of this._users) { + for (let userCard of user.cards) { + if (userCard.id === cardId) { + return true; + } + } + } + return false; - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 9020644..675ffe7 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -1,3 +1,4 @@ +import { BankOffice, IBankUser } from './../task_2/index'; /** Задача 3 - UserSettingsModule * Имеется класс UserSettingsModule. Который должен отвечать за * изменение настроек пользователя. @@ -20,30 +21,48 @@ import { UserSettingOptions } from '../enums'; 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) return false + this._user.name = newName; + return true; } - private changeUserSurname(newSurname: any): any { + private changeUserSurname(newSurname: string): boolean { + if(!this._user) return false + this._user.surname = newSurname; + return true; } - 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..e2f9e6c 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -18,13 +18,19 @@ import { Currency } from '../enums'; 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: Array): Array { + if(fromCurrency === toCurrency) return moneyUnits; + let amount: number = 0; + moneyUnits.forEach(x => amount += x.count * Number(x.moneyInfo.denomination)); + let fromRub = fromCurrency === Currency.RUB; + let money = fromRub ? (amount - amount % 70) / 70 : Math.floor(amount) * 70; + return this._moneyRepository.convertMoney(money, fromRub ? Currency.USD : Currency.RUB, moneyUnits); } } \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index 395285e..6cc0293 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -28,30 +28,47 @@ class BankTerminal { private _currencyConverterModule: CurrencyConverterModule; private _authorizedUser: IBankUser; - 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): any { - + public authorizeUser(user: IBankUser, card: ICard, cardPin: string): boolean { + if(!this._bankOffice.authorize(user.id, card.id, cardPin)) return false; + { + this._authorizedUser = user; + this._usersCard = card; + return true; + } + return false; } - public takeUsersMoney(moneyUnits: any): any { - + public takeUsersMoney(moneyUnits: IMoneyUnit[]): boolean { + if(!this._authorizedUser || this._authorizedUser.cards.length === 0) return false; + this._moneyRepository.takeMoney(moneyUnits); + moneyUnits.forEach(unit => this._authorizedUser.cards[0].balance += unit.count * Number(unit.moneyInfo.denomination)); + return true; } - public giveOutUsersMoney(count: any): any { + public giveOutUsersMoney(count: number): boolean { + if (typeof this._authorizedUser !== "undefined" && count > 0) { + let userCard = this._authorizedUser.cards[0]; + if (this._moneyRepository.giveOutMoney(count, userCard.currency)) { + return this._bankOffice.removeMoneyFromCartById(userCard.id, count); + } + return false; + } + return false; } - public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any { - + public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean { + return this._userSettingsModule.changeUserSettings(option, argsForChangeFunction); } - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: any): any { - + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: Array): Array { + return this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits); } } \ No newline at end of file From fbfe6bb98bfeb566c321664e597a440e1dfe5410 Mon Sep 17 00:00:00 2001 From: evgeniy Date: Thu, 15 Apr 2021 09:36:08 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5?= 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 | 46 ++++++++----------------- src/task_3/index.ts | 42 ++++++++++++----------- src/task_4/index.ts | 25 +++++++++----- src/task_5/index.ts | 52 ++++++++++++++-------------- 5 files changed, 142 insertions(+), 106 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 3cc2e15..f5873d5 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -31,32 +31,77 @@ 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 { - let counter = count; - this._repository.forEach(point =>{ - for (let pin in point) { - - if (point[currency] === currency && counter >= point[count]) { - counter = counter - point[count]; - point[count] = 0; - } + public giveOutMoney(count: number, currency: Currency): boolean { + let givenMoney: Array = new Array(); + this._repository + .filter(function (unit){ + return unit.moneyInfo.currency === currency + }) + .sort(function (a, b) { + return Number(a.moneyInfo.denomination) > Number(b.moneyInfo.denomination) ? -1 : 1; + }) + .forEach(unit => { + let currentDenomination = Number(unit.moneyInfo.denomination); + let amount = Math.min((count - count % currentDenomination) / currentDenomination, unit.count); + count -= amount * currentDenomination; + givenMoney.push({ + moneyInfo:{ + denomination: unit.moneyInfo.denomination, + currency: unit.moneyInfo.currency + }, + count: amount + }); + }); + if(count === 0){ + this.updateRepository(givenMoney, currency); + return true; } + return false; + } + + public updateRepository(givenMoney: Array, currency: Currency):void { + this._repository.forEach((value, index) => { + let current = givenMoney.find(x => x.moneyInfo.denomination === this._repository[index].moneyInfo.denomination); + if(this._repository[index].moneyInfo.currency === currency && current){ + this._repository[index].count -= current.count; + } }); + } - if (counter === 0) { - return true - } else { - return false + public convertMoney(count: number, currency: Currency, moneyUnits: Array):Array{ + let res: Array = new Array(); + let denominations = currency === Currency.RUB ? + [5000,2000,1000,500,200,100,50,10]: + [100,50,20,10,5,2,1]; + denominations.forEach(x=> { + let amount = (count - count % x) / x; + if (amount === 0 || this._repository.filter(y => y.moneyInfo.currency === currency + && y.moneyInfo.denomination === x.toString() && y.count >= amount).length === 0) return; + res.push({ + moneyInfo:{ + denomination: x.toString(), + currency: currency + }, + count: amount + }); + count -= amount * x; + }) + if(res.length > 0){ + this.takeMoney(moneyUnits); } + return res; } - public takeMoney(moneyUnits: IMoneyUnit): void { - this._repository.push(moneyUnits); + public takeMoney(moneyUnits: Array): void { + moneyUnits.forEach(unit => { + let unitExistIndex = this._repository + .findIndex(x => JSON.stringify(x.moneyInfo) === JSON.stringify(unit.moneyInfo)); + unitExistIndex !== -1 ? this._repository[unitExistIndex].count += unit.count : this._repository.push(unit); + }); } -} - +} \ No newline at end of file diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 3cd56c5..d96a426 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -1,4 +1,3 @@ -import { ICard } from './index'; /** Задача 1 - BankOffice * Имеется класс BankOffice. Который должен хранить пользователей и банковские карты. * Пользователи банка могу иметь карту, а могут не иметь. @@ -29,48 +28,31 @@ export interface IBankUser { id: string; name: string; surname: string; - cards?: Array; + cards: Array; } export class BankOffice { - private _users: IBankUser; - private _cards: ICard; + private _users: Array; + private _cards: Array; - constructor(users: IBankUser, cards: ICard) { + constructor(users: Array, cards: Array) { this._users = users; this._cards = cards; } public authorize(userId: string, cardId: string, cardPin: string): boolean { - for (let users of this._users) { - if (users.id === userId) { - if (users.cards.find(card => card.id === cardId && card.pin === cardPin)){ - return true; - } - } - } - return false; - } + let user = this._users.find(x => x.id === userId); + if(!user) return false; + let card = user.cards.find(x=> x.id === cardId); + if(!card) return false; + return card.pin === cardPin; } - public getCardById(cardId: string): ICards { - for (let card of this._cards) { - if (card.id === cardId) { - return card; - } - } - return null; - + public getCardById(cardId: string): ICard | undefined { + return this._cards.find(x=> x.id === cardId); } public isCardTiedToUser(cardId: string): boolean { - for (let user of this._users) { - for (let userCard of user.cards) { - if (userCard.id === cardId) { - return true; - } - } - } - return false; - - } \ No newline at end of file + return this._users.filter(x=> x.cards.find(y=> y.id === cardId)).length > 0; + } +} \ No newline at end of file diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 675ffe7..53d5f59 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -1,4 +1,3 @@ -import { BankOffice, IBankUser } from './../task_2/index'; /** Задача 3 - UserSettingsModule * Имеется класс UserSettingsModule. Который должен отвечать за * изменение настроек пользователя. @@ -18,7 +17,7 @@ import { BankOffice, IBankUser } from './../task_2/index'; * пользуясь уже предоставленными интерфейсами (избавиться от всех any типов) */ -import { UserSettingOptions } from '../enums'; +import { IBankUser, BankOffice } from '../task_2'; export class UserSettingsModule { private _bankOffice: BankOffice; @@ -32,37 +31,40 @@ export class UserSettingsModule { this._bankOffice = initialBankOffice; } - private changeUserName(newName: String): boolean { - if(!this._user) return false - this._user.name = newName; - return true; + private changeUserName(newName: string): boolean { + if (newName) { + this._user.name = newName; + return true; + } else{ + return false; + } } private changeUserSurname(newSurname: string): boolean { - if(!this._user) return false - this._user.surname = newSurname; - return true; - + if (newSurname) { + this._user.name = newSurname; + return true; + } else + return false; } private registerForUserNewCard(newCardId: string): boolean { - const newCard = this._bankOffice.getCardById(newCardId); - if (this._user && newCard && !this._bankOffice.isCardTiedToUser(newCardId)) { - this._user.cards.push(newCard); + let card = this._bankOffice.getCardById(newCardId); + if (card && !this._bankOffice.isCardTiedToUser(newCardId)) + { + this._user.cards.push(card); return true; } - return false; - + else + return false; } public changeUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean { - if (option === UserSettingOptions.name){ + if (option === UserSettingOptions.name) return this.changeUserName(argsForChangeFunction); - } else if (option === UserSettingOptions.surname){ + else if (option === UserSettingOptions.surname) return this.changeUserSurname(argsForChangeFunction); - } else { + else if (option === UserSettingOptions.newCard) 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 e2f9e6c..a06b23e 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -16,21 +16,28 @@ */ import { Currency } from '../enums'; +import { IMoneyUnit, MoneyRepository } from '../task_1'; export class CurrencyConverterModule { private _moneyRepository: MoneyRepository; - constructor(initialMoneyRepository: MoneyRepository) { + constructor(initialMoneyRepository: MoneyRepository) { this._moneyRepository = initialMoneyRepository; } - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: Array): Array { - if(fromCurrency === toCurrency) return moneyUnits; - let amount: number = 0; - moneyUnits.forEach(x => amount += x.count * Number(x.moneyInfo.denomination)); - let fromRub = fromCurrency === Currency.RUB; - let money = fromRub ? (amount - amount % 70) / 70 : Math.floor(amount) * 70; - return this._moneyRepository.convertMoney(money, fromRub ? Currency.USD : Currency.RUB, moneyUnits); - + 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 6cc0293..5004bad 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,6 +27,7 @@ class BankTerminal { private _userSettingsModule: UserSettingsModule; private _currencyConverterModule: CurrencyConverterModule; private _authorizedUser: IBankUser; + private _card: ICard; constructor(initBankOffice: BankOffice, initMoneyRepository: MoneyRepository) { this._moneyRepository = initMoneyRepository; @@ -35,40 +36,39 @@ class BankTerminal { this._currencyConverterModule = new CurrencyConverterModule(initMoneyRepository); } - public authorizeUser(user: IBankUser, card: ICard, cardPin: string): boolean { - if(!this._bankOffice.authorize(user.id, card.id, cardPin)) return false; + public authorizeUser(user: IBankUser, card: ICard, cardPin: string) { + if (this._bankOffice.authorize(user.id, card.id, cardPin)) { - this._authorizedUser = user; - this._usersCard = card; - return true; + this._authorizedUser = user; + this._card = card; } - return false; } - public takeUsersMoney(moneyUnits: IMoneyUnit[]): boolean { - if(!this._authorizedUser || this._authorizedUser.cards.length === 0) return false; - this._moneyRepository.takeMoney(moneyUnits); - moneyUnits.forEach(unit => this._authorizedUser.cards[0].balance += unit.count * Number(unit.moneyInfo.denomination)); - return true; + public takeUsersMoney(moneyUnits: Array) { + if (this._authorizedUser) + { + this._moneyRepository.takeMoney(moneyUnits); + } } - - public giveOutUsersMoney(count: number): boolean { - if (typeof this._authorizedUser !== "undefined" && count > 0) { - let userCard = this._authorizedUser.cards[0]; - if (this._moneyRepository.giveOutMoney(count, userCard.currency)) { - return this._bankOffice.removeMoneyFromCartById(userCard.id, count); - } - return false; + public giveOutUsersMoney(count: number) { + if (this._authorizedUser) + { + this._moneyRepository.giveOutMoney(count, this._card.currency); } - return false; } - public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean { - return this._userSettingsModule.changeUserSettings(option, argsForChangeFunction); + public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string) { + if (this._authorizedUser) + { + this._userSettingsModule.changeUserSettings(option, argsForChangeFunction); + } } - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: Array): Array { - return this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits); + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: Array) { + if (this._authorizedUser) + { + this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits); + } } } \ No newline at end of file