import java.util.Scanner; // Importing Scanner for taking user input
// Class representing a Bank Account
class BankAccount {
// Instance variables
private String accountHolderName;
private String accountNumber;
private double balance;
// Constructor to initialize the bank account
public BankAccount(String accountHolderName, String accountNumber, double initialBalance) {
this.accountHolderName = accountHolderName;
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// Method to deposit money into the account
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Successfully deposited Rs. " + amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}
// Method to withdraw money from the account
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Successfully withdrew Rs. " + amount);
} else if (amount > balance) {
System.out.println("Insufficient balance.");
} else {
System.out.println("Withdrawal amount must be positive.");
}
}
// Method to check the balance of the account
public void checkBalance() {
System.out.println("Account Balance: Rs. " + balance);
}
// Method to display account details
public void displayAccountDetails() {
System.out.println("Account Holder: " + accountHolderName);
System.out.println("Account Number: " + accountNumber);
System.out.println("Current Balance: Rs. " + balance);
}
}
// Main class to run the Bank Account Management System
public class BankSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompting user to create an account
System.out.print("Enter account holder name: ");
String accountHolderName = input.nextLine();
System.out.print("Enter account number: ");
String accountNumber = input.nextLine();
System.out.print("Enter initial deposit: ");
double initialDeposit = input.nextDouble();
// Creating a new BankAccount object
BankAccount account = new BankAccount(accountHolderName, accountNumber, initialDeposit);
System.out.println("Account successfully created!");
int choice;
do {
// Displaying menu for user actions
System.out.println("\n*** Bank Account Management System ***");
System.out.println("1. Deposit Money");
System.out.println("2. Withdraw Money");
System.out.println("3. Check Balance");
System.out.println("4. Display Account Details");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
choice = input.nextInt();
// Switch case for different user actions
switch (choice) {
case 1:
System.out.print("Enter amount to deposit: ");
double depositAmount = input.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = input.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
account.checkBalance();
break;
case 4:
account.displayAccountDetails();
break;
case 5:
System.out.println("Thank you for using the Bank Account Management System.");
break;
default:
System.out.println("Invalid choice. Please choose a valid option.");
break;
}
} while (choice != 5);
// Closing the Scanner to prevent resource leak
input.close();
}
}
BashExplanation of the Program:
The user can perform different actions using the menu, and the program will continue running until the user chooses to exit.
class BankAccount { ... }
:
This class represents a bank account. It holds the account holder’s name, account number, and balance as instance variables. The methods inside the class allow operations like depositing, withdrawing, and checking the balance.
Constructor:
The constructor BankAccount(String accountHolderName, String accountNumber, double initialBalance)
initializes the account holder’s details and the initial balance.
Methods:
deposit(double amount)
: Adds money to the balance if the deposit amount is positive.
withdraw(double amount)
: Deducts money from the balance if there are sufficient funds and the withdrawal amount is positive.
checkBalance()
: Displays the current account balance.
displayAccountDetails()
: Shows the account holder’s name, account number, and balance.
Main Class (BankSystem
):
This is where the program starts. It asks the user to enter details to create an account. Once the account is created, it provides a menu of options to deposit money, withdraw money, check the balance, or display account details.
Menu System:
Sample Output:
Enter account holder name: John Doe
Enter account number: 123456789
Enter initial deposit: 5000
Account successfully created!
*** Bank Account Management System ***
1. Deposit Money
2. Withdraw Money
3. Check Balance
4. Display Account Details
5. Exit
Choose an option: 1
Enter amount to deposit: 2000
Successfully deposited Rs. 2000
*** Bank Account Management System ***
1. Deposit Money
2. Withdraw Money
3. Check Balance
4. Display Account Details
5. Exit
Choose an option: 2
Enter amount to withdraw: 3000
Successfully withdrew Rs. 3000
*** Bank Account Management System ***
1. Deposit Money
2. Withdraw Money
3. Check Balance
4. Display Account Details
5. Exit
Choose an option: 3
Account Balance: Rs. 4000
*** Bank Account Management System ***
1. Deposit Money
2. Withdraw Money
3. Check Balance
4. Display Account Details
5. Exit
Choose an option: 5
Thank you for using the Bank Account Management System.
BashSummary:
This program demonstrates the use of classes, objects, and methods in Java. It allows users to create a bank account, deposit or withdraw money, check the account balance, and view account details. The program keeps running until the user decides to exit, making it interactive and user-friendly.
This would be a great addition to your website, showcasing more advanced Java concepts.