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

export class MoneyRepository {
private _repository: any;
private _repository: IMoneyUnit[];

constructor(initialRepository: any) {
constructor(initialRepository: IMoneyUnit[]) {
this._repository = initialRepository;
}

public giveOutMoney(count: any, currency: any): any {
public giveOutMoney(count: number, currency: Currency): boolean {
this._repository.sort((x,y) => parseInt(y.moneyInfo.denomination) - parseInt(x.moneyInfo.denomination));
for(const obj of this._repository){
const denomination: number = parseInt(obj.moneyInfo.denomination);
if(obj.moneyInfo.currency !== currency || denomination > count || obj.count === 0){
continue;
}else{
const ost: number = count/denomination;
if (ost > obj.count){
count -= obj.count * denomination;
obj.count = 0;
}else{
count -= ost*denomination;
obj.count -= ost;
}
}
}

return count === 0;
}

public takeMoney(moneyUnits: any): any {
public takeMoney(moneyUnits: IMoneyUnit[]): boolean {
for (let j = 0; j< moneyUnits.length; j++){
let canTake: boolean;
for (let i = 0; i < this._repository.length; i++) {

Choose a reason for hiding this comment

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

лучше всего использовать find

if (this._repository[i].moneyInfo.denomination === moneyUnits[j].moneyInfo.denomination
&& this._repository[i].moneyInfo.currency === moneyUnits[j].moneyInfo.currency) {
this._repository[i].count += moneyUnits[j].count;
canTake = true;
}
}
if (!canTake){
this._repository.push(moneyUnits[j])
}
}

return true;
}
}
30 changes: 21 additions & 9 deletions src/task_2/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-mixed-spaces-and-tabs */
/** Задача 1 - BankOffice
* Имеется класс BankOffice. Который должен хранить пользователей и банковские карты.
* Пользователи банка могу иметь карту, а могут не иметь.
Expand Down Expand Up @@ -32,23 +33,34 @@ export interface IBankUser {
}

export class BankOffice {
private _users: any;
private _cards: any;
private _users: IBankUser[];
private _cards: ICard[];

constructor(users: any, cards: any) {
this._users = users;
this._cards = cards;
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 {
const user: IBankUser = this._users.find(user => user.id === userId);
const card: ICard = user.cards.find(card => card.id === cardId);

return user.cards.indexOf(card) !== -1 && card.pin === cardPin;
}

public getCardById(cardId: any): any {
public getCardById(cardId: string): ICard {
const notCard: ICard = this._cards.find(card => card.id === cardId);
if (notCard === undefined) {
const userCard: IBankUser = this._users.find(user => user.cards.find(card => card.id === cardId));

}
return userCard === undefined ? undefined : userCard.cards.find(card => card.id === cardId);
}

public isCardTiedToUser(cardId: any): any {
return notCard;
}

public isCardTiedToUser(cardId: string): boolean {
return this._users.find(user => user.cards.find(
card => card.id === cardId)) !== undefined;
}

Choose a reason for hiding this comment

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

Зачёт. 2 бала

}
49 changes: 38 additions & 11 deletions src/task_3/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-mixed-spaces-and-tabs */
/** Задача 3 - UserSettingsModule
* Имеется класс UserSettingsModule. Который должен отвечать за
* изменение настроек пользователя.
Expand All @@ -18,32 +19,58 @@
*/

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) {
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 false;
}
this._user.name = newName;

return true;
}

private changeUserSurname(newSurname: any): any {
private changeUserSurname(newSurname: string): boolean {
if (!this._user || this._user.surname === newSurname) {
return false;
}
this._user.surname = newSurname;

return true;
}

private registerForUserNewCard(newCardId: any): any {
private registerForUserNewCard(newCardId: string): boolean {
if (!this._bankOffice.isCardTiedToUser(newCardId) && this._bankOffice.getCardById(newCardId)) {
this._user.cards.push(this._bankOffice.getCardById(newCardId))

}
return true
}

public changeUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any {
return false
}

public changeUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean {
switch(option) {
case UserSettingOptions.name:
return this.changeUserName(argsForChangeFunction);
case UserSettingOptions.surname:
return this.changeUserSurname(argsForChangeFunction);
case UserSettingOptions.newCard:
return this.registerForUserNewCard(argsForChangeFunction);
default:
return false;
}
}
}
19 changes: 14 additions & 5 deletions src/task_4/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-mixed-spaces-and-tabs */
/** Задача 4 - CurrencyConverterModule
* Имеется класс CurrencyConverterModule. Который должен отвечать за
* конвертацию валют.
Expand All @@ -16,15 +17,23 @@
*/

import { Currency } from '../enums';
import { IMoneyUnit, MoneyRepository } from '../task_1';

export class CurrencyConverterModule {
private _moneyRepository: any;
private _moneyRepository: MoneyRepository;

constructor(initialMoneyRepository: any) {
this._moneyRepository = initialMoneyRepository;
constructor(initialMoneyRepository: MoneyRepository) {
this._moneyRepository = initialMoneyRepository;
}

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

public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit): number {
switch (fromCurrency) {
case toCurrency:
return 0;
case Currency.USD:
return Number(moneyUnits.moneyInfo.denomination) * moneyUnits.count * 70;
case Currency.RUB:
return Number(moneyUnits.moneyInfo.denomination) * moneyUnits.count / 70;

Choose a reason for hiding this comment

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

Зачёт. 2 бала

}
}
}
40 changes: 24 additions & 16 deletions src/task_5/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-mixed-spaces-and-tabs */
/** Задача 5 - BankTerminal
* Имеется класс BankTerminal. Класс представляет банковский терминал.
* Требуется:
Expand All @@ -16,8 +17,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 @@ -28,30 +29,37 @@ export class BankTerminal {
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): boolean {
if (this._bankOffice.authorize(user.id, card.id, cardPin)) {
this._userSettingsModule.user = user;
this._authorizedUser = user;

}

public takeUsersMoney(moneyUnits: any): any {
return true;
}

return false;
}

public giveOutUsersMoney(count: any): any {

public takeUsersMoney(moneyUnits: IMoneyUnit[]): boolean {
return this._moneyRepository.takeMoney(moneyUnits);
}

public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: any): any {
public giveOutUsersMoney(count: number): boolean {
return this._moneyRepository.giveOutMoney(count, Currency.RUB);
}

public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: any): any {
public changeAuthorizedUserSettings(option: UserSettingOptions, argsForChangeFunction: string): boolean {
return this._userSettingsModule.changeUserSettings(option, argsForChangeFunction);
}

public convertMoneyUnits(fromCurrency: Currency, toCurrency: Currency, moneyUnits: IMoneyUnit): number {
return 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.

Зачёт. 2 бала

}
}