In: Computer Science
Create a class named BankAccount, containing:
Write a class definition for a subclass, CheckingAccount, that contains:
//BankAccount.java
public class BankAccount {
private String name;
private double balance;
public BankAccount(String name) {
this.name = name;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void withdraw(double amount)
{
this.balance-=amount;
}
}
//CheckingAccount.java
public class CheckingAccount extends BankAccount {
private boolean overdraft;
public CheckingAccount(String name,boolean od)
{
super(name);
this.overdraft=od;
}
public boolean hasOverdraft()
{
return this.overdraft;
}
public boolean clearCheck(double amount) {
if
(amount<this.getBalance())
{
this.withdraw(amount);
return
true;
}
else if(this.hasOverdraft())
{
this.withdraw(amount);
return
true;
}
else
return
false;
}
}
//Tester.java (maiin class)
public class Tester {
public static void main(String[] args) {
CheckingAccount acc1 = new
CheckingAccount("David", true);//overdraft
CheckingAccount acc2 = new
CheckingAccount("Pete", false);//no overdraft
acc1.setBalance(100);
acc2.setBalance(100);
System.out.println("Balance in acc1
before trying to clear check: "+acc1.getBalance());
System.out.println("Balance in acc2
before trying to clear check: "+acc2.getBalance());
System.out.println("Trying to cash
check more than balance for acc1:"+acc1.clearCheck(101));
System.out.println("Trying to cash
check more than balance for acc2:"+acc2.clearCheck(101));;
System.out.println("Balance in acc1
after trying to clear check: "+acc1.getBalance());
System.out.println("Balance in acc2
after trying to clear check: "+acc2.getBalance());
}
}