Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Description
You will be implementing a single class: StockGame. This class will process an input file to record simulated stock trades for users.

Instructions
This program will process simulated stock trades for users. The stock price and number of available shares will be determined by the buy and sell orders from traders, whose selections will be stored in a file available to the program. Each submitted order will be processed via the instructions represented in the file. Here is an example:

----------
Stock Price: 100.00
Available Shares: 100
Trade number: 1
Name: Xander
Purchasing 5 shares at 100.00...
----------
Stock Price: 107.50
Available Shares: 95
Trade number: 2
Name: Xander
Selling 3 shares at 107.50...
----------
Stock Price: 101.50
Available Shares: 98
Trade number: 3
Name: Afua
Purchasing 8 shares at 101.50...
----------
Stock Price: 113.50
Available Shares: 90
Trade number: 4
Name: Afua
Purchasing 10 shares at 113.50...
----------
Stock Price: 128.50
Available Shares: 80
Trade number: 5
Name: Xander
Purchasing 16 shares at 128.50...
----------
Stock Price: 152.50
Available Shares: 64
Trade number: 6
Name: Afua
Purchasing 7 shares at 152.50...
----------
Stock Price: 163.00
Available Shares: 57
Trade number: 7
Name: Xander
Purchasing 4 shares at 163.00...
----------
Stock Price: 169.00
Available Shares: 53
Trade number: 8
Name: Xander
Selling 2 shares at 169.00...
----------
Stock Price: 165.00
Available Shares: 55
Trade number: 9
Name: Afua
Selling 9 shares at 165.00...

In this example, there were nine trades. The traders Afua and Xander each placed buy and sell orders, changing the stock price as each trade was processed. As the solution is concurrent, the order of trades is not pre-determined. In the next run, Afua may go first. The trades for each participant are included in their own files. Your solution will synchronize access to the stock price and available shares to ensure the trader threads do not encounter race conditions.

Requirements Overview:
Create a class that utilizes static variables in a deterministic and multithreaded manner. Your solution must have three static variables:
stockPrice: A double, the current price associated with the stock. The initial value is 100.00.
availableShares: An int, the current number of stock shares available for purchase. The initial value is 100.
tradeCount: An int, the number of trades made so far.
The class must extend Thread.
Objects of the class will be passed a String name and a String filename containing the trades for that user.
Within the run() method, the class must iterate through each line of the file and apply the trade to the current stock.
The processing described above should happen concurrently for multiple threads.
The only synchronized portions of the solution should be for accessing shared resources or printing to the console.
Do not synchronize the entire run method. You will not receive credit if you do so.
Class Requirements:
StockGame must extend Thread.

Fields
Field Name

Type

Access Modifier

Description

stockPrice
double

private static

The current price associated with the stock

availableShares
int

private static

The current number of stock shares available for purchase

tradeCount
int

private static

The current number of trades made in the game.

name
String

private

The name of this trader.

numShares
int

private

The number of shares this player owns.

fileName

String

private

The name of the file containing this user's trades.

Constructors
Parameters

Access Modifier(s)

Description

String name, String fileName

public

Initialize the parameters to their respective fields. Initialize the numShares field to 0. It may be helpful to use tradeCount to decide whether or not to initialize the values of the static variables in the constructor.

Methods
You may implement any methods you choose to accomplish the goal, as long as you have a run() method that will process data according to the implementation requirements.

Implementation Details
The specifics of the implementation are up to you. The general requirements are below.

Remember, all access to shared resources must be synchronized to prevent race conditions.
Begin reading the file. Each line contains two values, a String and an int.
The String will be either "BUY" or "SELL".
The int will be an integer value greater than 0.
Any other value should be treated as an error. Print "Error, invalid input!" and do not make any modifications for that round.
Begin a round by printing the round separator "----------" and the current stock information.
Stock Price
Available Shares
Trade Number
Name of user
For each round, also print an additional line which indicates the action taking place as shown in the instructions section.
Before processing the trade, ensure it is valid:
"BUY"
Ensure that the number of shares being purchased is less than or equal to the number of shares that are available.
If not, print "Insufficient shares available, cancelling order..." and end the round without making any modifications.
"SELL"
Ensure that the number of shares being sold is less than or equal to the number shares that user owns.
If not, print "Insufficient owned shares, cancelling order..." and end the round without making any modifications.
When processing a trade, do the following:
"BUY"
Increase the number of owned shares by the amount purchased.
Decrease the number of available shares by the amount purchased.
Increase the stock price by adding (1.5 * the number of stock sold) to the existing price.
"SELL"
Decrease the number of owned shares by the amount sold.
Increase the number of available shares by the amount sold.
Decrease the stock price by subtracting (2.0 * the number of stock sold) from the existing price.
Whenever an order is processed, the input is valid, and sufficient shares are available, increment the trade number.
That's it! Repeat for as many moves as are included in the file.
Testing
No local test cases are provided with this assignment. However, we provide the following main method and sample test files as examples:

Main Method

try {
StockGame[] stockTraders = {new StockGame("Xander", "TraderOneMoves.txt"),
new StockGame("Afua", "TraderTwoMoves.txt")};

for (int i = 0; i < stockTraders.length; i++) {
stockTraders[i].start();
}
for (int i = 0; i < stockTraders.length; i++) {
stockTraders[i].join();
}
} catch (Exception ex) {
ex.printStackTrace();
return;
}



TraderOneMoves.txt
BUY,5
SELL,3
BUY,16
BUY,4
SELL,2


TraderTwoMoves.txt
BUY,8
BUY,10
BUY,7
SELL,9



Testing Recommendations

Create additional threads with new files.
Change the file contents, experiment with 1 or more lines in each.
Verify that your solution is actually concurrent. It should not process each user in order or rotate between them in a pre-determined manner.
Notes:
Files may contain any number of moves. Users may have different numbers of moves.
If a move is not one of the valid options, print "Error, invalid input!" and do not modify anything for that round.
You do not need to include a main method in your submission, but you should use one during testing.
You should follow the requirements documented in the Debugging handout for preventing race conditions. Solutions that use excessively broad scope are not considered concurrent and will not receive credit.
Remember the difference between static and non-static. As there will be multiple StockGame objects, you will need to find a way to implement concurrency among all of them. If your solution does not implement concurrency properly for multiple objects, it will not receive credit.

What I have tried:

Can you please provide a solution ?
Posted
Updated 30-Oct-22 5:44am

Quote:
Can you please provide a solution ?
Sorry, no. This site is not here to do other people's work for them. It is here to help resolve problems with the code that you write. If we did everyone else's work for them then a) we would never have time for our own, and b) all those people would never learn anything, and would find it impossible to gain future employment.
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900