Code for: Bank Account System
import [Link];
public class BankAccountSystem {
// BankAccount class using OOP concepts
static class BankAccount {
private double balance;
private String accountNumber;
public BankAccount(String accountNumber, double initialBalance) {
[Link] = accountNumber;
[Link] = initialBalance;
// Synchronized method to make thread-safe deposits
public synchronized void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Deposit amount must be positive.");
balance += amount;
[Link]([Link]().getName() + " deposited: " + amount);
[Link]("Updated Balance: " + balance);
// Synchronized method to make thread-safe withdrawals
public synchronized void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Withdrawal amount must be positive.");
if (amount > balance) {
throw new IllegalArgumentException("Insufficient balance.");
balance -= amount;
[Link]([Link]().getName() + " withdrew: " + amount);
[Link]("Updated Balance: " + balance);
public synchronized double getBalance() {
return balance;
// Customer class implementing Runnable for multithreading
static class Customer implements Runnable {
private BankAccount account;
private String transactionType;
private double amount;
public Customer(BankAccount account, String transactionType, double amount) {
[Link] = account;
[Link] = transactionType;
[Link] = amount;
@Override
public void run() {
try {
if ([Link]("deposit")) {
[Link](amount);
} else if ([Link]("withdraw")) {
[Link](amount);
} else {
[Link]("Invalid transaction type.");
} catch (IllegalArgumentException e) {
[Link]("Exception occurred: " + [Link]());
// Method to display the menu and handle user inputs
public static void displayMenu(BankAccount account) {
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("\n=== Bank Account System ===");
[Link]("1. Deposit");
[Link]("2. Withdraw");
[Link]("3. Check Balance");
[Link]("4. Exit");
[Link]("Choose an option (1-4): ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter amount to deposit: ");
double depositAmount = [Link]();
Thread depositThread = new Thread(new Customer(account, "deposit",
depositAmount), "Deposit Thread");
[Link]();
try {
[Link](); // Wait for the deposit thread to complete
} catch (InterruptedException e) {
[Link]();
break;
case 2:
[Link]("Enter amount to withdraw: ");
double withdrawAmount = [Link]();
Thread withdrawThread = new Thread(new Customer(account, "withdraw",
withdrawAmount), "Withdraw Thread");
[Link]();
try {
[Link](); // Wait for the withdrawal thread to complete
} catch (InterruptedException e) {
[Link]();
break;
case 3:
[Link]("Current Balance: " + [Link]());
break;
case 4:
[Link]("Exiting the system...");
[Link]();
[Link](0); // Exit the program
default:
[Link]("Invalid option! Please choose again.");
break;
// Main method
public static void main(String[] args) {
// Creating a BankAccount object with an initial balance
BankAccount account = new BankAccount("12345", 1000);
// Display the menu to the user
displayMenu(account);
Output: