Advertisement
Liliana797979

bank - js advanced exam

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