I need to modify my class Account where the applyInterest method is abstract, re
ID: 643510 • Letter: I
Question
I need to modify my class Account where the applyInterest method is abstract, rename method details() as toString and @Override
Here is my code so far...
public abstract class Account {
public double balance;
public double interestRate;
//no-parameter default constructor
public Account() {
this.balance = 0;
this.interestRate = 0.0025;
}
//method to get balance on account
public double getBalance() {
return balance;
}
//method to calculate deposit
public TransactionResult deposit(double amount) {
if (amount > 0) {
this.balance += amount;
return new TransactionResult(true, "Deposit was made");
} else
return new TransactionResult(false,
"Deposited amount is less than or zero");
}
//method to calculate withdrawal
public TransactionResult withdraw(double amount) {
if (amount > 0) {
double bal = this.balance - amount;
if (bal >= 0) {
this.balance = bal;
return new TransactionResult(true, "Withdrawl was made");
} else
return new TransactionResult(false,
"Withdrawl amount is greater than balance");
} else
return new TransactionResult(false,
"Withdrawl amount is less than or zero");
}
//method to calculate interest on balance
public void applyInterest() {
this.balance += (this.balance * this.interestRate);
}
// method that returns the value of field interestRate
public double getInterestRate(){
return interestRate;
}
//method using StringBuilder
public String details() {
StringBuilder sb = new StringBuilder("Account balance: ");
String amt = String.format("%.2f", this.balance);
sb.append(amt);
return new String(sb);
}
}
Explanation / Answer
public abstract class Account {
public double balance;
public double interestRate;
// no-parameter default constructor
public Account() {
this.balance = 0;
this.interestRate = 0.0025;
}
//method to get balance on account
public double getBalance() {
return balance;
}
// method to calculate deposit
public TransactionResult deposit(double amount) {
if (amount > 0) {
this.balance += amount;
return new TransactionResult(true, "Deposit was made");
} else
return new TransactionResult(false, "Deposited amount is less than or zero");
}
//method to calculate withdrawal
public TransactionResult withdraw(double amount) {
if (amount > 0) {
double bal = this.balance - amount;
if (bal >= 0) {
this.balance = bal;
return new TransactionResult(true, "Withdrawl was made");
} else
return new TransactionResult(false,
"Withdrawl amount is greater than balance");
} else
return new TransactionResult(false,
"Withdrawl amount is less than or zero");
}
// method to calculate interest on balance
public abstract void applyInterest();
// method that returns the value of field interestRate
public double getInterestRate(){
return interestRate;
}
// method using StringBuilder
public String toString() {
StringBuilder sb = new StringBuilder("Account balance: ");
String amt = String.format("%.2f", this.balance);
sb.append(amt);
return new String(sb);
}
}