Let us create a banking application in java. A banking application needs the following.
Member variables:
An account: customer name, account number and deposit amount.
Member functions:
‘deposit()’ – it deposits the amount to the account.
‘withdraw()’ – it withdraws the amount from the account.
‘display the details()’ -this function displays the account
details.
import java.util.Scanner;
public class BankingEg {
private String
CustomerName;
private int
account_no;
private double
balance;
public
BankingEg(String name, int account_no, double init_Balance) {
this.CustomerName = name;
this.account_no = account_no;
this.balance =
init_Balance;
}
public void
deposit(double amount) {
if (amount
> 0) {
balance +=
amount;
System.out.println("Deposit successful! Your Current balance is:
" + balance);
} else {
System.out.println("Sorry. It's a Invalid deposit amount.");
}
}
public void
withdraw(double amount) {
if (amount
> 0 && amount <= balance) {
balance -=
amount;
System.out.println("Withdrawal successful! Your Current balance is:
" + balance);
} else {
System.out.println("Sorry.Amount is Insufficient");
}
}
public void
displayIt() {
System.out.println("\nAccount Details:");
System.out.println("Account Holder Name: " + CustomerName);
System.out.println("Account Number: " + account_no);
System.out.println("Current Balance: " + balance);
}
public static void
main(String[] args) {
Scanner s1 =
new Scanner(System.in);
// Creating
the account
System.out.print("Enter the name of account holder: ");
String name =
s1.nextLine();
System.out.print("Enter the account number: ");
int account_no
= s1.nextInt();
System.out.print("Enter the initial deposit amount: ");
double
init_Balance = s1.nextDouble();
BankingEg b1 =
new BankingEg(name, account_no, init_Balance);
// Display
menu
while (true) {
System.out.println("\nXYZ Banking:");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Display the Account Details");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice
= s1.nextInt();
switch
(choice) {
case
1:
System.out.print("Enter the deposit amount: ");
double depositAmount = s1.nextDouble();
b1.deposit(depositAmount);
break;
case
2:
System.out.print("Enter the withdrawal amount: ");
double withdrawalAmount = s1.nextDouble();
b1.withdraw(withdrawalAmount);
break;
case
3:
b1.displayIt();
break;
case
4:
System.out.println("Thank you for using the banking
application!");
s1.close();
return;
default:
System.out.println("Invalid option. Please try again.");
}
}
}
}
Output:
Here ,is the output.
C:\raji\blog>javac BankingEg.java
C:\raji\blog>java BankingEg
Enter the name of account holder: Rajeswari
Enter the account number: 123
Enter the initial deposit amount: 24000
XYZ Banking:
1. Deposit
2. Withdraw
3. Display the Account Details
4. Exit
Choose an option: 1
Enter the deposit amount: 2500
Deposit successful! Your Current balance is: 26500.0
XYZ Banking:
1. Deposit
2. Withdraw
3. Display the Account Details
4. Exit
Choose an option: 2
Enter the withdrawal amount: 1500
Withdrawal successful! Your Current balance is: 25000.0
XYZ Banking:
1. Deposit
2. Withdraw
3. Display the Account Details
4. Exit
Choose an option: 3
Account Details:
Account Holder Name: Rajeswari
Account Number: 123
Current Balance: 25000.0
XYZ Banking:
1. Deposit
2. Withdraw
3. Display the Account Details
4. Exit
Choose an option: 4
Thank you for using the banking application!
This is the simple Banking application implemented in java. Hope
this code is useful to you. Keep coding!!!
No comments:
Post a Comment