Question
Perform the following tasks In the driving program, Create an Account array to store 10 Account or CDAccount objects randomly . (accountID from 1001~ 1010,
Perform the following tasks In the driving program,
- Create an Account array to store 10 Account or CDAccount objects randomly.
(accountID from 1001~ 1010, and generate the initial balance and CDAccount duration randomly)
2. Use a for-loop to invoke toString() method through the Account object reference to display account information. After withdraw $500 and deposit $1000 on each account, display the account information again.
3. The toString() method in the Account objects, returns a string of the account number and balance; for CDAccount objects, returns a string of the account number, initial balance, duration, mature balance, and Date created.
Modify the toString() methods in the Account class, and CDAccount class if necessary.
//Account Class
package Module4;
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated = new Date();
// constructor with all parameters
public Account(int id, double balance, double annualInterestRate, Date dateCreated) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.dateCreated = dateCreated;
}
// default constructor
public Account() {
}
// getter and setter methods
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 void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
// calculate monthly interest rate
public double getMonthlyInterestRate() {
return Math.round(((this.annualInterestRate / 12) * 100.0) / 100.0);
}
// calculate monthly interest
public double getMonthlyInterest() {
return (balance * this.getAnnualInterestRate()) / 100;
}
// withdraw amount if amount is less then balance
public void withdraw(double amount) {
if (amount
this.setBalance(balance - amount);
}
}
// deposit account if amount > 0
public void deposit(double amount) {
if (amount > 0) {
this.setBalance(balance + amount);
}
}
}
//CDAccount
package Module4;
import java.lang.Math;
import java.util.Date;
public class CDAccount extends Account {
private int duration;
private double CDannualInterestRate;
CDAccount(){
}
CDAccount(int id,double balance,int duration){
super.setId(id);
super.setBalance(balance);
this.duration = duration;
super.setDateCreated(new Date());
super.setAnnualInterestRate(5.50);
}
public void setDuration(int duration) {
this.duration = duration;
}
public int getDuration() {
return this.duration;
}
public double getMatureBalance() {
double matureBalance = 0;
matureBalance = super.getBalance() * Math.pow((1+super.getMonthlyInterestRate()),this.duration);
return matureBalance;
}
public void setCDAnnualInterestRate(double CDannualInterestRate) {
this.CDannualInterestRate = CDannualInterestRate;
}
public double getCDAnnualInterestRate() {
return super.getAnnualInterestRate();
}
public double getMonthlyInterestRate() {
return super.getMonthlyInterestRate();
}
public double getMonthlyInterest() {
return super.getMonthlyInterest();
}
public final void withdraw(double num) {
System.out.println("A CD Account can't withdraw any cash. You need to close this CD account.");
}
public final void deposit(double num) {
System.out.println("A CD Account can't make any additional deposit. You may open another CD account.");
}
public String toString() {
String res = String.format("%2s%25.2f%20.2f%16.2f%5s%20s", super.getId(), super.getBalance(),
this.getMatureBalance(), this.getAnnualInterestRate(), "",
this.getDateCreated());
System.out.format(res);
System.out.println();
return res;
}
public void displayMonthlyinterests() {
System.out.println();
for(int i=0;i String res = String.format("%2s%25.2f", "Month "+Integer.toString((i+1)), (this.getBalance() + this.getMonthlyInterest()*(i+1))); System.out.println(res); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started