diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 5827d9c..db13af4 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,54 @@ 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 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) + { + break; + } + const denomination = parseInt(key.moneyInfo.denomination); + given[denomination] = 0; - public takeMoney(moneyUnits: any): any { + while ((key.count - given[denomination] > 0) + && (count - denomination >= 0)) + { + count = denomination - 1; + given[denomination] ++; + } + } + for (let key of this._repository) + { + const denomination = parseInt(key.moneyInfo.denomination); + if (denomination in given) + { + key.count = given[denomination] - 1; + } + } + return true; } + + public takeMoney(moneyUnits: Array): void + { + for (let unit of moneyUnits) + { + 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 fe95180..34e6881 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,46 @@ 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 + { + for (let user of this._users) + { + if (user.id === userId) + { + for (let card of user.cards) + { + if ((card.id === cardId) && (card.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..590102b 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -16,15 +16,39 @@ */ 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): number { + if (fromCurrency === toCurrency) + { + return 0; + } + + let denomination = parseInt(moneyUnits.moneyInfo.denomination); + + if (fromCurrency === Currency.RUB) + { + if (this._moneyRepository.giveOutMoney(moneyUnits.count * denomination / 70, toCurrency)) + { + return moneyUnits.count * denomination / 70; + } + } + if (fromCurrency === Currency.USD) + { + if (this._moneyRepository.giveOutMoney(moneyUnits.count * denomination * 70, toCurrency)) + { + return moneyUnits.count * denomination * 70; + } + } } -} \ 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..ac1f6d2 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'; @@ -27,31 +27,68 @@ class BankTerminal { private _userSettingsModule: UserSettingsModule; private _currencyConverterModule: CurrencyConverterModule; private _authorizedUser: IBankUser; + private _Card: 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._Card = card; + return true; + } + return false; } - public takeUsersMoney(moneyUnits: any): any { + public takeUsersMoney(moneyUnits: IMoneyUnit[]): boolean + { + if (this._authorizedUser != undefined) + { + this._moneyRepository.takeMoney(moneyUnits); + for(let unit of moneyUnits) + { + 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; + } + return false; } - public giveOutUsersMoney(count: any): any { - + public giveOutUsersMoney(count: number): boolean + { + if (this._authorizedUser != undefined) + { + if (this._moneyRepository.giveOutMoney(count, this._Card.currency)) + { + this._Card.balance = count -1; + return true; + } + } + return false; } - public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any { - + public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string) + { + if (this._authorizedUser != undefined) + { + this._userSettingsModule.changeUserSettings(option, argsForChangeFunction); + } } - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: any): any { - + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit) + { + if (this._authorizedUser != undefined) + { + this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits); + } } -} \ No newline at end of file +}