java Q1. Implement the following inheritance hierarchy: BankCard DebitCard Credi
ID: 3717957 • Letter: J
Question
java
Q1. Implement the following inheritance hierarchy: BankCard DebitCard Creditcard 1) The abstract class BankCard is the superclass of the hierarchy. It has two instance variables: name of the person (of type String) accountNo (of type int). It has a constructor that initializes the name and accountNo to the specified values. It has two methods: toString that returns the name and the Account number and two abstract methods deposit(double amount) and withdraw(double a 2 Class DebitCard is derived from BankCard. It has one instance variable: balance (of type double). It has a constructor that calls the superclass constructor to initialize the name, accountNo, and then it initializes balance to the specified values. It has three methods: a method deposit that implements the abstract method and add to the balance and a method withdraw that implements the abstract method it takes the amount to be withdrawn and compare it to the balance if it was less than the balance it will subtract it, else and returm true else it will return false, and a method toString that overrides the superclass version and returns the person's name, accountNumber and balance. Class CreditCard is derived from BankCard. It has two instance variable limit and balance (of type double). It has a constructor that calls the superclass constructor to nitialize the name, accountNo, and then it initializes the limit to the specified values. Balance is initialized by zero. It has three methods: a method deposit that implements the abstract method and subtract from the balance, a method withdraw that check if the amount + 2% of the amount is less than limit-balance if yes then adds the required amount + 2% of the amount to the balance and retum true else return false, a method with toString that overrides the superclass version and person's name, accountNumber and remaining limit (limit-balance) 4) Write a test class named BankCardTest that has a main method to test your classes. In the main method, create an array of references to BankCard objects, the array holds two instances of DebitCard object and two instances of a Credit object. You should create the objects first, the information are Account 0: Name: Ali, Type: Debit Card Account Number: 1234, Balance: 700 Account 1 Name: Yahya, Type: Credit Card, Account Number: 1235, Limit: 1000 Account 2 Name: Mohammed, Type: Credit Card, Account Number: 1254, Limit: 1000 Account 3: Name: Nour, Type: Debit Card, Account Number: 1256, Balance: 500 Then Prompt the user to perform 5 operations on the cards (using a loop). The user wil pick the index of the required card and you shall perform the required operation on it. Sample Output: Choose index of Card: 8 Enter operation type 1. Deposit 2. Withdraw: 1 Enter Amount: 200 Card is credited Ali, Account Number 1234, Balance: 9e8.8e Choose index of Card: 1 Enter operation type 1. Deposit 2. Withdraw: 2 Enter Amount: 2808 Card cannot be debited with this amount Yahya, Account Number 1235, Limit: 1800.e0 Choose index of Card: e Enter operation type 1. Deposit 2. Withdraw:2 Enter Amount: 388 Card is debited A??, Account Number 1234, Balance: 608.ee Choose index of Card: 2 Enter operation type 1. Deposit 2. Withdraw:2 Enter Amount: 100 Card is debited Mohanmed, Account Number 1254, Limit: 898.8e Choose index of Card: 2 Enter operation type 1. Deposit 2. Withdraw:1 Enter Amount: 180 Card is credited Mohanmed, Account Number 1254, Limit 998.88Explanation / Answer
Bank.java
public abstract class Bank{
protected String name;
protected int accountNo;
public Bank(String name, int accountNo) {
super();
this.name = name;
this.accountNo = accountNo;
}
public String toString() {
return name + ", Account Number " + accountNo;
}
public abstract boolean deposit(double amount);
public abstract boolean withdraw(double amount);
}
DebitCard.java
public class DebitCard extends Bank{
private double balance;
public DebitCard(String name, int accountNo, double balance) {
super(name, accountNo);
this.balance = balance;
}
@Override
public boolean deposit(double amount) {
// TODO Auto-generated method stub
balance += amount;
System.out.println("Card is credited");
return true;
}
@Override
public boolean withdraw(double amount) {
// TODO Auto-generated method stub
if(amount <= balance) {
balance -= amount;
System.out.println("Card is debited");
this.toString();
return true;
}
else {
System.out.println("Card cannot be debited with this amount");
System.out.println(name + ", Account Number " + accountNo + ", Limit: " + balance);
return false;
}
}
public String toString() {
return name + ", Account Number " + accountNo + ", Balance: " + balance;
}
}
CreditCard.java
public class CreditCard extends Bank{
private double balance;
private double limit;
public CreditCard(String name, int accountNo, double limit) {
super(name, accountNo);
this.balance = 0;
this.limit = limit;
}
@Override
public boolean deposit(double amount) {
// TODO Auto-generated method stub
double total = 1.02 * balance;
if(total < limit) {
balance = total;
System.out.println("Card is credited");
this.toString();
return true;
}
else {
System.out.println("Card cannot be credited with this amount");
System.out.println(name + ", Account Number " + accountNo + ", Limit: " + limit);
return false;
}
}
@Override
public boolean withdraw(double amount) {
// TODO Auto-generated method stub
double total = 1.02 * balance;
if(amount <= balance) {
balance -= amount;
System.out.println("Card is debited");
this.toString();
return true;
}
else {
System.out.println("Card cannot be debited with this amount");
System.out.println(name + ", Account Number " + accountNo + ", Limit: " + balance);
return false;
}
}
public String toString() {
return name + ", Account Number " + accountNo + ", Balance: " + balance;
}
}
Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args){
Bank accounts[] = new Bank[4];
accounts[0] = new DebitCard("Ali", 1234, 700);
accounts[1] = new DebitCard("Yahya", 1235, 1000);
accounts[2] = new CreditCard("Mohammed", 1254, 1000);
accounts[3] = new CreditCard("Nour", 1256, 500);
Scanner in = new Scanner(System.in);
for(int i = 0; i < 5; i++) {
System.out.print("Choose index of card: ");
int index = in.nextInt();
System.out.print("Enter operation type 1. Deposit 2. Withdraw: ");
int operation = in.nextInt();
System.out.print("Enter amount: ");
double amount = in.nextDouble();
if(operation == 1)
if(accounts[index].deposit(amount))
System.out.println(accounts[index] + " ");
else
if(accounts[index].withdraw(amount))
System.out.println(accounts[index] + " ");
}
}
}
**Comment for any further queries.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.