Banking System

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.2 kB
1
Indexable
Never
function main() {
    class Account {
        #accountNumber;
        #balance;

        constructor(accountNumber) {
            this.#accountNumber = accountNumber;
            this.#balance = 0;
        }

        get balance() {
            return this.#balance;
        }

        set balance(value) {
            if (value < 0) {
                console.log("Please enter a positive value for the balance.");
            } else {
                this.#balance = value;
            }
        }

        deposit(amount) {
            if (amount > 0) {
                this.#balance += amount;
            } else {
                console.log("Please enter a positive amount to deposit.");
            }
        }

        withdraw(amount) {
            if (amount <= this.#balance) {
                this.#balance -= amount;
            } else {
                console.log("Insufficient balance.");
            }
        }

        get getbalance() {
            return this.balance;
        }
    }

    const myAccount = new Account("1234567890");
    myAccount.deposit(500);
    myAccount.withdraw(200);
    console.log(myAccount.getbalance); // should output: 300

    return Account;
}
main();
Leave a Comment