Design a class named Account that contains: • A field named ID (int) • A field n
ID: 3696910 • Letter: D
Question
Design a class named Account that contains: • A field named ID (int) • A field named ownerName (string) • A field named interestRate (double) representing the monthly interest rate • A field named balance (double) • Accessor and Mutator functions for ID, ownerName, interestRate and balance fields. • A function named getAnnualInterest( ) that returns the annual interest rate. • A function named withdraw( ) that withdraws a specified amount (given as an argument) from the account. The function verifies that the amount is positive and that there are sufficient funds in the account and issues an error message if these conditions are not met. The function updates the remaining balance accordingly. • A function named deposit( ) that deposits a specified amount (given as an argument) to the account. The function verifies that the amount is positive and issues an error message if it isn’t. The function updates the remaining balance accordingly.
Explanation / Answer
Account.java:-
----------------------------
package com.app.chegg;
public class Account {
private int id;
private String ownerName;
private double interestRate;
private double balance;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
double getAnnualInterest(){
return interestRate*12;
}
public void withdraw(double withdrawAmount) {
if(getBalance()>=withdrawAmount){
balance=balance-withdrawAmount;
System.out.println("WithdrawAmount $"+withdrawAmount+" .CurrentBalance $"+balance);
}
if(getBalance()<withdrawAmount){
System.out.println("Insufficient balance");
}
}
public void deposit(double depositAmount){
balance=balance+depositAmount;
System.out.println("OwnerName: "+ownerName+" .Current Balance$:"+balance);
}
public static void main(String[] args) {
Account account=new Account();
account.setId(123456789);
account.setBalance(5000);
account.setInterestRate(150);
account.setOwnerName("Mahindra");
double annualinterestRate=account.getAnnualInterest();
System.out.println("AnnualInterestRate$:"+annualinterestRate);
account.withdraw(2000);
account.deposit(1000);
}
}
Output :-
-----------------
AnnualInterestRate$:1800.0
WithdrawAmount $2000.0 .CurrentBalance $3000.0
OwnerName: Mahindra Current Balance$:4000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.