Advertisement
HariNikolov

Untitled

Apr 4th, 2020
234
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.     let pFirstName = this.allCustomers.find(
  9.       x => x.firstName === customer.firstName
  10.     );
  11.     let pLastName = this.allCustomers.find(
  12.       x => x.lastName === customer.lastName
  13.     );
  14.     if (pFirstName && pLastName) {
  15.       throw new Error(
  16.         `${customer.firstName} ${customer.lastName} is already our customer!`
  17.       );
  18.     } else {
  19.       this.allCustomers.push(customer);
  20.     }
  21.     return customer;
  22.   }
  23.  
  24.   depositMoney(personalId, amount) {
  25.     let checkId = this.allCustomers.find(x => x.personalId === personalId);
  26.     if (!checkId.hasOwnProperty("totalMoney")) {
  27.       checkId.totalMoney = 0;
  28.     }
  29.     if (!checkId.hasOwnProperty("transaction")) {
  30.       checkId.transaction = [];
  31.     }
  32.     if (!checkId) {
  33.       throw new Error(`We have no customer with this ID!`);
  34.     } else {
  35.       checkId.totalMoney = checkId.totalMoney + amount;
  36.       let currTransaction = `${checkId.firstName} ${checkId.lastName} made deposit of ${amount}$`;
  37.       checkId.transaction.push(currTransaction);
  38.     }
  39.     return `${checkId.totalMoney}$`;
  40.   }
  41.  
  42.   withdrawMoney(personalId, amount) {
  43.     let checkId = this.allCustomers.find(x => x.personalId === personalId);
  44.     if (!checkId.hasOwnProperty("totalMoney")) {
  45.       checkId.totalMoney = 0;
  46.     }
  47.     if (!checkId.hasOwnProperty("transaction")) {
  48.       checkId.transaction = [];
  49.     }
  50.     if (!checkId) {
  51.       throw new Error(`We have no customer with this ID!`);
  52.     }
  53.     if (checkId.totalMoney < amount) {
  54.       throw new Error(
  55.         `${checkId.firstName} ${checkId.lastName} does not have enough money to withdraw that amount!`
  56.       );
  57.     } else {
  58.       checkId.totalMoney = checkId.totalMoney - amount;
  59.       let currTransaction = `${checkId.firstName} ${checkId.lastName} withdrew ${amount}$`;
  60.       checkId.transaction.push(currTransaction);
  61.     }
  62.     return `${checkId.totalMoney}$`;
  63.   }
  64.   customerInfo(personalId) {
  65.     let checkId = this.allCustomers.find(x => x.personalId === personalId);
  66.     let result = checkId.transaction;
  67.     if (!checkId) {
  68.       throw new Error(`We have no customer with this ID!`);
  69.     } else {
  70.       return `
  71.         Bank name: ${this._bankName}
  72.         Customer name: ${checkId.firstName} ${checkId.lastName}
  73.         Customer ID: ${checkId.personalId}
  74.         Total Money: ${checkId.totalMoney}$
  75.         Transactions: \n${result
  76.           .map((e, i) => (result = `${i + 1}. ${e}!`))
  77.           .reverse()
  78.           .join("\n")}
  79.         `;
  80.     }
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement