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
69 changes: 65 additions & 4 deletions src/task_1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,77 @@ export interface IMoneyUnit {
}

export class MoneyRepository {
private _repository: any;
constructor(initialRepository: any) {
private _repository: Array<IMoneyUnit>;
constructor(initialRepository: Array<IMoneyUnit>) {
this._repository = initialRepository;
}

public giveOutMoney(count: any, currency: any): any {
public giveOutMoney(count: number, currency: Currency): boolean {
let givenMoney: Array<IMoneyUnit> = new Array<IMoneyUnit>();
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<IMoneyUnit>, 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;
}
});
}

public takeMoney(moneyUnits: any): any {
public convertMoney(count: number, currency: Currency, moneyUnits: Array<IMoneyUnit>):Array<IMoneyUnit>{
let res: Array<IMoneyUnit> = new Array<IMoneyUnit>();
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: Array<IMoneyUnit>): 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);
});
}

Choose a reason for hiding this comment

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

0 балов

Copy link
Author

Choose a reason for hiding this comment

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

Посмотрите, я исправил решение

Choose a reason for hiding this comment

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

Метод теперь работает верно 2 бала

}
24 changes: 14 additions & 10 deletions src/task_2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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

interface ICard {
export interface ICard {
id: string;
balance: number;
currency: Currency,
Expand All @@ -32,23 +32,27 @@ export interface IBankUser {
}

export class BankOffice {

Choose a reason for hiding this comment

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

Файл не валидный.

private _users: any;
private _cards: any;
private _users: Array<IBankUser>;
private _cards: Array<ICard>;

constructor(users: any, cards: any) {
constructor(users: Array<IBankUser>, cards: Array<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 {
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: any): any {

public getCardById(cardId: string): ICard | undefined {
return this._cards.find(x=> x.id === cardId);
}

public isCardTiedToUser(cardId: any): any {

public isCardTiedToUser(cardId: string): boolean {
return this._users.filter(x=> x.cards.find(y=> y.id === cardId)).length > 0;
}

Choose a reason for hiding this comment

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

2 бала

}
47 changes: 34 additions & 13 deletions src/task_3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,54 @@
* пользуясь уже предоставленными интерфейсами (избавиться от всех any типов)
*/

import { UserSettingOptions } from '../enums';
import { IBankUser, BankOffice } 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 (newName) {
this._user.name = newName;
return true;
} else{
return false;
}
}

private changeUserSurname(newSurname: any): any {

private changeUserSurname(newSurname: string): boolean {
if (newSurname) {
this._user.name = newSurname;
return true;
} else
return false;
}

private registerForUserNewCard(newCardId: any): any {

private registerForUserNewCard(newCardId: string): boolean {
let card = this._bankOffice.getCardById(newCardId);
if (card && !this._bankOffice.isCardTiedToUser(newCardId))
{
this._user.cards.push(card);
return true;
}
else
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 if (option === UserSettingOptions.newCard)
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 бала

21 changes: 17 additions & 4 deletions src/task_4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,28 @@
*/

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[] {

Choose a reason for hiding this comment

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

Этот метод не может возвращать массив купюр к выдаче. Потому что купюра не может быть дробной. Этот метод должен возвращать сумму, которая передаётся в _moneyRepository

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;
}
}
43 changes: 30 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,48 @@ 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): any {

public authorizeUser(user: IBankUser, card: ICard, cardPin: string) {
if (this._bankOffice.authorize(user.id, card.id, cardPin))
{
this._authorizedUser = user;
this._card = card;
}
}

public takeUsersMoney(moneyUnits: any): any {

public takeUsersMoney(moneyUnits: Array<IMoneyUnit>) {
if (this._authorizedUser)
{
this._moneyRepository.takeMoney(moneyUnits);
}
}

public giveOutUsersMoney(count: any): any {

public giveOutUsersMoney(count: number) {
if (this._authorizedUser)
{
this._moneyRepository.giveOutMoney(count, this._card.currency);

Choose a reason for hiding this comment

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

Добавь уменьшение денег на авторизованной карте. Изменение баланса карты

}
}

public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any {

public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string) {
if (this._authorizedUser)
{
this._userSettingsModule.changeUserSettings(option, argsForChangeFunction);
}
}

public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: any): any {

public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: Array<IMoneyUnit>) {
if (this._authorizedUser)
{
this._currencyConverterModule.convertMoneyUnits(fromCurrency, toCurrency, moneyUnits);
}
}

Choose a reason for hiding this comment

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

0 балов

}