Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bank {
- constructor(bankName) {
- this._bankName = bankName;
- this.allCustomers = [];
- }
- newCustomer(customer) {
- if (this.allCustomers.some(x => x.firstName == customer.firstName) && this.allCustomers.some(x => x.lastName == customer.lastName)) {
- throw new Error(`${customer.firstName} ${customer.lastName} is already our customer!`);
- } else {
- this.allCustomers.push(customer);
- return customer;
- }
- }
- depositMoney(personalId, amount) {
- personalId = Number(personalId);
- amount = Number(amount);
- if (!this.allCustomers.some(x => x.personalId == personalId)) {
- throw new Error(` We have no customer with this ID!`);
- } else {
- const person = this.allCustomers.find(x => x.personalId == personalId);
- if (!person.totalMoney) {
- person.totalMoney = 0;
- person.transactions = [];
- }
- let deposit = true;
- person.transactions.push({ number: person.transactions.length + 1, names: { firstName: person.firstName, lastName: person.lastName }, deposit, amount });
- person.totalMoney += amount;
- return `${person.totalMoney}$`;
- }
- }
- withdrawMoney(personalId, amount) {
- personalId = Number(personalId);
- amount = Number(amount);
- if (!this.allCustomers.some(x => x.personalId == personalId)) {
- throw new Error(` We have no customer with this ID!`);
- } else {
- const person = this.allCustomers.find(x => x.personalId == personalId);
- if (person.totalMoney >= amount) {
- person.totalMoney -= amount;
- let withdraw = true;
- person.transactions.push({ number: person.transactions.length + 1, names: { firstName: person.firstName, lastName: person.lastName }, withdraw, amount });
- return `${person.totalMoney}$`;
- } else {
- throw new Error(`${firstName} ${lastName} does not have enough money to withdraw that amount!`);
- }
- }
- }
- customerInfo(personalId) {
- personalId = Number(personalId);
- if (!this.allCustomers.some(x => x.personalId == personalId)) {
- throw new Error(` We have no customer with this ID!`);
- } else {
- const person = this.allCustomers.find(x => x.personalId == personalId);
- let result = [];
- result.push(`Bank name: ${this._bankName}`);
- result.push(`Customer name: ${person.firstName} ${person.lastName}`);
- result.push(`Customer ID: ${person.personalId}`);
- result.push(`Total Money: ${person.totalMoney}$`);
- result.push(`Transactions:`);
- const sortedTransactions = person.transactions.sort((a, b) => b.number - a.number);
- for (const x of sortedTransactions) {
- if (x.deposit) {
- result.push(`${x.number}. ${x.names.firstName} ${x.names.lastName} made deposit of ${x.amount}$!`);
- } else if (x.withdraw) {
- result.push(`${x.number}. ${x.names.firstName} ${x.names.lastName} withdrew ${x.amount}$!`);
- }
- }
- return result.join('\n');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement