Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bank {
- constructor(bankName) {
- this._bankName = bankName;
- this.allCustomers = [];
- }
- newCustomer(customer) {
- if (this.allCustomers.find(c => c.firstName === customer.firstName && c.lastName === customer.lastName) !== undefined) {
- throw new Error(`${customer.firstName} ${customer.lastName} is already our customer!`);
- } else {
- customer.totalMoney = 0;
- customer.transactions=[];
- this.allCustomers.push(customer);
- return customer;
- }
- }
- depositMoney(personalId, amount) {
- let customer = this.allCustomers.find(c => c.personalId === personalId);
- if (customer === undefined) {
- throw new Error('We have no customer with this ID!');
- } else {
- customer.totalMoney += amount;
- customer.transactions.unshift(`${customer.transactions.length+1}.${customer.firstName} ${customer.lastName} made deposit of ${amount}$! `)
- return `${customer.totalMoney}$`;
- }
- }
- withdrawMoney(personalId, amount) {
- let customer = this.allCustomers.find(c => c.personalId === personalId);
- if (customer === undefined) {
- throw new Error('We have no customer with this ID!');
- } else if (customer.totalMoney < amount) {
- throw new Error(`${customer.firstName} ${customer.lastName} does not have enough money to withdraw that amount!`);
- } else {
- customer.totalMoney -= amount;
- customer.transactions.unshift(`${customer.transactions.length+1}.${customer.firstName} ${customer.lastName} withdrew ${amount}$! `)
- return `${customer.totalMoney}$`;
- }
- }
- customerInfo(personalId) {
- let customer = this.allCustomers.find(c => c.personalId === personalId);
- if (customer === undefined) {
- throw new Error('We have no customer with this ID!');
- } else {
- return [
- `Bank name: ${this._bankName}`,
- `Customer name: ${customer.firstName} ${customer.lastName}`,
- `Customer ID: ${personalId}`,
- `Total Money: ${customer.totalMoney}$`,
- 'Transactions: '
- ].concat(customer.transactions).join('\n');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement