In this post, we will learn Bank Account Details Program in java Programming language.
Question:
In the first round of HR interview for a banking sector, HR decides to make candidates design an application which provides only information on transaction like amount withdrawn with respect to fields given. Develop a program to implement this scenario.
Create a class Account with the private attributes:
- accountId int
- accountType String
- balance int
The method public boolean withdraw(int) used to calculate the current balance of the respective account. Before that it should enough balance. If there is enough balance, deduct the amount from the balance and print “Balance amount after withdraw: XXX” and return true. If there is no enough balance, print “Sorry!!! No enough balance” and return false.
Create a class AccountDetails with main function and the below methods :
- public Account getAccountDetails() – This methods gets the input related to Account from the user and returns the Account object with all values set. If the input given for balance is less than or equal to zero, consider it as invalid and display “Balance should be positive”. Continue this kind of evaluation till user enters a positive value.
- public int getWithdrawAmount() – This methods gets the amount to be withdrawn as input from the user and returns the same. If the input given for amount is less than or equal to zero, consider it as invalid and display “Amount should be positive”. Continue this kind of evaluation till user enters a positive value.
Use appropriate getters and setters.
Sample input 1:
Enter account id:
100
Enter account type:
Savings
Enter balance:
10000
Enter amount to be withdrawn:
500
Sample Output 1:
Balance amount after withdraw: 9500
Sample input 2:
Enter account id:
101
Enter account type:
Savings
Enter balance:
1000
Enter amount to be withdrawn:
1500
Sample Output 2:
Sorry!!! No enough balance
Sample input 3:
Enter account id:
100
Enter account type:
Savings
Enter balance:
-100
Balance should be positive
Enter balance:
5000
Enter amount to be withdrawn:
500
Sample Output 1:
Balance amount after withdraw: 4500
CODE:–
public class Account { private int accountId; private String accountType; private int balance; public int getAccountId() { return accountId; } public String getAccountType() { return accountType; } public int getBalance() { return balance; } public void setAccountId(int id) { accountId=id; } public void setAccountType(String s) { accountType=s; } public void setBalance(int b) { balance=b; } public boolean withdraw(int w) { if(getBalance()<w) { System.out.println("Sorry!!! No enough balance"); return false; } else { System.out.println("Balance amount after withdraw: "+(getBalance()-w)); return true; } } }
import java.util.*; public class AccountDetails { public static Account getAccountDetails() { Account acc=new Account(); Scanner sc=new Scanner(System.in); System.out.println("Enter account id: "); acc.setAccountId(sc.nextInt()); sc.nextLine(); System.out.println("Enter account type: "); acc.setAccountType(sc.nextLine()); int b; do { System.out.println("Enter Balance"); acc.setBalance(sc.nextInt()); b=acc.getBalance(); if(b<=0) System.out.println("Balance should be positive"); } while(b<=0); return acc; } public static int getWithdrawAmount() { Scanner sc=new Scanner(System.in); int w; do { System.out.println("Enter amount to be withdrawn:"); w=sc.nextInt(); if(w<=0) System.out.println("Amount should be positive"); } while(w<=0); return w; } public static void main(String[] args) { Account accObj=new Account(); accObj=getAccountDetails(); int c=getWithdrawAmount(); accObj.withdraw(c); } }