Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Assignment #2

CSCI 201 Spring 2024

Title

JoesStocks

Topics Covered

Locks Programming

Semaphore Programming

Multi-Threaded Programming Design

Introduction

Introducing the ground-breaking, original, non-USC backed, nor affiliated start-up, JoesStocks! JoesStocks aims to hire stockbrokers to complete stock trades between USC and various companies. As the Executive Officer of JoesStocks, I am here to inform you that the Council of SAL has decided to commission all Spring 2024 CSCI 201 students to design a cutting-edge system that supports the scheduling of stockbrokers and trades.

Assignment

In this assignment, you will first read in a JSON file containing various information regarding public companies and their stock. The JSON file format is generally the same as the one we used in the previous assignment, so feel free to reuse the parser you've built. However, you will need to alter the classes, as we have modified the JSON object, by adding an additional name/value pair. Here's a sample JSON:

{

"data": [

{

"name": "Tesla Inc",

"ticker": "TSLA",

"startDate": "2010-06-29",

"stockBrokers": 3,

"description": "Tesla Motors, Inc. designs, develops,

manufactures and sells electric vehicles and advanced electric vehicle powertrain components.",

"exchangeCode": "NASDAQ"

},

{

"name": "Apple Inc",

"ticker": "AAPL",

"startDate": "1980-12-12",

"stockBrokers": 1,

"description": "Apple Inc. designs, manufactures and markets

mobile communication and media devices, personal computers, and

portable digital music players, and a variety of related software,

services, peripherals, networking solutions, and third-party digital content and applications.",

"exchangeCode": "NASDAQ"

},

{

"name": "Microsoft Corporation",

"ticker": "MSFT",

"startDate": "1986-03-13",

"stockBrokers": 2,

"description": "Microsoft enables digital transformation for the era of an intelligent cloud and an intelligent edge. Its mission is to empower every person and every organization on the planet to achieve

more.",

"exchangeCode": "NASDAQ"

},

{

"name": "Advanced Micro Devices",

"ticker": "AMD",

"startDate": "1972-12-29",

"stockBrokers": 2,

"description": "Advanced Micro Devices, Inc. engages in the provision of semiconductor businesses. It operates through the

following segments: Computing \u0026 Graphics, and Enterprise, Embedded and Semi-Custom.",

"exchangeCode": "NASDAQ"

}

]

}

Aside from the JSON file, you will need to read in a second file, in CSV format.

Below is a layout of the second file, which contains information for the stock trades:

0,AAPL,2,191

0,MSFT,5,398

1,TSLA,-3,212

5,AAPL,-1,192

18,TSLA,10,213

18,MSFT,-1,397

20,AMD,3,174

25,MSFT,3,398

On each line of the CSV file, you will have the following fields:

●   The first field indicates when the stock trade is to be initiated, measured in seconds from the start of trading.

●   The second field indicates the ticker which corresponds to the public company whose stocks are being bought or sold.

●   The third field indicates how many stocks are being bought or sold, where a positive number indicates a buy transaction, and a negative number indicates a sell transaction.

●   The fourth field indicates the price for the corresponding stock.

●   Notice that in both the JSON and the CSV files, the last character/entry is not followed by a “newline.”

It takes each stockbroker 2 seconds for any BUY transaction and 3 seconds for any SELL transaction. You can assume that each individual stockbroker will only carry out trades for a single company. The number of stockbrokers “allocated” to  each company to trade exclusively their stock will be listed in the assignment2.json file, as the additional value of the stockBrokers name.

Stockbrokers that facilitate trading in a specific stock are called “market makers” in the finance industry.

You will be prompting the user for the initial account balance to carry out the transactions. The account balance needs to be updated after every BUY and SELL transaction and printed after the completion of transaction. When a stock is sold, its cash is added to the balance and when a stock is bought its net total is deducted from the account balance. If a BUY transaction costs more than available account balance, the transaction should FAIL, and an appropriate message should be displayed.

Just like the previous assignment, you should also prompt the user to enter a filename and check for the file's existence and validity when the program initially runs. You will need to utilize locks and conditions to settle the availability of the stockbrokers. For example, one stockbroker cannot make two trades at the same time. The first trade must be completed before starting the second trade. The program should output when a trade is initiated and when it is completed.

Example output with timestamps bolded for clarity (you do not have to bold the timestamps for your execution of the program):

Case 1:

What is the name of the file containing the company information?

assignment2.json

The company file has been properly read.

What is the name of the file containing the schedule information?

schedule.csv

The schedule file has been properly read .

What is the initial balance?

5000

Starting execution of program...

Initial Balance: 5000

[00:00:00.000] Starting purchase of 5 stocks of MSFT

[00:00:00.000] Starting purchase of 2 stocks of AAPL

[00:00:00.980] Starting sale of 3 stocks of TSLA

[00:00:02.003] Finished purchase of 5 stocks of MSFT

Current Balance after trade: 3010

[00:00:02.004] Finished purchase of 2 stocks of AAPL

Current Balance after trade: 2628

[00:00:03.988] Finished sale of 3 stocks of TSLA

Current Balance after trade: 3264

[00:00:05.005] Starting sale of 1 stocks of AAPL

[00:00:08.013] Finished sale of 1 stocks of AAPL

Current Balance after trade: 3456

[00:00:18.104] Starting purchase of 10 stocks of TSLA

[00:00:18.104] Starting sale of 1 stocks of MSFT

[00:00:20.105] Finished purchase of 10 stocks of TSLA

Current Balance after trade: 1326

[00:00:20.117] Starting purchase of 3 stocks of AMD

[00:00:21.114] Finished sale of 1 stocks of MSFT

Current Balance after trade: 1723

[00:00:22.131] Finished purchase of 3 stocks of AMD

Current Balance after trade: 1201

[00:00:25.160] Starting purchase of 3 stocks of MSFT

[00:00:27.161] Finished purchase of 3 stocks of MSFT

Current Balance after trade: 7

All trades completed!

Case 2:

What is the name of the file containing the company information?

assignment2.json

The company file has been properly read.

What is the name of the file containing the schedule information?

schedule.csv

The schedule file has been properly read .

What is the initial balance?

2000

Starting execution of program...

Initial Balance: 2000

[00:00:00.000] Starting purchase of 2 stocks of AAPL

[00:00:00.000] Starting purchase of 5 stocks of MSFT

[00:00:00.985] Starting sale of 3 stocks of TSLA

Transaction failed due to insufficient balance. Unsuccessful purchase of 5 stocks of MSFT

[00:00:02.006] Finished purchase of 2 stocks of AAPL

Current Balance after trade: 1618

[00:00:03.989] Finished sale of 3 stocks of TSLA

Current Balance after trade: 2254

[00:00:05.000] Starting sale of 1 stocks of AAPL

[00:00:08.004] Finished sale of 1 stocks of AAPL

Current Balance after trade: 2446

[00:00:18.047] Starting sale of 1 stocks of MSFT

[00:00:18.047] Starting purchase of 10 stocks of TSLA

[00:00:20.048] Finished purchase of 10 stocks of TSLA

Current Balance after trade: 316

[00:00:20.053] Starting purchase of 3 stocks of AMD

[00:00:21.052] Finished sale of 1 stocks of MSFT

Current Balance after trade: 713

[00:00:22.054] Finished purchase of 3 stocks of AMD

Current Balance after trade: 191

[00:00:25.064] Starting purchase of 3 stocks of MSFT

Transaction failed due to insufficient balance. Unsuccessful purchase of 3 stocks of MSFT

All trades completed

Case 2 (2nd  possible output):

What is the name of the file containing the company information?

assignment2.json

The company file has been properly read.

What is the name of the file containing the schedule information?

schedule.csv

The schedule file has been properly read .

What is the initial balance?

2000

Starting execution of program...

Starting execution of program...

Initial Balance: 2000

[00:00:00.000] Starting purchase of 5 stocks of MSFT

[00:00:00.000] Starting purchase of 2 stocks of AAPL

[00:00:00.980] Starting sale of 3 stocks of TSLA

[00:00:02.004] Finished purchase of 5 stocks of MSFT

Current Balance after trade: 10

Transaction failed due to insufficient balance. Unsuccessful purchase of 2 stocks of AAPL

[00:00:03.994] Finished sale of 3 stocks of TSLA

Current Balance after trade: 646

[00:00:04.999] Starting sale of 1 stocks of AAPL

[00:00:08.010] Finished sale of 1 stocks of AAPL

Current Balance after trade: 838

[00:00:18.085] Starting sale of 1 stocks of MSFT

[00:00:18.085] Starting purchase of 10 stocks of TSLA

Transaction failed due to insufficient balance. Unsuccessful purchase of 10 stocks of TSLA

[00:00:20.094] Starting purchase of 3 stocks of AMD

[00:00:21.085] Finished sale of 1 stocks of MSFT

Current Balance after trade: 1235

[00:00:22.102] Finished purchase of 3 stocks of AMD

Current Balance after trade: 713

[00:00:25.132] Starting purchase of 3 stocks of MSFT

Transaction failed due to insufficient balance. Unsuccessful purchase

of 3 stocks of MSFT

All trades completed!

Grading Criteria

You will be graded based on the correctness of scheduling as well as the order and duration of trades. The maximum number of points earned is 5.

Note: If the program crashes or does not terminate at any point, -0.5 will be deducted.

File I/O (1.0)

0.25 - JSON File I/O

0.25 - CSV File I/O

0.50 - Checking for invalid user inputs.

JoesStocks Program Execution (4)

1.50 - Trade start/completion print statements

2.50 - Semaphores and/or locks implementation