SHOW:
|
|
- or go back to the newest paste.
| 1 | class Bank {
| |
| 2 | constructor(bankName) {
| |
| 3 | this._bankName = bankName; | |
| 4 | this.allCustomers = []; | |
| 5 | } | |
| 6 | ||
| 7 | newCustomer({ firstName, lastName, personalId }) {
| |
| 8 | let hasCustomer = this.allCustomers.find( | |
| 9 | (customer) => customer.personalId === personalId | |
| 10 | ); | |
| 11 | ||
| 12 | if (hasCustomer) {
| |
| 13 | throw new Error(`${firstName} ${lastName} is already our customer!`);
| |
| 14 | } | |
| 15 | ||
| 16 | let customer = { firstName, lastName, personalId };
| |
| 17 | this.allCustomers.push(customer); | |
| 18 | ||
| 19 | return customer; | |
| 20 | } | |
| 21 | ||
| 22 | depositMoney(personalId, amount) {
| |
| 23 | let currentCustomer = this.getCustomerById(personalId); | |
| 24 | ||
| 25 | if (currentCustomer.totalMoney) {
| |
| 26 | currentCustomer.totalMoney += amount; | |
| 27 | } else {
| |
| 28 | currentCustomer.totalMoney = amount; | |
| 29 | currentCustomer.transactions = []; | |
| 30 | } | |
| 31 | ||
| 32 | currentCustomer.transactions.push( | |
| 33 | `${currentCustomer.firstName} ${currentCustomer.lastName} made deposit of ${amount}$!`
| |
| 34 | ); | |
| 35 | ||
| 36 | return `${currentCustomer.totalMoney}$`;
| |
| 37 | } | |
| 38 | ||
| 39 | withdrawMoney(personalId, amount) {
| |
| 40 | let currentCustomer = this.getCustomerById(personalId); | |
| 41 | ||
| 42 | if (currentCustomer.totalMoney < amount) {
| |
| 43 | throw new Error( | |
| 44 | `${currentCustomer.firstName} ${currentCustomer.lastName} does not have enough money to withdraw that amount!`
| |
| 45 | ); | |
| 46 | } else {
| |
| 47 | currentCustomer.totalMoney -= amount; | |
| 48 | currentCustomer.transactions.push( | |
| 49 | `${currentCustomer.firstName} ${currentCustomer.lastName} withdrew ${amount}$!`
| |
| 50 | ); | |
| 51 | } | |
| 52 | ||
| 53 | return `${currentCustomer.totalMoney}$`;
| |
| 54 | } | |
| 55 | ||
| 56 | customerInfo(personalId) {
| |
| 57 | let currentCustomer = this.getCustomerById(personalId); | |
| 58 | ||
| 59 | let output = `Bank name: ${this._bankName}\nCustomer name: ${currentCustomer.firstName} ${currentCustomer.lastName}\nCustomer ID: ${personalId}\nTotal Money: ${currentCustomer.totalMoney}$\n`;
| |
| 60 | ||
| 61 | if (currentCustomer.transactions.length > 0) {
| |
| 62 | output += 'Transactions:\n'; | |
| 63 | for(let i = currentCustomer.transactions.length; i > 0; i-- ) {
| |
| 64 | output += `${i}. ${currentCustomer.transactions[i - 1]}\n`;
| |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | return output.trim(); | |
| 69 | } | |
| 70 | ||
| 71 | getCustomerById(personalId) {
| |
| 72 | let currentCustomer = this.allCustomers.find( | |
| 73 | (customer) => customer.personalId === personalId | |
| 74 | ); | |
| 75 | ||
| 76 | if (!currentCustomer) {
| |
| 77 | throw new Error(`We have no customer with this ID!`); | |
| 78 | } | |
| 79 | ||
| 80 | return currentCustomer; | |
| 81 | } | |
| 82 | } |