Data Abstraction Homework
An intro to data abstraction
JavaScript BankAccount Function
Create a BankAccount class that returns an object. Inside this function, follow these steps:
- Initialize Balance:
- Create a
balancevariable and initialize it to100, or any balance you choose (representing $100).
- Create a
- Methods:
deposit(amount): Adds a specified amount to the balance.withdraw(amount): Deducts a specified amount from the balance.getBalance(): Returns the current balance.transfer(ammount): Transfers a specified amount from one account to another.
- Encapsulation:
- Only allow access to the
balancevariable through thedeposit,withdraw, andgetBalancemethods. - Avoid direct access to
balancefrom outside theBankAccountfunction.
- Only allow access to the
- Validation:
- A deposit can only add positive amounts.
- A withdrawal can only occur if the amount is available in the balance.
- Testing:
- Test the account by creating a new
BankAccountinstance. - Call each method to verify functionality.
- Test the account by creating a new
- Inheritance:
- Create accounts that can transfer money to eachother
%%js
class BankAccount {
}
<IPython.core.display.Javascript object>