Classes and Objects: Create a bank account

Question: Design a class named Account that contains:
o A private int data field named id for the account (default 0 ).
o A private double data field named balance for the account (default 0 ).
o A private double data field named annualInterestRate that stores the
current interest rate (default 0 ). Assume that all accounts have the
same interest rate.
o A private Date data field named dateCreated that stores the date when
the account was created.
o A no-arg constructor that creates a default account.
o A constructor that creates an account with the specified id and initial
balance.
o The accessor and mutator methods for id , balance , and
annualInterestRate .
o The accessor method for dateCreated .
o A method named getMonthlyInterestRate() that returns the monthly
interest rate.
o A method named getMonthlyInterest() that returns the monthly
interest.
o A method named withdraw that withdraws a specified amount from
the account.o
A method named deposit that deposits a specified amount to the
account.
Draw the UML diagram for the class then implement the class. (Hint: The
method getMonthlyInterest() is to return monthly interest, not the
interest rate. Monthly interest is balance * monthlyInterestRate .
monthlyInterestRate is annualInterestRate / 12 . Note
annualInterestRate is a percentage, for example 4.5%. You need to divide
it by 100.)
Write a test program that creates an Account object with an account ID of
1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the
withdraw method to withdraw $2,500, use the deposit method to deposit
$3,000, and print the balance, the monthly interest, and the date when this
account was created.

package Ch9;

import java.util.Date;


/**
 * ********************Account********************
 *
 * -id: int
 * -balance: double
 * -annualInterestRate: double
 * -date: Date
 *
 * -----------------------------------------
 * +Account()
 * +Account(id: int, balance: double)
 * +getID() :int
 * +setId(id: int)
 * +getBalance() :double
 * +setBalance(balance: double)
 * +getannualInterestRate(): double
 * +setannualInterestRate(annualInterestRate: double)
 * +getMonthlyInterestRate(): double
 * +getMonthlyInterest(): double
 * +withdraw(double amountToWithdraw)
 * +deopsit(double amountToDeposit)
 *
 */

public class Ex07Account {

    //Data fields
    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;

    //Constructors
    /** no-arg constructor that creates a default account */
    public Ex07Account(){
        id=0;
        balance=0;
        annualInterestRate=0;
        dateCreated= new Date();
    }

    /** constructor that creates an account with the specified id and initial balance.*/
    public Ex07Account(int id, double balance){
        this.id= id;
        this.balance= balance;
        dateCreated= new Date();
    }

    /**Accessor and mutator methods for id , balance , and annualInterestRate .*/
    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;
    }

    /**accessor method for dateCreated*/
    public Date getDateCreated(){
        return dateCreated;
    }

    /**method named getMonthlyInterestRate() that returns the monthly interest rate.*/
    public double getMonthlyInterestRate(){
        return annualInterestRate/12;
    }

    /**method named getMonthlyInterest() that returns the monthly interest.*/
    public double getMonthlyInterest(){
        return balance*(getMonthlyInterestRate()/100);
    }

    /**method named withdraw that withdraws a specified amount from the account.*/
    public void withdraw(double amountToWithdraw){
        balance-=amountToWithdraw;
    }

    /**method named deposit that deposits a specified amount to the account.*/
    public void deposit(double amountToDeposit){
        balance+=amountToDeposit;
    }
}
package Ch9;

/**
 * Q: Design a class named Account that contains:
 o A private int data field named id for the account (default 0 ).
 o A private double data field named balance for the account (default 0 ).
 o A private double data field named annualInterestRate that stores the
 current interest rate (default 0 ). Assume that all accounts have the
 same interest rate.
 o A private Date data field named dateCreated that stores the date when
 the account was created.
 o A no-arg constructor that creates a default account.
 o A constructor that creates an account with the specified id and initial
 balance.
 o The accessor and mutator methods for id , balance , and
 annualInterestRate .
 o The accessor method for dateCreated .
 o A method named getMonthlyInterestRate() that returns the monthly
 interest rate.
 o A method named getMonthlyInterest() that returns the monthly
 interest.
 o A method named withdraw that withdraws a specified amount from
 the account.o
 A method named deposit that deposits a specified amount to the
 account.
 Draw the UML diagram for the class then implement the class. (Hint: The
 method getMonthlyInterest() is to return monthly interest, not the
 interest rate. Monthly interest is balance * monthlyInterestRate .
 monthlyInterestRate is annualInterestRate / 12 . Note
 annualInterestRate is a percentage, for example 4.5%. You need to divide
 it by 100.)
 Write a test program that creates an Account object with an account ID of
 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the
 withdraw method to withdraw $2,500, use the deposit method to deposit
 $3,000, and print the balance, the monthly interest, and the date when this
 account was created.
 * Created by aarushi on 19/6/21.
 */

public class Ex07AccountTest {

    public static void main (String [] args){
        Ex07Account obj= new Ex07Account(1122, 20000);
        obj.setAnnualInterestRate(4.5);
        obj.withdraw(2500);
        obj.deposit(3000);
        System.out.println("Balance: " + obj.getBalance());
        System.out.println("Monthly Interest: " + obj.getMonthlyInterest());
        System.out.println("Date account was created: " + obj.getDateCreated());
    }
}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: