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
311 changes: 298 additions & 13 deletions src/task_1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCash> = [];
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
}
}

]

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: [

]
},
]
64 changes: 31 additions & 33 deletions src/task_2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICard>;
}
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<IBankUser>;
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));
}
}
Loading