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
balance
variable 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
balance
variable through thedeposit
,withdraw
, andgetBalance
methods. - Avoid direct access to
balance
from outside theBankAccount
function.
- 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
BankAccount
instance. - 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>