In: Computer Science
The following java files are created from this:
Objective: To learn about inheritance and understand how to inherit and override superclass methods and to be able to invoke superclass constructors
In this assignment we will simulate a bank transactions that offers the customer the following types:
Program Specification and implementation
Create a superclass BankAccount and two subclasses CheckingAccount and SavingsAccount. All the bank accounts should support getBalance, deposit and withdraw methods. Follow the below detailed specifications for each class.
BankAccount:
- Implement instance field balance
- Constructor creates an account with 0 balance.
- Constructor to create an account with an initial balance
- deposit method that takes in amount to be deposited
- withdraw method that takes amount to withdraw
- getBalance method to return the current balance in the account.
- transferMoney method that transfers money from this bank account to other bank account. This method takes two arguments (BankAccount otherAccount, double amount)
- display method that displays the account balance.
CheckingAccount:
- Constant instance fields for ALLOWED_TRANS and TRANS_FEE. Allow for 2 free transactions and $3 for additional transaction.
- instance field transactionCount (specific and new to CheckingAccount).
- Constructor to create zero balance account.
- Constructor to create an account with an initial balance
- override deposit and withdraw methods in order to increment the transaction count.
- chargeFees that charges transaction fee if any to the account at the end of the period.
SavingsAccount:
- Instance field interestRate that holds the interest rate for period.
- Constructor that sets interest rate.
- Constructor that sets rate and initial balance.
- addCompoundInterest method that adds the interest for the current period to the account balance. Deposit the interest to the account. Use this formula to calculate interest = balance * interestRate / 100.0.
TransactionsDriver:
- Instantiate a savings account name it dadsSavings with interestRate 0.3%
- Instantiate a checking account and name it kidsChecking.
- Deposit 10000 into dadsSavings account.
- Transfer 3000 to kidsChecking
- Withdraw 200 from kidsChecking
- Withdraw 400 from kidsChecking
- Withdraw 300 from kidsChecking
- Withdraw 500 from kidsChecking
- Withdraw 400 from kidsChecking
- Invoke end of period interest for the savings account
- Invoke end of period transactions fees for the checking account
- Display the final balance for dadsSavings account
- Display the final balance for kidsChecking account
- End of program
Sample Output:
At the end of these transactions the bank accounts should have the below balances
End of the period account balances
Dad's savings. Account balance: 7021.0
kid's checking. Account balance: 1188.0
Also, draw the inheritance UML diagram for the three bank accounts classes.
BankAccount.java
==========================
package bank;
public class BankAccount {
// instance field
protected double balance;
// constructors
// create an account with 0 balance
public BankAccount() {
balance = 0;
}
// create an account with given amount of
balance
public BankAccount(double amount){
balance = amount;
}
// takes in amount to be deposited and add it to
balance
public void deposit(double amount) {
balance = balance + amount;
}
// takes amount to withdraw and removes that amount
from balance
public void withdraw(double amount) {
balance = balance - amount;
}
// return the current balance in the account
public double getBalance() {
return balance;
}
// transfers money from this bank account to other
bank account
public void transferMoney(BankAccount otherAccount,
double amount) {
// remove money from this bank
account
this.withdraw(amount);
// add money to other bank
account
otherAccount.deposit(amount);
}
// displays the account balance
public void display() {
System.out.printf("Account balance:
%.2f", balance);
System.out.println();
}
}
===========================
CheckingAccount.java
===========================
package bank;
public class CheckingAccount extends BankAccount{
// instance fields
private final int ALLOWED_TRANS = 2;
private final int TRANS_FEE = 3;
private int transactionCount;
// constructors
// constructor to create zero balance account.
public CheckingAccount() {
super(); // call to parent class
constructor
transactionCount = 0;
}
// constructor to create an account with an initial
balance
public CheckingAccount(double amount) {
super(amount); // call to parent
class constructor
transactionCount = 0;
}
// override method to increment the transaction
count
@Override
public void deposit(double amount) {
// call parent class method for
deposit
super.deposit(amount);
// increase transaction
counts
transactionCount++;
}
// override method to increment the transaction
count
@Override
public void withdraw(double amount) {
// call parent class method for
deposit
super.withdraw(amount);
// increase transaction
counts
transactionCount++;
}
// charges transaction fee if any to the account at
the end of the period
public void chargeFees() {
// check for the transaction
counts
if(transactionCount >
ALLOWED_TRANS) {
// charge fees
for more transactions
double charge =
(transactionCount - ALLOWED_TRANS)*TRANS_FEE;
// update
balance
this.balance =
this.balance - charge;
}
}
}
============================
SavingsAccount.java
============================
package bank;
public class SavingsAccount extends BankAccount{
// Instance field
private double interestRate;
// constructors
// constructor that sets interest rate
public SavingsAccount(double rate) {
super(); // call to parent class
constructor
interestRate = rate;
}
// constructor that sets rate and initial
balance
public SavingsAccount(double rate, double amount)
{
super(amount); // call to parent
class constructor
interestRate = rate;
}
// adds the interest for the current period to the
account balance
public void addCompoundInterest() {
// calculate interest
double interest = this.balance *
interestRate / 100.0;
// deposit the interest to the
account
this.deposit(interest);
}
}
==================================
TransactionsDriver.java
==================================
package bank;
public class TransactionsDriver {
// main method to run the program
public static void main(String[] args) {
// Instantiate a savings account
name it dadsSavings with interestRate 0.3%
SavingsAccount dadsSavings = new
SavingsAccount(0.3);
// Instantiate a checking account
and name it kidsChecking
CheckingAccount kidsChecking = new
CheckingAccount();
// Deposit 10000 into dadsSavings
account.
dadsSavings.deposit(10000);
// Transfer 3000 to
kidsChecking
dadsSavings.transferMoney(kidsChecking, 3000);
// Withdraw 200 from
kidsChecking
kidsChecking.withdraw(200);
// Withdraw 400 from
kidsChecking
kidsChecking.withdraw(400);
// Withdraw 300 from
kidsChecking
kidsChecking.withdraw(300);
// Withdraw 500 from
kidsChecking
kidsChecking.withdraw(500);
// Withdraw 400 from
kidsChecking
kidsChecking.withdraw(400);
// Invoke end of period interest
for the savings account
dadsSavings.addCompoundInterest();
// Invoke end of period
transactions fees for the checking account
kidsChecking.chargeFees();
// Display the final balance for
dadsSavings account
System.out.print("Dad's savings.
");
dadsSavings.display();
// Display the final balance for
kidsChecking account
System.out.print("kid's checking.
");
kidsChecking.display();
}
}


let me know if you have any doubts or problem.