Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000 (the...

80.2K

Verified Solution

Question

Programming

Problem:

Add a condition to the deposit method of the BankAccount class,restricting deposits to $100,000 (the insurance limit of the U.S.government). The method should block until sufficient money hasbeen withdrawn by another thread. Test your program with a largenumber of deposit threads.

Bank account class: (THE ONE THAT NEEDS TO BEEDITED)

/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;

/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}

/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
System.out.print(\"Depositing \" + amount);
double newBalance = balance + amount;
System.out.println(\", new balance is \" + newBalance);
balance = newBalance;
}

/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
System.out.print(\"Withdrawing \" + amount);
double newBalance = balance - amount;
System.out.println(\", new balance is \" + newBalance);
balance = newBalance;
}

/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}

Included for test classes:

Bank Account threadrunner.java

/**
This program runs threads that deposit and withdraw
money from the same bank account.
*/
public class BankAccountThreadRunner
{
public static void main(String[] args)
{
BankAccount account = new BankAccount();
final double AMOUNT = 100;
final int REPETITIONS = 100;
final int THREADS = 100;

for (int i = 1; i <= THREADS; i++)
{
DepositRunnable d = new DepositRunnable(
account, AMOUNT, REPETITIONS);
WithdrawRunnable w = new WithdrawRunnable(
account, AMOUNT, REPETITIONS);

Thread dt = new Thread(d);
Thread wt = new Thread(w);

dt.start();
wt.start();
}
}
}

Withdraw runnable.java:

/**
A withdraw runnable makes periodic withdrawals from a bankaccount.
*/
public class WithdrawRunnable implements Runnable
{
private static final int DELAY = 1;
private BankAccount account;
private double amount;
private int count;

/**
Constructs a withdraw runnable.
@param anAccount the account from which to withdraw money
@param anAmount the amount to withdraw in each repetition
@param aCount the number of repetitions
*/
public WithdrawRunnable(BankAccount anAccount, doubleanAmount,
int aCount)
{
account = anAccount;
amount = anAmount;
count = aCount;
}

DepositRunnable.java

/**
A deposit runnable makes periodic deposits to a bank account.
*/
public class DepositRunnable implements Runnable
{
private static final int DELAY = 1;
private BankAccount account;
private double amount;
private int count;

/**
Constructs a deposit runnable.
@param anAccount the account into which to deposit money
@param anAmount the amount to deposit in each repetition
@param aCount the number of repetitions
*/
public DepositRunnable(BankAccount anAccount, doubleanAmount,
int aCount)
{
account = anAccount;
amount = anAmount;
count = aCount;
}

public void run()
{
try
{
for (int i = 1; i <= count; i++)
{
account.deposit(amount);
Thread.sleep(DELAY);
}
}
catch (InterruptedException exception) {}
}
}

Answer & Explanation Solved by verified expert
3.6 Ratings (633 Votes)
Problem Add a condition to the deposit method of the BankAccount class restricting deposits to 100000 the insurance limit of the US government The method should block until sufficient money has been withdrawn by another thread Test your program with a large number of deposit threads Bank account class THE ONE THAT NEEDS TO BE EDITED Explanation Given question is of bank accounts and thread    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students