Advertisement
kstoyanov

03. Bank Exam May 2020

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