The following implementation with the obvious meanings of Account, SavingsAccount, and CheckingAccount violates some fundamental object-oriented design
Question:
The following implementation with the obvious meanings of Account, SavingsAccount, and CheckingAccount violates some fundamental object-oriented design principles. Re-implement the three classes to get a clean set of classes.
public class Account {
private double balance;
private int type;
public static final int CHECKING_ACCOUNT = 1;
public static final int SAVINGS_ACCOUNT = 2;
private static final double CHECKING_INTEREST_RATE = 2.0;
private static final double SAVINGS_INTEREST_RATE = 3.0;
/** The second parameter indicates the type of account. */
public Account(double balance, int type) {
this.balance = balance;
this.type = type;
}
/** processDebit is applicable only for CheckingAccount objects */
public void processDebit(CheckingAccount account, double amount) {
account.debit(amount);
}
/** Return the interest depending on the type of the account. */
public double computeInterest() {
if (type == CHECKING_ACCOUNT) {
return balance * CHECKING_INTEREST_RATE / 100;
} else if (type == SAVINGS_ACCOUNT) {
return balance * SAVINGS_INTEREST_RATE / 100;
} else {
return 0.0;
}
}
}
public class CheckingAccount extends Account {
public CheckingAccount(double balance) {
super(balance, Account.CHECKING_ACCOUNT);
}
public void debit(double amount) {
// implementation not shown
}
}
public class SavingsAccount extends Account {
public SavingsAccount(double balance) {
super(balance, Account.SAVINGS_ACCOUNT);
}
}
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang