Advertisement
dilyana2001

Untitled

Oct 20th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Bank {
  2.     constructor(bankName) {
  3.         this._bankName = bankName;
  4.         this.allCustomers = [];
  5.     }
  6.  
  7.     newCustomer(customer) {
  8.         if (this.allCustomers.some(x => x.firstName == customer.firstName) && this.allCustomers.some(x => x.lastName == customer.lastName)) {
  9.             throw new Error(`${customer.firstName} ${customer.lastName} is already our customer!`);
  10.         } else {
  11.             this.allCustomers.push(customer);
  12.             return customer;
  13.         }
  14.     }
  15.  
  16.     depositMoney(personalId, amount) {
  17.         personalId = Number(personalId);
  18.         amount = Number(amount);
  19.         if (!this.allCustomers.some(x => x.personalId == personalId)) {
  20.             throw new Error(` We have no customer with this ID!`);
  21.         } else {
  22.             const person = this.allCustomers.find(x => x.personalId == personalId);
  23.             if (!person.totalMoney) {
  24.                 person.totalMoney = 0;
  25.                 person.transactions = [];
  26.  
  27.             }
  28.             let deposit = true;
  29.             person.transactions.push({ number: person.transactions.length + 1, names: { firstName: person.firstName, lastName: person.lastName }, deposit, amount });
  30.             person.totalMoney += amount;
  31.             return `${person.totalMoney}$`;
  32.         }
  33.     }
  34.  
  35.     withdrawMoney(personalId, amount) {
  36.         personalId = Number(personalId);
  37.         amount = Number(amount);
  38.         if (!this.allCustomers.some(x => x.personalId == personalId)) {
  39.             throw new Error(` We have no customer with this ID!`);
  40.         } else {
  41.             const person = this.allCustomers.find(x => x.personalId == personalId);
  42.             if (person.totalMoney >= amount) {
  43.                 person.totalMoney -= amount;
  44.                 let withdraw = true;
  45.                 person.transactions.push({ number: person.transactions.length + 1, names: { firstName: person.firstName, lastName: person.lastName }, withdraw, amount });
  46.                 return `${person.totalMoney}$`;
  47.             } else {
  48.                 throw new Error(`${firstName} ${lastName} does not have enough money to withdraw that amount!`);
  49.             }
  50.         }
  51.     }
  52.  
  53.     customerInfo(personalId) {
  54.         personalId = Number(personalId);
  55.         if (!this.allCustomers.some(x => x.personalId == personalId)) {
  56.             throw new Error(` We have no customer with this ID!`);
  57.         } else {
  58.             const person = this.allCustomers.find(x => x.personalId == personalId);
  59.             let result = [];
  60.             result.push(`Bank name: ${this._bankName}`);
  61.             result.push(`Customer name: ${person.firstName} ${person.lastName}`);
  62.             result.push(`Customer ID: ${person.personalId}`);
  63.             result.push(`Total Money: ${person.totalMoney}$`);
  64.             result.push(`Transactions:`);
  65.  
  66.             const sortedTransactions = person.transactions.sort((a, b) => b.number - a.number);
  67.  
  68.             for (const x of sortedTransactions) {
  69.                 if (x.deposit) {
  70.                     result.push(`${x.number}. ${x.names.firstName} ${x.names.lastName} made deposit of ${x.amount}$!`);
  71.                 } else if (x.withdraw) {
  72.                     result.push(`${x.number}. ${x.names.firstName} ${x.names.lastName} withdrew ${x.amount}$!`);
  73.                 }
  74.             }
  75.             return result.join('\n');
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement