In: Computer Science
Design a class named Account that contains:
A private int data field named id for the account.
A private double data field named balance for the account.
A private double data field named annualInterestRate that stores the current interest rate.
A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0.
The accessor and mutator methods for id, balance, and annualInterestRate.
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named withdraw(amount) that withdraws a specified amount from the account.
A method named deposit(amount) that deposits a specified amount to the account.
Write a test program that creates an Account object with an account ID of 1122, a balance of $20000, and an annual interest rate of 4.5%. Use the withdraw function to withdraw $2500, use the deposit function to deposit $3000, and print the balance, and the monthly interest.
class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account() {
id = 0;
balance = 0;
annualInterestRate = 0;
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
public double getMonthlyInterest() {
return (getMonthlyInterestRate() / 100) * balance;
}
}
class AccountTest {
public static void main(String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.printf("Balance: %.2f\n", account.getBalance());
System.out.printf("Monthly interest: %.2f\n", account.getMonthlyInterest());
}
}
