Java Reference
In-Depth Information
5.
Create a constructor in the
CheckingAccount
class that also uses the superclass constructor and
adds this instance variable initialization.
6.
Make the
Account
class abstract, ensuring that all accounts are either
SavingsAccounts
or
CheckingAccounts
.
7.
Override the
withdraw()
method in the
CheckingAccount
class to ensure a minimum balance is
maintained in the account. Store the minimum balance required as a static variable. If the with-
drawal is too large, throw an exception, otherwise use the
super.withdraw()
method.
8.
Override the
toString()
method from the
Object
class to print information about
Account
instances in the
Account
class. Override the
Account
class
toString()
method to specialize it for
the subclasses in
SavingsAccount
and
CheckingAccount
.
9.
Adapt the
AccountManager
class to try out your new classes and methods. It should look some-
thing like this:
public class AccountManager {
public static void main(String[] args) {
Account mySavings = new SavingsAccount("Save001", "10.00");
try {
mySavings.withdraw("5.00");
} catch (IllegalArgumentException e) {
System.err.println("Invalid Withdrawal");
}
Account myChecking = new CheckingAccount("Check002", "10.00", 1);
try {
myChecking.withdraw("5.00");
} catch (IllegalArgumentException e) {
System.err.println("Invalid Withdrawal");
}
myChecking.deposit("500.00");
try {
myChecking.withdraw("5.00");
} catch (IllegalArgumentException e) {
System.err.println("Invalid Withdrawal");
}
}
}
Your
Account
,
SavingsAccount
, and
CheckingAccount
classes should resemble these:
import java.math.BigDecimal;
public abstract class Account {
private String name;
private BigDecimal amount;
public Account(String acctName, String startAmount) {
Search WWH ::
Custom Search