简体   繁体   中英

Simple bank system (JavaScript)

My main aim is to build a simple bank system (transfer, withdraw, receive, transactions history, addCard), each user can have a maximum of 3 cards.

Here is my code:

const parameters = {
    tax: 0.05,
    cardsLimit: 3
}

function userCard(number) {
    const cardInformation = { 
    balance: 100,
    transactionLimit: 100,
    historyLogs: [],
    key: number 
    }

    // getCardOptions - return iformation about card
    function getCardOptions() {
      return cardInformation;
    }


    // putCredits
    function putCredits(amount) {
        cardInformation.balance = cardInformation.balance + amount;
        updateHistoryLogs('Received credits', amount)
    }

    // takeCredits
    function takeCredits(amount) {
            cardInformation.balance = cardInformation.balance - amount + amount * parameters.tax;
            updateHistoryLogs('Withdrawal of credits', amount);
    }

    // setTransactionLimit
    function setTransactionLimit(amount) {
        cardInformation.transactionLimit = amount;
        updateHistoryLogs('Transaction limit change', amount);
    }

    // transferCredits
    function transferCredits(amount, card) {
        let amountwithTaxes = amount + amount * parameters.tax;
        if (amountwithTaxes > cardInformation.balance) {
            console.log('Not enough money')
        } else if (amountwithTaxes > cardInformation.transactionLimit) {
            console.log('Amount exceed the Transaction limit')
        } else {
            this.takeCredits(amount);
            card.putCredits(amount);
        }
    }

    // Logs about past transactions
    function updateHistoryLogs(operation, credits) {
        let input = {
            operation,
            credits,
            operationTime: new Date().toLocaleString('en-GB')
        };
        cardInformation.historyLogs.push(input);
    }
    return {
      getCardOptions,
      putCredits,
      takeCredits,
      setTransactionLimit,
      transferCredits
      };

}

// addCard/getcardbyKey
class UserAccount {
    constructor(name) {
        this.name = name;
        this.cards = [];
    }
    addCard() {
        if (this.cards.length < parameters.cardsLimit) {
            console.log('You\'ve got too many cards')
        } else {
            this.cards.push(userCard(this.cards.length + 1));
        }
    }
    getCardByKey(number) {
        return this.cards[number - 1];
    }
}

When I'm trying to execute it and make several transactions, the following error occuring:

Uncaught TypeError: Cannot read property 'putCredits' of undefined

I've defined putCredits property in my main function and don't know why code is not executing.

Appreciate any help and advices!

Here is the code I'm trying to execute:

let user = new UserAccount('John');

user.addCard()
user.addCard()

let card1 = user.getCardByKey(1);
let card2 = user.getCardByKey(2);

card1.putCredits(1000);
card1.setTransactionLimit(500);
card1.transferCredits(500, card2);

card2.takeCredits(50);

console.log(card1.getCardOptions()); 

The problem is that you never add any card because you had an incorrect if condition. So when you tried to access to a card: let card1 = user.getCardByKey(1); you got undefined.

Your addCard if condition should be like:

if (this.cards.length > parameters.cardsLimit)

The code:

 const parameters = { tax: 0.05, cardsLimit: 3 } function userCard(number) { const cardInformation = { balance: 100, transactionLimit: 100, historyLogs: [], key: number } // getCardOptions - return iformation about card function getCardOptions() { return cardInformation; } // putCredits function putCredits(amount) { cardInformation.balance = cardInformation.balance + amount; updateHistoryLogs('Received credits', amount) } // takeCredits function takeCredits(amount) { cardInformation.balance = cardInformation.balance - amount + amount * parameters.tax; updateHistoryLogs('Withdrawal of credits', amount); } // setTransactionLimit function setTransactionLimit(amount) { cardInformation.transactionLimit = amount; updateHistoryLogs('Transaction limit change', amount); } // transferCredits function transferCredits(amount, card) { let amountwithTaxes = amount + amount * parameters.tax; if (amountwithTaxes > cardInformation.balance) { console.log('Not enough money') } else if (amountwithTaxes > cardInformation.transactionLimit) { console.log('Amount exceed the Transaction limit') } else { this.takeCredits(amount); card.putCredits(amount); } } // Logs about past transactions function updateHistoryLogs(operation, credits) { let input = { operation, credits, operationTime: new Date().toLocaleString('en-GB') }; cardInformation.historyLogs.push(input); } return { getCardOptions, putCredits, takeCredits, setTransactionLimit, transferCredits }; } // addCard/getcardbyKey class UserAccount { constructor(name) { this.name = name; this.cards = []; } addCard() { if (this.cards.length > parameters.cardsLimit) { console.log('You\\'ve got too many cards') } else { this.cards.push(userCard(this.cards.length + 1)); } } getCardByKey(number) { return this.cards[number - 1]; } } let user = new UserAccount('John'); user.addCard() user.addCard() let card1 = user.getCardByKey(1); let card2 = user.getCardByKey(2); card1.putCredits(1000); card1.setTransactionLimit(500); card1.transferCredits(500, card2.getCardOptions().balance); card2.takeCredits(50); console.log(card1.getCardOptions()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM