Advertisement
Btwonu

Bank

Oct 21st, 2020
302
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.   updateTransactions(currentCustomer, operation, amount) {
  8.     if (!currentCustomer.transactions) {
  9.       currentCustomer.transactions = [
  10.         {
  11.           n: 1,
  12.           operation,
  13.           amount,
  14.         },
  15.       ];
  16.     } else {
  17.       // store the transaction information to this customer
  18.       // update the transaction ID
  19.       let lastTransactionId =
  20.         currentCustomer.transactions[currentCustomer.transactions.length - 1].n;
  21.  
  22.       currentCustomer.transactions.push({
  23.         n: ++lastTransactionId,
  24.         operation,
  25.         amount,
  26.       });
  27.     }
  28.   }
  29.  
  30.   idFound(id) {
  31.     return this.allCustomers.find(
  32.       (customerObj) => customerObj.personalId === id
  33.     );
  34.   }
  35.  
  36.   newCustomer(customer) {
  37.     // console.log('customer: ', customer); // Delete
  38.     const { firstName, lastName, personalId } = customer;
  39.  
  40.     // check if the customer is already registered
  41.  
  42.     let errorMessage = `${firstName} ${lastName} is already our customer!`;
  43.     if (this.idFound(personalId)) {
  44.       throw new Error(errorMessage);
  45.     }
  46.  
  47.     // otherwise, add the customer and return customer details
  48.     // create customer
  49.     this.allCustomers.push(customer);
  50.     return customer;
  51.   }
  52.  
  53.   depositMoney(personalId, amount) {
  54.     (personalId = Number(personalId)), (amount = Number(amount));
  55.     // console.log('ID: ', personalId, 'amount: ', amount); // Delete
  56.  
  57.     // check if personalId corresponds to an id in this.allCustomers
  58.     // error => 'We have no customer with this ID!'
  59.     let errorMessage = 'We have no customer with this ID!';
  60.     let currentCustomer = this.idFound(personalId);
  61.     if (!currentCustomer) {
  62.       throw new Error(errorMessage);
  63.     }
  64.  
  65.     // if there is no balance account, create one
  66.     if (!currentCustomer.totalMoney) {
  67.       currentCustomer.totalMoney = 0;
  68.     }
  69.  
  70.     // add the amount to the corresponding customer's totalMoney
  71.     currentCustomer.totalMoney += amount;
  72.  
  73.     // update transaction history
  74.     this.updateTransactions(currentCustomer, 'deposit', amount);
  75.  
  76.     // return the total money of the customer
  77.     return `${currentCustomer.totalMoney}$`;
  78.   }
  79.  
  80.   withdrawMoney(personalId, amount) {
  81.     (personalId = Number(personalId)), (amount = Number(amount));
  82.     // console.log('ID: ', personalId, 'amount: ', amount); // Delete
  83.  
  84.     // check if personalId corresponds to an id in this.allCustomers
  85.     // error => 'We have no customer with this ID!'
  86.     let errorMessage = 'We have no customer with this ID!';
  87.     let currentCustomer = this.idFound(personalId);
  88.     if (!currentCustomer) {
  89.       throw new Error(errorMessage);
  90.     }
  91.  
  92.     // if not enough money in account to withdraw
  93.     errorMessage = `${currentCustomer.firstName} ${currentCustomer.lastName} does not have enough money to withdraw that amount!`;
  94.     if (currentCustomer.totalMoney < amount) {
  95.       throw new Error(errorMessage);
  96.     }
  97.  
  98.     // if no error subtract amount from totalMoney
  99.     currentCustomer.totalMoney -= amount;
  100.     // update transaction history
  101.     this.updateTransactions(currentCustomer, 'withdraw', amount);
  102.  
  103.     // return the total money of the customer
  104.     return `${currentCustomer.totalMoney}$`;
  105.   }
  106.  
  107.   customerInfo(personalId) {
  108.     const output = [];
  109.     personalId = Number(personalId);
  110.     // console.log('ID: ', personalId); // Delete
  111.  
  112.     // check if personalId corresponds to an id in this.allCustomers
  113.     // error => 'We have no customer with this ID!'
  114.     let currentCustomer = this.idFound(personalId);
  115.     if (!currentCustomer) {
  116.       throw new Error(errorMessage);
  117.     }
  118.  
  119.     // if no error return all the customer info
  120.     output.push(`Bank name: ${this._bankName}`);
  121.  
  122.     output.push(
  123.       `Customer name: ${currentCustomer.firstName} ${currentCustomer.lastName}`
  124.     );
  125.     output.push(`Customer ID: ${currentCustomer.personalId}`);
  126.     output.push(`Total Money: ${currentCustomer.totalMoney}$`);
  127.  
  128.     // for each transaction reversed
  129.     output.push('Transactions:');
  130.     currentCustomer.transactions.reverse().forEach((transaction) => {
  131.       console.log(transaction);
  132.       if (transaction.operation == 'withdraw') {
  133.         output.push(
  134.           `${transaction.n}. ${currentCustomer.firstName} ${currentCustomer.lastName} withdrew ${transaction.amount}$!`
  135.         );
  136.       } else {
  137.         output.push(
  138.           `${transaction.n}. ${currentCustomer.firstName} ${currentCustomer.lastName} made deposit of ${transaction.amount}$!`
  139.         );
  140.       }
  141.     });
  142.  
  143.     return output.join('\n');
  144.   }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement