0% found this document useful (0 votes)
1 views6 pages

Import Java.util.Scanner;

The document provides a Java implementation of a Bank Account System using Object-Oriented Programming principles. It includes a BankAccount class for managing account balance with synchronized methods for thread-safe deposits and withdrawals, and a Customer class for handling transactions in a multithreaded environment. The main method initializes a bank account and displays a menu for user interactions to deposit, withdraw, check balance, or exit the system.

Uploaded by

vaishupc17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Import Java.util.Scanner;

The document provides a Java implementation of a Bank Account System using Object-Oriented Programming principles. It includes a BankAccount class for managing account balance with synchronized methods for thread-safe deposits and withdrawals, and a Customer class for handling transactions in a multithreaded environment. The main method initializes a bank account and displays a menu for user interactions to deposit, withdraw, check balance, or exit the system.

Uploaded by

vaishupc17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Code for: Bank Account System

import java.util.Scanner;

public class BankAccountSystem {

// BankAccount class using OOP concepts

static class BankAccount {

private double balance;

private String accountNumber;

public BankAccount(String accountNumber, double initialBalance) {

this.accountNumber = accountNumber;

this.balance = 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;

System.out.println(Thread.currentThread().getName() + " deposited: " + amount);

System.out.println("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;

System.out.println(Thread.currentThread().getName() + " withdrew: " + amount);

System.out.println("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) {

this.account = account;

this.transactionType = transactionType;

this.amount = amount;

@Override

public void run() {

try {

if (transactionType.equalsIgnoreCase("deposit")) {
account.deposit(amount);

} else if (transactionType.equalsIgnoreCase("withdraw")) {

account.withdraw(amount);

} else {

System.out.println("Invalid transaction type.");

} catch (IllegalArgumentException e) {

System.out.println("Exception occurred: " + e.getMessage());

// Method to display the menu and handle user inputs

public static void displayMenu(BankAccount account) {

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("\n=== Bank Account System ===");

System.out.println("1. Deposit");

System.out.println("2. Withdraw");

System.out.println("3. Check Balance");

System.out.println("4. Exit");

System.out.print("Choose an option (1-4): ");

int choice = scanner.nextInt();

switch (choice) {

case 1:
System.out.print("Enter amount to deposit: ");

double depositAmount = scanner.nextDouble();

Thread depositThread = new Thread(new Customer(account, "deposit",


depositAmount), "Deposit Thread");

depositThread.start();

try {

depositThread.join(); // Wait for the deposit thread to complete

} catch (InterruptedException e) {

e.printStackTrace();

break;

case 2:

System.out.print("Enter amount to withdraw: ");

double withdrawAmount = scanner.nextDouble();

Thread withdrawThread = new Thread(new Customer(account, "withdraw",


withdrawAmount), "Withdraw Thread");

withdrawThread.start();

try {

withdrawThread.join(); // Wait for the withdrawal thread to complete

} catch (InterruptedException e) {

e.printStackTrace();

break;

case 3:
System.out.println("Current Balance: " + account.getBalance());

break;

case 4:

System.out.println("Exiting the system...");

scanner.close();

System.exit(0); // Exit the program

default:

System.out.println("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:

You might also like