From 58fc85bb01505559e8017fe1c1b8abae4ddb9543 Mon Sep 17 00:00:00 2001 From: Petrov012110 Date: Tue, 24 Aug 2021 07:30:52 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=82=D1=80=D0=BE=D0=B2=20ts2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 311 ++++++++++++++++++++++++++++++++++++++++++-- src/task_2/index.ts | 64 +++++---- src/task_3/index.ts | 57 +++++--- src/task_4/index.ts | 30 +++-- src/task_5/index.ts | 79 +++++++---- src/types.ts | 31 +++++ 6 files changed, 470 insertions(+), 102 deletions(-) create mode 100644 src/types.ts diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 5827d9c..88f9e8e 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -19,28 +19,313 @@ */ import { Currency } from '../enums'; +import { IMoneyUnit, TCash } from '../types'; -interface IMoneyInfo { - denomination: string; - currency: Currency; -} -export interface IMoneyUnit { - moneyInfo: IMoneyInfo; - count: number; -} export class MoneyRepository { - private _repository: any; - constructor(initialRepository: any) { + private _repository: IMoneyUnit[]; + constructor(initialRepository: IMoneyUnit[]) { this._repository = initialRepository; } - public giveOutMoney(count: any, currency: any): any { + public giveOutMoney(count: number, currency: number): boolean { + let newArr: Array = []; + let i = 0; + let creminder = count; + + while (i < this._repository.length) { + let coincounter = 0; + while (Number(this._repository[i].moneyInfo.denomination) <= creminder && this._repository[i].moneyInfo.currency === currency && this._repository[i].count !== 0) { + creminder = creminder - Number(this._repository[i].moneyInfo.denomination); + coincounter++; + this._repository[i].count -= 1; + } + + if (coincounter !== 0) { + newArr.push({ + count: coincounter, + denomination: this._repository[i].moneyInfo.denomination, + currency: this._repository[i].moneyInfo.currency + }) + } + i++; + } + console.log(newArr); //массив, который показывает какие купюры сняли и их количество + if (creminder == 0) { //если остаток 0 - пользовтель получил все деньги и купюр хватило + return true; + } else { //если бабки снять не получилось, то мы возвращаем их обратно в _repository + newArr.forEach(item => { + this._repository.forEach(el => { + if (item.currency === el.moneyInfo.currency && item.denomination === el.moneyInfo.denomination) { + el.count += item.count; + } + }) + }) + return false; + } } - public takeMoney(moneyUnits: any): any { + public takeMoney(moneyUnits: IMoneyUnit[]): boolean { + + let arrOfTakenMoney = moneyUnits.map(item => { + return this._repository.some(el => { + if (el.moneyInfo.currency === item.moneyInfo.currency && el.moneyInfo.denomination === item.moneyInfo.denomination) { + el.count += item.count; + return true; + } + return false; + }) + }) + + return arrOfTakenMoney.every(item => item === true); + + } +} +let arrOfMoney = [ + { + moneyInfo: { + denomination: "1000", + currency: Currency.RUB + }, + count: 10 + }, + { + moneyInfo: { + denomination: "500", + currency: Currency.RUB + }, + count: 100 + }, + { + moneyInfo: { + denomination: "200", + currency: Currency.RUB + }, + count: 15 + }, + { + moneyInfo: { + denomination: "100", + currency: Currency.USD + }, + count: 20 + }, + { + moneyInfo: { + denomination: "100", + currency: Currency.RUB + }, + count: 15 + }, + { + moneyInfo: { + denomination: "50", + currency: Currency.USD + }, + count: 15 + }, + { + moneyInfo: { + denomination: "50", + currency: Currency.RUB + }, + count: 50 + }, + { + moneyInfo: { + denomination: "10", + currency: Currency.USD + }, + count: 30 + }, + { + moneyInfo: { + denomination: "10", + currency: Currency.RUB + }, + count: 100 + }, + { + moneyInfo: { + denomination: "5", + currency: Currency.USD + }, + count: 30 + }, + { + moneyInfo: { + denomination: "1", + currency: Currency.USD + }, + count: 15 } -} \ No newline at end of file + +] + +let cards = [ + { + id: "0", + balance: 10000, + currency: Currency.RUB, + pin: "0000" + }, + { + id: "1", + balance: 1000, + currency: Currency.RUB, + pin: "0001" + }, + { + id: "2", + balance: 8000, + currency: Currency.RUB, + pin: "0002" + }, + { + id: "3", + balance: 35000, + currency: Currency.RUB, + pin: "0003" + }, + { + id: "4", + balance: 70000, + currency: Currency.RUB, + pin: "0004" + }, + { + id: "5", + balance: 2000, + currency: Currency.USD, + pin: "0005" + }, + { + id: "6", + balance: 5000, + currency: Currency.USD, + pin: "0006" + }, + { + id: "7", + balance: 500, + currency: Currency.USD, + pin: "0007" + }, + { + id: "8", + balance: 500, + currency: Currency.USD, + pin: "0008" + }, + { + id: "9", + balance: 1500, + currency: Currency.USD, + pin: "0009" + }, + { + id: "10", + balance: 7500, + currency: Currency.USD, + pin: "0010" + }, +] + +let users = [ + { + id: "1", + name: "Grisha", + surname: "Petrov", + cards: [ + { + id: "7", + balance: 500, + currency: Currency.USD, + pin: "0007" + }, + { + id: "0", + balance: 10000, + currency: Currency.RUB, + pin: "0000" + } + ] + }, + { + id: "2", + name: "Alex", + surname: "Melik", + cards: [ + { + id: "6", + balance: 5000, + currency: Currency.USD, + pin: "0006" + }, + ] + }, + { + id: "3", + name: "Artem", + surname: "Kojevnikov", + cards: [ + { + id: "1", + balance: 1000, + currency: Currency.RUB, + pin: "0001" + }, + ] + }, + { + id: "4", + name: "Grisha", + surname: "Balagov", + cards: [ + { + id: "2", + balance: 8000, + currency: Currency.RUB, + pin: "0002" + }, + ] + }, + { + id: "5", + name: "Sveta", + surname: "Milavina", + cards: [ + { + id: "4", + balance: 70000, + currency: Currency.RUB, + pin: "0004" + }, + { + id: "5", + balance: 2000, + currency: Currency.USD, + pin: "0005" + }, + ] + }, + { + id: "6", + name: "Masha", + surname: "Hudina", + cards: [ + + ] + }, + { + id: "7", + name: "Hana", + surname: "Look", + cards: [ + + ] + }, +] diff --git a/src/task_2/index.ts b/src/task_2/index.ts index fe95180..b0f6fa2 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -16,39 +16,37 @@ */ import { Currency } from '../enums'; - -interface ICard { - id: string; - balance: number; - currency: Currency, - pin: string, -} - -export interface IBankUser { - id: string; - name: string; - surname: string; - cards: Array; -} +import { IBankUser, ICard } from '../types'; export class BankOffice { - private _users: any; - private _cards: any; - - constructor(users: any, cards: any) { - this._users = users; - this._cards = cards; - } - - public authorize(userId: any, cardId: any, cardPin: any): any { - - } - - public getCardById(cardId: any): any { - - } - - public isCardTiedToUser(cardId: any): any { - - } + private _users: Array; + private _cards: ICard[]; + + constructor(users: IBankUser[], cards: ICard[]) { + this._users = users; + this._cards = cards; + } + + public authorize(userId: string, cardId: string, cardPin: string): boolean { + return this._users.filter(item => item.id === userId).some(el => el.cards.some(i => i.id === cardId && i.pin === cardPin)) + // let checkCardAndUser = this._users.filter(item => { + // if (item.id === userId) return item.cards.some(el => (el.id === cardId && el.pin === cardPin)); + // }); + // if (checkCardAndUser.length) return true; + // else return false; + } + + public getCardById(cardId: string): ICard { + let cardObj: ICard; + this._cards.forEach(item => { + if (item.id === cardId) { + cardObj = item; + } + }); + return cardObj; + } + + public isCardTiedToUser(cardId: string): boolean { + return this._users.some(item => item.cards.some(el => el.id === cardId)); + } } \ No newline at end of file diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 9020644..374e27e 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -18,32 +18,47 @@ */ import { UserSettingOptions } from '../enums'; +import { BankOffice } from '../task_2'; +import { IBankUser } from '../types'; export class UserSettingsModule { - private _bankOffice: any; - private _user: any; + private _bankOffice: BankOffice; + private _user: any; - public set user(user: any) { - this._user = user; - } + public set user(user: IBankUser) { + this._user = user; + } - constructor(initialBankOffice: any) { - this._bankOffice = initialBankOffice; - } + 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: string): boolean { + if (this._user) { + this._user.surname = newSurname; + return true; + } + return false; + } - private changeUserSurname(newSurname: any): any { - - } - - private registerForUserNewCard(newCardId: any): any { - - } - - public changeUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any { - - } + private registerForUserNewCard(newCardId: string): any { + if (!this._bankOffice.isCardTiedToUser(newCardId) && this._bankOffice.getCardById(newCardId)) { + this._user.cards.push(this._bankOffice.getCardById(newCardId)); + return true; + } + return false; + } + public changeUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean { + if (option === 0) return this.changeUserName(argsForChangeFunction); + else if (option === 1) return this.changeUserSurname(argsForChangeFunction); + else if (option === 2) 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..6d3b3b3 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -16,15 +16,29 @@ */ import { Currency } from '../enums'; +import { MoneyRepository } from '../task_1'; +import { IMoneyUnit } from '../types'; export class CurrencyConverterModule { - private _moneyRepository: any; + private _moneyRepository: MoneyRepository; + public oneUSD: number; + constructor(initialMoneyRepository: MoneyRepository) { + this._moneyRepository = initialMoneyRepository; + this.oneUSD = 70; + } - constructor(initialMoneyRepository: any) { - this._moneyRepository = initialMoneyRepository; - } - - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: any): any { - - } + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit[]): boolean { + // let currency = (fromCurrency === 0) ? "₽" : "$"; + let amount = 0; + let checkMoneyUnitCurrency = moneyUnits.every(item => item.moneyInfo.currency === fromCurrency); + // console.log("Валюта соответствует fromCurrency:", checkMoneyUnitCurrency); + moneyUnits.forEach(el => amount += Number(el.count) * Number(el.moneyInfo.denomination)); + if (amount % this.oneUSD === 0 && toCurrency === 1 && checkMoneyUnitCurrency) { + // console.log(`Сумма, которую получили: ${amount}${currency}`); + return this._moneyRepository.giveOutMoney(amount / this.oneUSD, toCurrency); + } else if (toCurrency === 0 && checkMoneyUnitCurrency) { + return this._moneyRepository.giveOutMoney(amount * this.oneUSD, toCurrency); + } + return false; + } } \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index 3c99ee2..deb2c56 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -17,41 +17,66 @@ import { Currency, UserSettingOptions } from '../enums'; import { MoneyRepository } from '../task_1'; -import { BankOffice, IBankUser } from '../task_2'; +import { BankOffice } from '../task_2'; import { UserSettingsModule } from '../task_3'; import { CurrencyConverterModule } from '../task_4'; +import { IBankUser, ICard, IMoneyUnit } from '../types'; class BankTerminal { - private _bankOffice: BankOffice; - private _moneyRepository: MoneyRepository; - private _userSettingsModule: UserSettingsModule; - private _currencyConverterModule: CurrencyConverterModule; - private _authorizedUser: IBankUser; + private _bankOffice: BankOffice; + private _moneyRepository: MoneyRepository; + private _userSettingsModule: UserSettingsModule; + private _currencyConverterModule: CurrencyConverterModule; + private _authorizedUser: IBankUser; - constructor(initBankOffice: any, initMoneyRepository: any) { - this._moneyRepository = initMoneyRepository; - this._bankOffice = initBankOffice; - this._userSettingsModule = new UserSettingsModule(initBankOffice); - this._currencyConverterModule = new CurrencyConverterModule(initMoneyRepository); - } + 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): void { + if (this._bankOffice.authorize(user.id, card.id, cardPin)) { + this._authorizedUser = user; + } + } - } + public takeUsersMoney(moneyUnits: IMoneyUnit[]): void { + if (this._authorizedUser) { + this._moneyRepository.takeMoney(moneyUnits); + this._authorizedUser.cards.forEach(item => { + moneyUnits.forEach(el => { + if (item.currency === el.moneyInfo.currency) { + this._bankOffice.getCardById(item.id).balance += el.count * Number(el.moneyInfo.denomination); + } + }) + }) + } + } - public takeUsersMoney(moneyUnits: any): any { + public giveOutUsersMoney(count: number, currency: number): void { + if (this._authorizedUser) { + if (this._moneyRepository.giveOutMoney(10000, 0)) { + this._authorizedUser.cards.forEach(item => { + if (item.currency === currency) { + item.balance -= count; + } + }) + } + } + } - } + public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean { + if (this._authorizedUser) { + this._userSettingsModule.user = this._authorizedUser; + return this._userSettingsModule.changeUserSettings(option, argsForChangeFunction); + } + } - public giveOutUsersMoney(count: any): any { - - } - - public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any { - - } - - public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: any): any { - - } + public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit[]): boolean { + if (this._authorizedUser) { + return this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits); + } + } } \ No newline at end of file diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..debb640 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,31 @@ +import { Currency } from "./enums"; + +interface IMoneyInfo { + denomination: string; + currency: Currency; +} + +export type TCash = { + count: number; + denomination: string; + currency: number; +} + +export interface IMoneyUnit { + moneyInfo: IMoneyInfo; + count: number; +} + +export interface ICard { + id: string; + balance: number; + currency: Currency, + pin: string, +} + +export interface IBankUser { + id: string; + name: string; + surname: string; + cards: Array; +} \ No newline at end of file