Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Account class )Design a class named Account that contains: A private int dat

ID: 3713115 • Letter: T

Question

The Account class

)Design a class named Account that contains:

A private int data field named id for the account (default 0).

A private double data field named balance for the account (default 0).

A private double data field named annualInterestRate that stores the current interest rate

(default 0). All accounts have the same interest rate (static variable).

A private Date data field named dateCreated that stores the date when the account was

created. (import java.util.Date)

A no-argument constructor that creates a default account.

A constructor that creates an account with the specified id and initial balance.

The accessor and mutator methods for id, balance, and annualInterestRate.

A method named getMonthlyInterest() that returns the monthly interest that is accrued on

the current balance.

A method named withdraw that withdraws a specified amount from the account.

A method named deposit that deposits a specified amount to the account.

A toString method which will print the current Account Id, the balance, the monthly

interest and the date when the account was created. (FYI. The Date class also contains a

toString() method which will provide a String of the date created)

Implement the class above.

?

Write a test program that creates an array of Account objects, using a constant

MAX_ACCOUNTS to define the size of the Account array.

Prompt the user if they would like to create an account. If so, create an Account in the

array and prompt the user for the Account Id and the initial balance.

Prompt the user for the interest rate, a withdrawal amount and a deposit amount

Add an option to display all Accounts so you can see how changing the interest rate for

one account will change the interest rate for all accounts.

This is my code so far:

AccountTest.java:

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class AccountTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);
Account ac=new Account(1,5000.00);
System.out.println("Enter the annual intrest rate");
double intrestRate=sc.nextDouble();
ac.setAnnualIntrestRate(intrestRate);
Date d=new Date();
Calendar currentDate = Calendar.getInstance();
ac.setDateCreated(currentDate.getTime());
System.out.println("Date id "+ac.getDateCreated());
System.out.println("Monthly intrest rate is :"+ac.getMonthlyInterestRate());
System.out.println("Monthly intrest is :"+ac.getMonthlyInterest());
System.out.println("Enter Amount for diposite ");
double dipositeAmount=sc.nextDouble();
System.out.println("The amount after diposite is :"+ac.diposite(dipositeAmount));
System.out.println("Enter Amount to withdraw :");
double withdramount= sc.nextDouble();
System.out.println("The amount remain after with draw "+ac.withDraw(withdramount));
System.out.println("The total amount is "+ac.totalBalance());

}

}

Account.java:

import java.util.Date;
public class Account {

/**
* @param args
*/
private int id=0;
private double balance=0;
private double annualIntrestRate=0;
private Date dateCreated;

public Account() {
super();
}


public Account(int id, double balance) {
super();
this.id = id;
this.balance = balance;
}

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 getAnnualIntrestRate() {
return annualIntrestRate;
}

public void setAnnualIntrestRate(double annualIntrestRate) {
this.annualIntrestRate = annualIntrestRate;
}


public Date getDateCreated() {
return dateCreated;
}


public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}

public double getMonthlyInterestRate()
{
return (this.getAnnualIntrestRate()/12);
}
public double getMonthlyInterest()
{
return (getBalance() *getMonthlyInterestRate()/100);
}
public double withDraw(double balance)
{
this.setBalance(this.getBalance()-balance);
return this.getBalance();
}
public double diposite(double balance)
{
this.setBalance(this.getBalance()+balance);
return this.getBalance();
}
public double totalBalance()
{
balance =balance + getMonthlyInterest();
return balance;
}
}

I need the output to look like this

Sample Output

1. Add new Account

2. Display Accounts

3. Exit

Choose: 1

Enter Account ID: 2211

Enter Account Balance: 20000

Enter Account Interest Rate: 4.5

Enter Amount to Withdrawal: 2500

Enter Amount of Deposit: 3000

Account: 2211

Balance: $20500.00

Interest Rate: 4.50

Monthly Interest: $76.88

Date Created : Sat Oct 28 11:41:10 PDT 2017

Explanation / Answer

Hi,

Find below the modified versions of your Account.java and AccountTest.java files.

