Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bank {
- constructor(bankName) {
- this._bankName = bankName;
- this.allCustomers = [];
- }
- updateTransactions(currentCustomer, operation, amount) {
- if (!currentCustomer.transactions) {
- currentCustomer.transactions = [
- {
- n: 1,
- operation,
- amount,
- },
- ];
- } else {
- // store the transaction information to this customer
- // update the transaction ID
- let lastTransactionId =
- currentCustomer.transactions[currentCustomer.transactions.length - 1].n;
- currentCustomer.transactions.push({
- n: ++lastTransactionId,
- operation,
- amount,
- });
- }
- }
- idFound(id) {
- return this.allCustomers.find(
- (customerObj) => customerObj.personalId === id
- );
- }
- newCustomer(customer) {
- // console.log('customer: ', customer); // Delete
- const { firstName, lastName, personalId } = customer;
- // check if the customer is already registered
- let errorMessage = `${firstName} ${lastName} is already our customer!`;
- if (this.idFound(personalId)) {
- throw new Error(errorMessage);
- }
- // otherwise, add the customer and return customer details
- // create customer
- this.allCustomers.push(customer);
- return customer;
- }
- depositMoney(personalId, amount) {
- (personalId = Number(personalId)), (amount = Number(amount));
- // console.log('ID: ', personalId, 'amount: ', amount); // Delete
- // check if personalId corresponds to an id in this.allCustomers
- // error => 'We have no customer with this ID!'
- let errorMessage = 'We have no customer with this ID!';
- let currentCustomer = this.idFound(personalId);
- if (!currentCustomer) {
- throw new Error(errorMessage);
- }
- // if there is no balance account, create one
- if (!currentCustomer.totalMoney) {
- currentCustomer.totalMoney = 0;
- }
- // add the amount to the corresponding customer's totalMoney
- currentCustomer.totalMoney += amount;
- // update transaction history
- this.updateTransactions(currentCustomer, 'deposit', amount);
- // return the total money of the customer
- return `${currentCustomer.totalMoney}$`;
- }
- withdrawMoney(personalId, amount) {
- (personalId = Number(personalId)), (amount = Number(amount));
- // console.log('ID: ', personalId, 'amount: ', amount); // Delete
- // check if personalId corresponds to an id in this.allCustomers
- // error => 'We have no customer with this ID!'
- let errorMessage = 'We have no customer with this ID!';
- let currentCustomer = this.idFound(personalId);
- if (!currentCustomer) {
- throw new Error(errorMessage);
- }
- // if not enough money in account to withdraw
- errorMessage = `${currentCustomer.firstName} ${currentCustomer.lastName} does not have enough money to withdraw that amount!`;
- if (currentCustomer.totalMoney < amount) {
- throw new Error(errorMessage);
- }
- // if no error subtract amount from totalMoney
- currentCustomer.totalMoney -= amount;
- // update transaction history
- this.updateTransactions(currentCustomer, 'withdraw', amount);
- // return the total money of the customer
- return `${currentCustomer.totalMoney}$`;
- }
- customerInfo(personalId) {
- const output = [];
- personalId = Number(personalId);
- // console.log('ID: ', personalId); // Delete
- // check if personalId corresponds to an id in this.allCustomers
- // error => 'We have no customer with this ID!'
- let currentCustomer = this.idFound(personalId);
- if (!currentCustomer) {
- throw new Error(errorMessage);
- }
- // if no error return all the customer info
- output.push(`Bank name: ${this._bankName}`);
- output.push(
- `Customer name: ${currentCustomer.firstName} ${currentCustomer.lastName}`
- );
- output.push(`Customer ID: ${currentCustomer.personalId}`);
- output.push(`Total Money: ${currentCustomer.totalMoney}$`);
- // for each transaction reversed
- output.push('Transactions:');
- currentCustomer.transactions.reverse().forEach((transaction) => {
- console.log(transaction);
- if (transaction.operation == 'withdraw') {
- output.push(
- `${transaction.n}. ${currentCustomer.firstName} ${currentCustomer.lastName} withdrew ${transaction.amount}$!`
- );
- } else {
- output.push(
- `${transaction.n}. ${currentCustomer.firstName} ${currentCustomer.lastName} made deposit of ${transaction.amount}$!`
- );
- }
- });
- return output.join('\n');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement