Guest User

Bank

a guest
Jun 14th, 2020
540
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.         this.allCustomers.find((c) => {
  9.             if (c.personalId === customer.personalId) {
  10.                 throw new Error(
  11.                     `${customer.firstName} ${customer.lastName} is already our customer!`
  12.                 );
  13.             }
  14.         });
  15.         //customer.transactionInfos = [];
  16.         //customer.totalMoney = 0;
  17.         this.allCustomers.push(customer);
  18.         return customer;
  19.     }
  20.  
  21.     depositMoney(personalId, amount) {
  22.         let person = this.allCustomers.find((c) => {
  23.             return c.personalId === personalId;
  24.         });
  25.         if (person === undefined) {
  26.             throw new Error('We have no customer with this ID!');
  27.         } else {
  28.             if (isNaN(person.totalMoney)) {
  29.                 person.totalMoney = amount;
  30.             } else {
  31.                 person.totalMoney += amount;
  32.             }
  33.             if (!Array.isArray(person.transactionInfos)) {
  34.                 person.transactionInfos = [];
  35.             }
  36.             let message = `${person.transactionInfos.length + 1}. ${
  37.                 person.firstName
  38.             } ${person.lastName} made deposit of ${amount}$!`;
  39.             person.transactionInfos.push(message);
  40.             return `${person.totalMoney}$`;
  41.         }
  42.     }
  43.  
  44.     withdrawMoney(personalId, amount) {
  45.         let person = this.allCustomers.find((c) => {
  46.             return c.personalId === personalId;
  47.         });
  48.         if (person === undefined) {
  49.             throw new Error('We have no customer with this ID!');
  50.         } else {
  51.             if (isNaN(person.totalMoney)) {
  52.                 person.totalMoney = amount;
  53.             }
  54.             if (!Array.isArray(person.transactionInfos)) {
  55.                 person.transactionInfos = [];
  56.             }
  57.             if (person.totalMoney < amount) {
  58.                 throw new Error(`${person.firstName} ${person.lastName} does not have enough money to withdraw that amount!`);
  59.             } else {
  60.                 person.totalMoney -= amount;
  61.  
  62.                 let message = `${person.transactionInfos.length + 1}. ${
  63.                     person.firstName
  64.                 } ${person.lastName} withdrew ${amount}$!`;
  65.  
  66.                 person.transactionInfos.push(message);
  67.  
  68.                 return `${person.totalMoney}$`;
  69.             }
  70.         }
  71.     }
  72.  
  73.     customerInfo(personalId) {
  74.         let person = this.allCustomers.find((c) => {
  75.             return c.personalId === personalId;
  76.         });
  77.         if (person === undefined) {
  78.             throw new Error('We have no customer with this ID!');
  79.         } else {
  80.             let infoString = '';
  81.             infoString += `Bank name: ${this._bankName}\n`;
  82.             infoString += `Customer name: ${person.firstName} ${person.lastName}\n`;
  83.             infoString += `Customer ID: ${person.personalId}\n`;
  84.             infoString += `Total Money: ${person.totalMoney}$\n`;
  85.             infoString += `Transactions:\n`;
  86.             for (let index = person.transactionInfos.length - 1; index >= 0; index--) {
  87.                 infoString += person.transactionInfos[index];
  88.                 if (index !== 0) {
  89.                     infoString += `\n`;
  90.                 }
  91.             }
  92.             return infoString;
  93.         }
  94.     }
  95. }
Add Comment
Please, Sign In to add comment