//************************************************[ Account.java ]*****************************************************

import java.util.Date;

public class Account {

   /**
   * @param args
   */
   private int id = 0;
   private double balance = 0;
   private static double annualIntrestRate = 0;
   private Date dateCreated;

   public Account() {
       super();
   }

   public Account(int id, double balance) {
       super();
       this.id = id;
       this.balance = balance;
   }

   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 getAnnualIntrestRate() {
       return annualIntrestRate;
   }

   public void setAnnualIntrestRate(double annualIntrestRate1) {
       annualIntrestRate = annualIntrestRate1;
   }

   public Date getDateCreated() {
       return dateCreated;
   }

   public void setDateCreated(Date dateCreated) {
       this.dateCreated = dateCreated;
   }

   public double getMonthlyInterestRate() {
       return (this.getAnnualIntrestRate() / 12);
   }

   public double getMonthlyInterest() {
       return (getBalance() * getMonthlyInterestRate() / 100);
   }

   public double withDraw(double balance) {
       this.setBalance(this.getBalance() - balance);
       return this.getBalance();
   }

   public double deposite(double balance) {
       this.setBalance(this.getBalance() + balance);
       return this.getBalance();
   }

   public double totalBalance() {
       balance = balance + getMonthlyInterest();
       return balance;
   }

   @Override
   public String toString() {
       return "Account : " + id
               + " Balance : $" + balance
               + " Interest Rate : " + getAnnualIntrestRate()
               + " Monthly Interest : $" + getMonthlyInterest()
               + " Date Created : " + getDateCreated().toString();
   }
}

//*******************************************************[ AccountTest.java ]**************************************************


import java.util.Date;
import java.util.Scanner;

public class AccountTest {

   private static final int MAX_ACCOUNTS = 5;
  
   public static void main(String[] args) throws Exception {
      
       Account[] accounts = new Account[MAX_ACCOUNTS];

       Scanner sc = new Scanner(System.in);
       System.out.println("***************************************************************");
       System.out.println("1. Add new Account 2. Display Accounts 3. Exit Choose: ");
       int choice = sc.nextInt();
       int count = 0;
      
       do {
           if(choice != 1 && choice != 2) {
               System.out.println("Exiting...");
               break;
           }
           if(choice == 1) {
              
               if(count < MAX_ACCOUNTS) {
                   System.out.println("-----------------------------------------------------------------");
                   System.out.println("Enter Account Id(must be an integer) : ");
                   int accountId = sc.nextInt();
                   System.out.println("Enter Initial Account Balance(must be an number) : ");
                   double initialBal = sc.nextDouble();
                   System.out.println("Enter Interest Rate(must be an number) : ");
                   double interestRate = sc.nextDouble();
                   System.out.println("Enter Withdrawal Amount(must be an number) : ");
                   double withdrawAmt = sc.nextDouble();
                   System.out.println("Enter Deposit Amount(must be an number) : ");
                   double depositAmt = sc.nextDouble();
                  
                   Account account = new Account(accountId, initialBal);
                   account.setDateCreated(new Date());
                   account.setAnnualIntrestRate(interestRate);
                   account.withDraw(withdrawAmt);
                   account.deposite(depositAmt);
                  
                   accounts[count] = account;
                   System.out.println("-----------------------------------------------------------------");
                   System.out.println("An account with Following Details was added to array : ");
                   System.out.println(account.toString());
                  
                   count++;
               }else {
                   System.out.println("No more accounts can be added. Accounts array max capacity reached.");
               }
              
           }
           if(choice == 2) {
               for(Account account : accounts) {
                   if(null != account) {
                       System.out.println(account.toString());
                   }
               }
           }
           System.out.println();
           System.out.println("***************************************************************");
           System.out.println("1. Add new Account 2. Display Accounts 3. Exit Choose: ");
           choice = sc.nextInt();
           if(choice != 1 && choice != 2) {
               System.out.println("Exiting...");
           }
       }while(choice == 1 || choice == 2);
       sc.close();

   }

}