Create an inheritance hierarchy that a bank might use to represent customers’ ba
ID: 3807675 • Letter: C
Question
Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit money into their accounts and withdraw money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction.
Create an inheritance hierarchy containing base class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base class Account should include data members like name, gender (male/female), phone number, address and account_balance. The class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0.0. If not, the balance should be set to 0.0, and the constructor should display an error message, indicating that the initial balance was invalid.
The class should provide three functions. The function Credit() should add an amount to the current balance. The function Debit() should withdraw amount from the account balance and ensure that he debit amount does not exceed the account balance. If it does, the balance should be left unchanged and the function should print the message “Debit amount exceeds account balance”. Function getBalance() should return the current balance.
Derived class SavingsAccount should inherit the functionality of the calss Account, but also include a data member interest-rate of type double indicating the interest rate (percentage). SavingsAccount’s constructor should receive the initial balance, as well as an initial interest_rate for the SavingsAccount. SavingsAccount should provide public method Calculate-Interest() that returns a double value indicating the amount of interest earned by an account. The function CalculateInterest() should determine this amount by multiplying the amount by the interest rate. [Note: the SavingsAccount should inherit the public functions Credit and Debit without redefining them.]
Derived class CheckingAccount should inherit the functionality of the class Account, but also include a data member fee-charged of type double indicating the fee charged per transaction (let’s say $0.50 per transaction). CheckingAccount’ s constructor should retrieve the initial balance, as well as a parameter indicating a fee amount. CheckingAccount should provide a public function IsFeeChareged() that returns a Boolean value indicating weather the fee amount is charged or not. If the transaction is successful then only fee must be charged. If the transaction is cancelled then the fee must not be charged. [Note: the CheckingAccount should inherit the public functions Credit and Debit as is without redefining them].
After defining the classes in this hierarchy, write a program that creates objects of each class and tests their functions. Add interest to the SavingsAccount object by first invoking its CalculateInteres() function, then passing the returned interest amount to the object’s Credit function
Explanation / Answer
package bank;
import java.util.Scanner;
class Account1 {
String name,gender,phoneNo,Address;
double account_bal=0;
public Account1()
{
name="";
gender="";
phoneNo="";
Address="";
account_bal=0;
}
public Account1(String name, String gender, String phoneNo, String address, double account_bal) {
this.name = name;
this.gender = gender;
this.phoneNo = phoneNo;
Address = address;
this.account_bal = account_bal;
}
public void credit(double amt)
{
account_bal=account_bal+amt;
System.out.println("Amount Successfully Credited. New Account Balance is:"+account_bal);
}
public void debit(double amt)
{
if(amt>account_bal)
{
System.out.println("Debit Amount Exceeds the Account Balance.");
}
else
{
account_bal=account_bal-amt;
System.out.println("Amount Successfully Debited. New Account Balance:"+account_bal);
}
}
public double getBalance()
{
return account_bal;
}
}
class SavingAccount extends Account
{
double interestrate=0.0,act_bal;
public SavingAccount()
{
interestrate=0;
act_bal=0;
}
public SavingAccount(double act,double rate)
{
act_bal=act;
interestrate=rate;
}
public double calculate_interest()
{
double interest;
interest=(act_bal*2*interestrate)/100;
act_bal=act_bal+interest;
return interest;
}
}
class CheckingAccount extends SavingAccount
{
double act_bal,fee;
public CheckingAccount(double act_bal,double fee)
{
this.act_bal=act_bal;
this.fee=fee;
}
public boolean isFeeCharged()
{
System.out.println("Account_Bal:"+act_bal);
if(act_bal>1)
{
System.out.println("Account Charged:"+fee);
return true;
}
return false;
}
}
public class Account
{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double bal;
int ch;
char c;
String name,gender,phno,address;
System.out.println("Welcome to Bank System:");
System.out.println("Enter Customer Name:");
name=sc.nextLine();
System.out.println("Enter Gender:");
gender=sc.nextLine();
System.out.println("Enter Customer Phone Number:");
phno=sc.nextLine();
System.out.println("Enter Customer Address:");
address=sc.nextLine();
System.out.println("Enter Starting Balance:");
bal=sc.nextDouble();
Account1 a=new Account1(name,gender,phno,address,bal);
do
{
System.out.println("*****Menu***** 1.Credit 2.Withdraw 3.Interest 4.Check Balance Enter your Choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter Amount U want to Credit:");
bal=sc.nextDouble();
a.credit(bal);
break;
case 2:
System.out.println("Enter Amount To Withdraw:");
bal=sc.nextDouble();
a.debit(bal);
break;
case 3:
SavingAccount s=new SavingAccount(a.getBalance(),5.0);
System.out.println("Interest:"+s.calculate_interest());
break;
case 4:
CheckingAccount ca=new CheckingAccount(a.getBalance(),0.50);
a.account_bal=a.getBalance()-0.50;
ca.isFeeCharged();
break;
default:break;
}
System.out.println("Do U Want To Continue(Y Or N)");
c=sc.next().charAt(0);
}while(c=='Y'||c=='y');
}//end of main
}//end of class Account
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.