In: Computer Science
Write in Java the following:
A. A bank account class that has three protected attributes: account number (string), customer name (string), and balance (float). The class also has a parameterized constructor and a method public void withDraw (float amount) which subtracts the amount provided as a parameter from the balance.
B. A saving account class that is a child class of the bank account class with a private attribute: penality rate (float). This class also has a parameterized constructor and a method public void withDraw (float amount) which decreases the available balance based on the following formula:
balance = balance - amount * (1+penality rate ).
Code:
import java.util.*;
class BankAccount {
protected String accountNumber;
protected String customerName;
protected float balance;
public BankAccount(String accountNumber, String customerName, float balance) {
this.accountNumber = accountNumber;
this.customerName = customerName;
this.balance = balance;
}
public void withdraw(float amount) {
this.balance = this.balance - amount;
}
}
class SavingAccount extends BankAccount {
private float penaltyRate;
public SavingAccount(String accountNumber, String customerName, float balance, float penaltyRate) {
super(accountNumber, customerName, balance);//calling parent class constructor
this.penaltyRate = penaltyRate;
}
public void withdraw(float amount) {
super.balance = super.balance - amount * (1 + penaltyRate / (float) 100.0);
}
}
class Main {
public static void main(String[] args) {
SavingAccount s = new SavingAccount("48499920dx03d", "Tom Michaels", 22000, 5);
//displaying balance information
System.out.println("Current balance : "+s.balance);
//withdrawing amount
s.withdraw(10000);
System.out.print("Balance after withdrawing 10000 at 5 percent penalty rate : ");
System.out.println(s.balance);
}
}
Code screenshot:
output: