Question
Q: create the main class in this code and give the code with output (NetBeans) /* * To change this license header, choose License Headers
Q: create the main class in this code and give the code with output (NetBeans)
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package bankscenario; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;
public class BankScenario { private Lock lock = new ReentrantLock();
private Condition updated = lock.newCondition();
private int accountBalance = 0;
public void makeTransaction(int amount, boolean isDeposit) { lock.lock(); try { if (isDeposit) { accountBalance += amount; System.out.println("Bank Teller: Deposited $" + amount + " to the account. New balance: $" + accountBalance); } else { if (accountBalance >= amount) { accountBalance -= amount; System.out.println("Bank Teller: Withdrawn $" + amount + " from the account. New balance: $" + accountBalance); } else { System.out.println("Bank Teller: Insufficient funds. Transaction failed."); } } updated.signal(); } finally { lock.unlock(); } }
public void checkBalance() { lock.lock(); try { System.out.println("Customer: Checking account balance... Current balance: $" + accountBalance); updated.await(); System.out.println("Customer: Account updated. New balance: $" + accountBalance); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started