Answered step by step
Verified Expert Solution
Question
1 Approved Answer
// Java multithreading Create a class named BankAccount that has properties to keep track of its account number, and balance. The class will need getters
// Java multithreading
Create a class named BankAccount that has properties to keep track of its account number, and balance. The class will need getters and setters for these properties, with the getBalance() and setBalance() methods being particularly important. Create a second class, with a main method, that (for now) verifies that you can create an instance of BankAccount, as well as setting and retrieving its balance.
The Transaction Processor class is responsible for performing either a deposit or withdrawal operation on a Bank Account instance. To achieve this, it will need three properties: a Bank Account instance on which to perform the transaction, a variable to keep track of whether the transaction is a deposit or a withdrawal, and the amount that is to be deposited/withdrawn. All three of these properties are to be passed to the Transaction Processor's constructor method. The Transaction Processor will need a perform Transaction() method, which will actually make the deposit or withdrawal, as appropriate, depending on the values of the properties that were set when the processor was constructed. This method should perform some basic sanity checking of the properties before actually making the transaction. Check to make sure that transactions aren't for negative amounts, and that the Bank Account has a balance sufficient to make withdrawals. If there's a problem, print a suitable error message. Add some code to your main method to check the Transaction Processor is working. It should be possible to run many Transaction Processors in parallel using Threading. Make sure that the class implements the Runnable interface. The Transacction Processor class's run() method can simply call performTransaction Modify the code in your main method such that a Bank Account is created with a balance of 1000, and that 100 individual 1 withdrawals are made, each using its own thread. Print the final balance of the account. Note: Make sure that your main method waits for all of the Transaction Processors to finish processing before printing the final balance, or you'll get an answer that's badly wrong, as you saw in the lecture! To achieve this, you'll need to store the Threads you create in your main method in an array (or list), and call .join() on each of the threads in a loop. This will make the main program wait until each of the threads has finished before continuingStep 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