In this post, we will learn Inheritance-Account Program in java Programming language.
Question:
Atlas Bank is providing services for various accounts like Savings, Current,Loan for Education ,Vehicle, Home, Personal. As a first step they wanted to automate Savings Account. As a java developer, design the classes as per the given specifications
Create a Account class with the following private attributes:
accntNo – int
accntHolderName – String
accntBalance – double
Provide the appropriate getters/setters
Create another class SavingsAccount which is of type Account with the additional private attributes:
float minBalance
Provide appropriate getters/setters
Override the toString() method in the SavingsAccount class to display the details in the below format
AccountNo:<<accntNum>>-AccountHolderName:<<accntHolderName>>-AccountBalance<<accntBalance>>-MinimumBalance:<<minBalance>>
Example: AccountNo:1000-AccountHolderName:Peter-AccountBalance:80000-MinimumBalance:1000
In the TestMain class , get the details of Savings Account set the details and print the reference of SavingsAccount
Sample Input/Output:
Enter the AccountNumber: 4000
Enter the Account Holder Name: Rose
Enter the Account Balance: 4000
Enter the Minimum Balance: 500
AccountNo:4000-AccountHolderName:Rose-AccountBalance:4000.0-MinimumBalance:500.0
CODE:–
TestMain.java
import java.util.*; public class TestMain { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the AccountNumber: "); int an=sc.nextInt(); System.out.println("Enter the Account Holder Name: "); String hn=sc.next(); System.out.println("Enter the Account Balance: "); double ab=sc.nextDouble(); System.out.println("Enter the Minimum Balance: "); float mb=sc.nextFloat(); SavingsAccount sa=new SavingsAccount(); sa.setAccntNo(an); sa.setAccntHolderName(hn); sa.setAccntBalance(ab); sa.setMinBalance(mb); System.out.println(sa.toString()); } }
Account.java
public class Account { private int accntNo; private String accntHolderName; private double accntBalance; public void setAccntNo(int accntNo) { this.accntNo=accntNo; } public int getAccntNo() { return accntNo; } public void setAccntHolderName(String accntHolderName) { this.accntHolderName=accntHolderName; } public String getAccntHolderName() { return accntHolderName; } public void setAccntBalance(double accntBalance) { this.accntBalance=accntBalance; } public double getAccntBalance() { return accntBalance; } }
SavingAccount.java
public class SavingsAccount extends Account { private float minBalance; public void setMinBalance(float minBalance) { this.minBalance=minBalance; } public float getMinBalance() { return minBalance; } @Override public String toString() { String str="AccountNo:"+String.valueOf(getAccntNo())+"-AccountHolderName:"+getAccntHolderName()+"-AccountBalance:" +String.valueOf(getAccntBalance())+"-MinimumBalance:"+String.valueOf(getMinBalance()); return str; } }