Advertisement
divanov94

Untitled

Oct 16th, 2020
78
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.find(c => c.firstName === customer.firstName && c.lastName === customer.lastName) !== undefined) {
  9.             throw new Error(`${customer.firstName} ${customer.lastName} is already our customer!`);
  10.         } else {
  11.             customer.totalMoney = 0;
  12.             customer.transactions=[];
  13.             this.allCustomers.push(customer);
  14.             return customer;
  15.  
  16.         }
  17.  
  18.     }
  19.  
  20.     depositMoney(personalId, amount) {
  21.         let customer = this.allCustomers.find(c => c.personalId === personalId);
  22.         if (customer === undefined) {
  23.             throw new Error('We have no customer with this ID!');
  24.         } else {
  25.             customer.totalMoney += amount;
  26.             customer.transactions.unshift(`${customer.transactions.length+1}.${customer.firstName} ${customer.lastName} made deposit of ${amount}$! `)
  27.             return `${customer.totalMoney}$`;
  28.  
  29.         }
  30.  
  31.  
  32.     }
  33.  
  34.     withdrawMoney(personalId, amount) {
  35.         let customer = this.allCustomers.find(c => c.personalId === personalId);
  36.         if (customer === undefined) {
  37.             throw new Error('We have no customer with this ID!');
  38.         } else if (customer.totalMoney < amount) {
  39.             throw new Error(`${customer.firstName} ${customer.lastName} does not have enough money to withdraw that amount!`);
  40.         } else {
  41.             customer.totalMoney -= amount;
  42.             customer.transactions.unshift(`${customer.transactions.length+1}.${customer.firstName} ${customer.lastName} withdrew ${amount}$! `)
  43.             return `${customer.totalMoney}$`;
  44.         }
  45.     }
  46.  
  47.     customerInfo(personalId) {
  48.         let customer = this.allCustomers.find(c => c.personalId === personalId);
  49.         if (customer === undefined) {
  50.             throw new Error('We have no customer with this ID!');
  51.         } else {
  52.             return [
  53.                 `Bank name: ${this._bankName}`,
  54.                 `Customer name: ${customer.firstName} ${customer.lastName}`,
  55.                 `Customer ID: ${personalId}`,
  56.                 `Total Money: ${customer.totalMoney}$`,
  57.                 'Transactions: '
  58.             ].concat(customer.transactions).join('\n');
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement