In: Computer Science
Private int data field named id for the account
A private double data field named balance for the account
Private double data field named annualInterestRate that stores the current interest rate Assume all accounts have the same rate
Private date data field named date Created that stores the date when the account was created
No arg constructor that creates an account with the specified id and initial balance
The get and set methods for id balance and annual Interest Rate
Accessor method for Date created
Method named get monthly Interest Rate that returns month interest rate
Method named withdraw that withdraws a specified amount from the account
Method named deposit that deposits a specified amount into the account
Part 2//
Create two subclasses for checking and saving account. Checking accounts has an overdraft limit, but a savings account cant
Write a test program that creates objects of account, savings, and checking’s and invokes their toString() methods.
Add a method named AccountInf to implement the concept of polymorphism to display all the details of an account (This method can display the information of different types of Bank accounts).
JAVA language
Account.java
import java.util.Date; import java.util.Random; public class Account { private int id; private double balance; private double annualInterestRate; private Date dateCreated; public Account() { Random random = new Random(); this.id = random.nextInt(123456); this.balance = random.nextDouble()*10000; annualInterestRate = 10; dateCreated = new Date(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public Date getDateCreated() { return dateCreated; } public double getMonthlyInterestRate() { return annualInterestRate/12; } public void withdraw(double amount) { balance = balance - amount; } public void deposite(double amount) { balance += amount; } @Override public String toString() { return "Account ID: " + getId() + "\tCurrent Balance: " + getBalance() + "\tCreated Date: " + getDateCreated(); } }
CheckingAccount.java
public class CheckingAccount extends Account { private double overdraftLimit; public CheckingAccount() { super(); overdraftLimit = 50; } public double getOverdraftLimit() { return overdraftLimit; } public void setOverdraftLimit(double overdraftLimit) { this.overdraftLimit = overdraftLimit; } @Override public void withdraw(double amount) { if(getBalance() >= amount + overdraftLimit) { super.withdraw(amount); } } }
SavingAccount.java
public class SavingAccount extends Account { }
Test.java
public class Test { public void AccountInfo(Account account) { System.out.println(account.toString()); } public static void main(String[] args) { SavingAccount savingAccount = new SavingAccount(); CheckingAccount checkingAccount = new CheckingAccount(); Test test = new Test(); test.AccountInfo(savingAccount); test.AccountInfo(checkingAccount); } }