Reputation: 47
I have a project where I am creating a BankAccount superclas with a SavingsAccount subclass. Everything is working fine but I am having trouble returning the string I specifically want.
Example: (Trimmed Down)
public class BankAccount {
public static final double MONTHS_IN_YEAR = 12.0;
private String myCustomerName;
private double myAccountBalance;
private double myInterestRate;
protected int myMonthlyWithdrawCount;
protected double myMonthlyServiceCharges;
public BankAccount(final String theNameOfOwner,
final double theInterestRate) {
myCustomerName = theNameOfOwner;
myAccountBalance = 0.0;
myInterestRate = theInterestRate;
myMonthlyWithdrawCount = 0;
myMonthlyServiceCharges = 0.0;
}
public String toString() {
String result = "";
result += String.format("BankAccount[owner: %s, balance: %.2f,",
myCustomerName, myAccountBalance);
result += String.format(" interest rate: %.2f,", myInterestRate);
result += String.format("\n\t\t ");
result += String.format("number of withdrawals this month: %d,",
myMonthlyWithdrawCount);
result += String.format(" service charges for this month: %.2f]",
myMonthlyServiceCharges);
return result;
}
}
The driver class will use the toString method for BankAccount and it prints this:
BankAccount[owner: John Doe, balance: 0.00, interest rate: 0.05,
number of withdrawals this month: 0, service charges for this month: 0.00]
(Which is perfect for this superclass)
However, here comes the subclass SavingsAccount
public class SavingsAccount extends BankAccount {
public static final double SAVINGS_THRESHOLD = 25.0;
private boolean myStatusIsActive;
public SavingsAccount(final String theNameOfOwner,
final double theInterestRate) {
super(theNameOfOwner, theInterestRate);
myStatusIsActive = false;
if (super.getBalance() >= SAVINGS_THRESHOLD) {
myStatusIsActive = true;
}
}
public String toString() {
String result = "";
result += "SavingsAccount";
result += super.toString();
return result;
}
}
When calling the toString method of SavingsAccount, it prints:
SavingsAccountBankAccount[owner: Dan Doe, balance: 0.00, interest rate: 0.05, number of withdrawals this month: 0, service charges for this month: 0.00]
(I don't want the BankAccount
to be included, I just want it to print the "SavingsAccount
" header then goes directly to "[owner: Dan Doe,"
I've tried having it return "SavingsAccount
" first which it does correctly, but when calling super.toString(), it also ends up returning the BankAccount
header which I don't want.
Any ideas on how I could fix this issue?
Upvotes: 1
Views: 1141
Reputation: 50716
A few options I can think of:
Replace "BankAccount" with "SavingsAccount" instead of just appending it:
@Override
public String toString() {
return super.toString().replaceFirst("BankAccount", "SavingsAccount");
}
Move the details portion into its own method, and call it from each toString()
with the appropriate header:
// BankAccount.java
@Override
public String toString() {
return "BankAccount" + getDetailsString();
}
protected String getDetailsString() {
String result = String.format("[owner: %s, balance: %.2f,",
myCustomerName, myAccountBalance);
// ...
return result;
}
// SavingsAccount.java
@Override
public String toString() {
return "SavingsAccount" + getDetailsString();
}
Get the runtime class name in the superclass so it doesn't need to be overridden altogether:
@Override
public String toString() {
String result = String.format("%s[owner: %s, balance: %.2f,",
getClass().getSimpleName(), myCustomerName, myAccountBalance);
// ...
return result;
}
Upvotes: 4