In: Computer Science
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class by constructing a ChequingAccount object with id as 10020, and balance as 1000, and show the result of withdrawing 200 from the chequing account. it should be in java
BankAccount.java
public class BankAccount {
// Data members
private int id;
private double balance;
// Parameterized constructor to initialize data
members
public BankAccount(int id, double balance) {
this.id = id;
this.balance = balance;
}
// Getter function for balance
public double getBalance() {
return this.balance;
}
// Withdraw given amount
public void withdraw(double amount) {
this.balance -= amount;
}
}
ChequingAccount.java
public class ChequingAccount extends BankAccount {
private double overdraftlimit = 1000;
// Call super constructor to initialize data
members
public ChequingAccount(int id, double balance) {
super(id, balance);
}
@Override
public void withdraw(double amount) {
// Allow user to only withdraw
amount if it doesn't exceed the overdraftlimit
if (amount <= this.getBalance()
+ overdraftlimit) {
super.withdraw(amount);
} else {
System.out.println("Can't withdraw");
}
}
}
BankAccountTest.java
public class BankAccountTest {
public static void main(String[] args) {
// Create ChequingAccount
object
// Print the balance
// Withdraw 200
// Print balance again
ChequingAccount acc = new
ChequingAccount(10020, 1000);
System.out.println("Balance: " +
acc.getBalance());
acc.withdraw(200);
System.out.println("Balance: " +
acc.getBalance());
}
}
OUTPUT