0% found this document useful (0 votes)
236 views

Bank Account

The document defines three classes for different types of bank accounts: BankAccount, CheckingAccount, and SavingsAccount. BankAccount defines basic deposit, withdraw, and balance functions. CheckingAccount extends BankAccount and adds transaction counting and fees. SavingsAccount extends BankAccount and calculates interest to add to the balance.

Uploaded by

Elijah Chen
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
236 views

Bank Account

The document defines three classes for different types of bank accounts: BankAccount, CheckingAccount, and SavingsAccount. BankAccount defines basic deposit, withdraw, and balance functions. CheckingAccount extends BankAccount and adds transaction counting and fees. SavingsAccount extends BankAccount and calculates interest to add to the balance.

Uploaded by

Elijah Chen
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/** * A bank account has a balance that can be changed by * deposits and withdrawals.

*/ public class BankAccount { private double balance; /** * Constructs a bank account with a zero balance. */ public BankAccount() { balance = 0; } /** * Constructs a bank account with a given balance. * @param initialBalance the initial balance */ public BankAccount(double initialBalance) { balance = initialBalance; } /** * Deposits money into the bank account. * @param amount the amount to deposit */ public void deposit(double amount) { balance = balance + amount; } /** * Withdraws money from the bank account. * @param amount the amount to withdraw */ public void withdraw(double amount) { balance = balance - amount; } /** * Gets the current balance of the bank account. * @return the current balance */ public double getBalance() { return balance; } /** * Transfers money from the bank account to another account. * @param amount the amount to transfer * @param other the other account */ public void transfer(double amount, BankAccount other) { withdraw(amount);

other.deposit(amount); } } /** * A checking account that charges transaction fees. */ public class CheckingAccount extends BankAccount { private int transactionCount; private static final int FREE_TRANSACTIONS = 3; private static final double TRANSACTION_FEE = 2.0; /** * Constructs a checking account with a given balance * @param initialBalance the initial balance */ public CheckingAccount(double initialBalance) { //Construct superclass super(initialBalance); //Initialize transaction count transactionCount = 0; } public void deposit(double amount) { transactionCount++; //Now add amount to balance super.deposit(amount); } public void withdraw(double amount) { transactionCount++; //Now subtract amount from balance super.withdraw(amount); } /** * Deducts the accumulated fees and resets the * transaction count. */ public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS); super.withdraw(fees); } transactionCount = 0; } } /** * An account that earns interest at a fixed rate

*/ public class SavingsAccount extends BankAccount { private double interestRate; /** * Constructs a bank account with a given interest rate. * @param rate the interest rate */ public SavingsAccount(double rate) { interestRate = rate; } /** * Adds the earned interest to the account balance. */ public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } }

You might also like