Advertisement
viligen

bank

Jun 22nd, 2022
895
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.         //object {firstName, lastName, personalId}.
  9.         if (
  10.             this.allCustomers.find((c) => c.personalId == customer.personalId)
  11.         ) {
  12.             throw new Error(
  13.                 `${customer.firstName} ${customer.lastName} is already our customer!`
  14.             );
  15.         }
  16.         let currentData = Object.assign({}, customer);
  17.         customer.totalMoney = 0;
  18.         customer.transactions = []; //{type:amount}
  19.         this.allCustomers.push(customer);
  20.         return currentData;
  21.     }
  22.     depositMoney(personalId, amount) {
  23.         let currentCustomer = this._findCustomerById(personalId);
  24.         if (!currentCustomer)
  25.             throw new Error(`We have no customer with this ID!`);
  26.         currentCustomer.totalMoney += amount;
  27.         currentCustomer.transactions.push({ 'made deposit of': amount });
  28.         return `${currentCustomer.totalMoney}$`;
  29.     }
  30.     withdrawMoney(personalId, amount) {
  31.         let currentCustomer = this._findCustomerById(personalId);
  32.         if (!currentCustomer) {
  33.             throw new Error('We have no customer with this ID!');
  34.         }
  35.         if (currentCustomer.totalMoney < amount) {
  36.             throw new Error(
  37.                 `${currentCustomer.firstName} ${currentCustomer.lastName} does not have enough money to withdraw that amount!`
  38.             );
  39.         }
  40.         currentCustomer.totalMoney -= amount;
  41.         currentCustomer.transactions.push({ withdrew: amount });
  42.         return `${currentCustomer.totalMoney}$`;
  43.     }
  44.     customerInfo(personalId) {
  45.         let currentCustomer = this._findCustomerById(personalId);
  46.         if (!currentCustomer) {
  47.             throw new Error('We have no customer with this ID!');
  48.         }
  49.         let result = [
  50.             `Bank name: ${this._bankName}
  51. Customer name: ${currentCustomer.firstName} ${currentCustomer.lastName}
  52. Customer ID: ${personalId}
  53. Total Money: ${currentCustomer.totalMoney}$
  54. Transactions:`,
  55.         ];
  56.         for (let i = currentCustomer.transactions.length - 1; i >= 0; i--) {
  57.             let [type, amnt] = Object.entries(
  58.                 currentCustomer.transactions[i]
  59.             )[0];
  60.             result.push(
  61.                 `${i + 1}. ${currentCustomer.firstName} ${
  62.                     currentCustomer.lastName
  63.                 } ${type} ${amnt}$!`
  64.             );
  65.         }
  66.         return result.join('\n');
  67.     }
  68.     _findCustomerById(id) {
  69.         return this.allCustomers.find((c) => c.personalId == id);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement