Advertisement
Guest User

Untitled

a guest
Oct 11th, 2020
547
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.         this.transactions = [];
  6.     }
  7.  
  8.     newCustomer(customer) {
  9.         const findCustomer = this.allCustomers.find((x) => {
  10.             return x.firstName === customer.firstName || x.lastName === customer.lastName
  11.         });
  12.  
  13.         if (findCustomer) {
  14.             throw Error(`${findCustomer.firstName} ${findCustomer.lastName} is already our customer!`)
  15.         } else {
  16.             this.allCustomers.push(customer);
  17.             return customer;
  18.         }
  19.     }
  20.  
  21.     depositMoney(personalId, amount) {
  22.         const findCustomer = this.allCustomers.find((x) => { return x.personalId === personalId });
  23.  
  24.         if (!findCustomer) {
  25.             throw Error('We have no customer with this ID!')
  26.         }
  27.         if (isNaN(findCustomer['totalMoney'])) {
  28.             findCustomer['totalMoney'] = amount;
  29.         } else {
  30.             findCustomer['totalMoney'] += amount;
  31.         }
  32.         this.transactions.push(`${findCustomer.firstName} ${findCustomer.lastName} made deposit of ${amount}$!`)
  33.         return `${findCustomer['totalMoney']}$`
  34.     }
  35.  
  36.     withdrawMoney(personalId, amount) {
  37.         const findCustomer = this.allCustomers.find((x) => { return x.personalId === personalId });
  38.  
  39.         if (!findCustomer) {
  40.             throw Error('We have no customer with this ID!');
  41.         }
  42.  
  43.         if (findCustomer.totalMoney <= 0) {
  44.             throw Error(`${findCustomer.firstName} ${findCustomer.lastName} does not have enough money to withdraw that amount!`)
  45.         }
  46.  
  47.         findCustomer.totalMoney -= amount;
  48.         this.transactions.push(`${findCustomer.firstName} ${findCustomer.lastName} withdrew ${amount}$!`);
  49.         return `${findCustomer.totalMoney}$`
  50.     }
  51.  
  52.     customerInfo(personalId) {
  53.         const info = this.allCustomers.find((x) => { return x.personalId === personalId });
  54.         let transaction = this.transactions.filter((x) => { return x.includes(info.firstName) });
  55.         let reversed = transaction.reverse();
  56.  
  57.         let result = `Bank name: ${this._bankName}\nCustomer name: ${info.firstName} ${info.lastName}\nCustomer ID: ${info.personalId}\nTotal Money: ${info.totalMoney}$\nTransactions:`;
  58.         let counter = reversed.length;
  59.  
  60.         reversed.forEach(x => result += '\n' + `${counter--}. ` + x);
  61.  
  62.         return result.trim();
  63.     }
  64. }
  65. let bank = new Bank('Softuni Bank');
  66.  
  67. bank.newCustomer({ firstName: 'Svetlin', lastName: 'Nakov', personalId: 1111111 });
  68.  
  69.  
  70. bank.newCustomer({ firstName: 'Mihaela', lastName: 'Mileva', personalId: 3333333 });
  71.  
  72. bank.depositMoney(1111111, 250);
  73.  
  74. bank.depositMoney(1111111, 250);
  75.  
  76. bank.depositMoney(3333333, 555);
  77.  
  78. bank.withdrawMoney(1111111, 125);
  79.  
  80. console.log(bank.customerInfo(1111111));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement