Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions src/task_1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import { Currency } from '../enums';

interface IMoneyInfo {
interface IMoneyInfo {
denomination: string;
currency: Currency;
}
Expand All @@ -31,16 +31,72 @@ export interface IMoneyUnit {
}

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 {
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 takeMoney(moneyUnits: any): any {
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();
//Промежуточное хранилище для того, чтобы фиксировать денеж. ед, которые надо будет удалить, если удасться "жадно" набрать сумму из хранилища
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(this._createMoneyUnitFromUnit(val, unit));
if (count === 0) {
//Ниже следует метод для изъятия из хранилища купюр.
this._removeFromRepos(countDenominationsPair);
return result;
}
}
}
return null;
}
}

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);
}
})
}
}
33 changes: 23 additions & 10 deletions src/task_2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
for(const user of this._users) {
if (user.id === userId){
if (user.cards.find(card => card.id === cardId && card.pin === cardPin)){
return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этот метод не отработает, он сделает ретурн для функции .foreach ( callbaack() { return }) , а не для родительской. Попробуй проверить. Авторизация всегда будет false.

}
}
}
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему здесь не используешь this._users.forEach ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исходя из следующего Вашего комментария, не лучше ли будет в методах использовать просто "for"? Или хорошим тоном является все-таки использование "forEach"?

if (user.cards.find(card => card.id === cardId)) {
return true;
}
}
return false;
}
}
44 changes: 32 additions & 12 deletions src/task_3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Засчитано 3 бала

}
12 changes: 9 additions & 3 deletions src/task_4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
*/

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;
const count = moneyUnits.reduce((accumulator, unit) => {
return accumulator + unit.count * +unit.moneyInfo.denomination;
}, 0) * coeff;

return this._moneyRepository.giveOutMoney(count, toCurrency);
}
}
60 changes: 47 additions & 13 deletions src/task_5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Засчитано 3 бала

}