In this lab assignment, you create an application that models the ups and downs of a particular stock. The value of a stock is assumed to change by plus or minus a specified number of units after every time unit (such as one hour). A notification is generated each time a stock changes more than a specified number of units above or below its initial value. A collection of brokers who control the stock must receive this notification. The range within which the stock can change every time unit and the threshold above or below which collection of brokers who control the stock must be notified are specified when the stock is created (using its constructor). You shall design and implement a C# application that satisfies the specification given above. This application involves .NET EventHandler events, threads, and text file processin

In this lab assignment, you create an application that models the ups and downs of a particular stock. The value of a stock is assumed to change by plus or minus a specified number of units after every time unit (such as one hour). A notification is generated each time a stock changes more than a specified number of units above or below its initial value. A collection of brokers who control the stock must receive this notification. The range within which the stock can change every time unit and the threshold above or below which collection of brokers who control the stock must be notified are specified when the stock is created (using its constructor).

You shall design and implement a C# application that satisfies the specification given above. This application involves .NET EventHandler events, threads, and text file processing.

When an event of this type is fired, the stock’s name, initial values, current values, and the number of changes in value can be sent to the listener . The sending information is sent to the console window and a text file. You are required to use built-in .NET EventHandler delegate in your solution.

Create the class Stock with the following attributes:

• Stock name
• Stock initial value
• Maximum change (the range within a stock can change every time unit)
• Notification threshold (the threshold above or below which the collection of brokers who control the stock must be notified)

You are required to implement other members in the class Stock that are needed. When a stock object is created, a thread is started. This thread causes the stock’s value to be modified every 500 milliseconds. If its value changes from its initial value by more than the specified notification threshold, an event method is invoked. This invokes the stock Event and multicasts a notification to all listeners who have registered with stock Event.

Create the class StockBroker which has fields broker name and stocks, a List of Stock. This latter field is not used in this application but could be used to obtain the stocks currently controlled by a given broker. The addStock method registers the Notify listerner with the stock (in addition to adding it to the list of stocks held by the broker). This Notify method outputs to the console the name, value, and the number of changes of the stock whose value is out of the range given the stock’s notification threshold.

The listing below presents a main driver class, StockApplication.

static void Main(string[] args)
{
Stock stock1 = new Stock(“Technology”,160, 5, 15);
Stock stock2 = new Stock(“Retail”,30, 2, 6);
Stock stock3 = new Stock(“Banking”, 90, 4, 10);
Stock stock4 = new Stock(“Commodity”, 500, 20, 50);

StockBroker b1 = new StockBroker(“Broker 1”);
b1.AddStock(stock1);
b1.AddStock(stock2);

StockBroker b2 = new StockBroker(“Broker 2”);
b2.AddStock(stock1);
b2.AddStock(stock3);
b2.AddStock(stock4);

StockBroker b3 = new StockBroker(“Broker 3”);
b3.AddStock(stock1);
b3.AddStock(stock3);

StockBroker b4 = new StockBroker(“Broker 4”);
b4.AddStock(stock1);
b4.AddStock(stock2);

b4.AddStock(stock3);
b4.AddStock(stock4);

}

The output sample (All the column must be left alignment)l:

Broker Stock Value Changes Date and Time
Broker 2 Banking 102 5 2/2/2022 1:23:25 PM
Broker 3 Banking 102 5 2/2/2022 1:23:25 PM
Broker 4 Banking 102 5 2/2/2022 1:23:25 PM
Broker 2 Commodity 559 5 2/2/2022 1:23:25 PM
Broker 4 Commodity 559 5 2/2/2022 1:23:25 PM
Broker 1 Technology 176 6 2/2/2022 1:23:25 PM
Broker 2 Technology 176 6 2/2/2022 1:23:25 PM
Broker 3 Technology 176 6 2/2/2022 1:23:25 PM
Broker 4 Technology 176 6 2/2/2022 1:23:25 PM
Broker 2 Commodity 562 6 2/2/2022 1:23:25 PM
Broker 4 Commodity 562 6 2/2/2022 1:23:25 PM
Broker 2 Banking 104 6 2/2/2022 1:23:25 PM
Broker 3 Banking 104 6 2/2/2022 1:23:25 PM
Broker 4 Banking 104 6 2/2/2022 1:23:25 PM

Broker 1 Retail 37 7 2/2/2022 1:23:26 PM
Broker 4 Retail 37 7 2/2/2022 1:23:26 PM
Broker 1 Technology 179 7 2/2/2022 1:23:26 PM
Broker 2 Technology 179 7 2/2/2022 1:23:26 PM
Broker 3 Technology 179 7 2/2/2022 1:23:26 PM
Broker 4 Technology 179 7 2/2/2022 1:23:26 PM
Broker 2 Commodity 569 7 2/2/2022 1:23:26 PM
Broker 4 Commodity 569 7 2/2/2022 1:23:26 PM
Broker 2 Banking 105 7 2/2/2022 1:23:26 PM
Broker 3 Banking 105 7 2/2/2022 1:23:26 PM
Broker 4 Banking 105 7 2/2/2022 1:23:26 PM
Broker 1 Retail 38 8 2/2/2022 1:23:26 PM
Broker 4 Retail 38 8 2/2/2022 1:23:26 PM
Broker 1 Technology 182 8 2/2/2022 1:23:26 PM
Broker 2 Technology 182 8 2/2/2022 1:23:26 PM
Broker 3 Technology 182 8 2/2/2022 1:23:26 PM
Broker 4 Technology 182 8 2/2/2022 1:23:26 PM
Broker 2 Commodity 571 8 2/2/2022 1:23:26 PM
Broker 4 Commodity 571 8 2/2/2022 1:23:26 PM
Broker 2 Banking 107 8 2/2/2022 1:23:26 PM
Broker 3 Banking 107 8 2/2/2022 1:23:26 PM
Broker 4 Banking 107 8 2/2/2022 1:23:26 PM
Broker 1 Retail 39 9 2/2/2022 1:23:27 PM
Broker 4 Retail 39 9 2/2/2022 1:23:27 PM
Broker 1 Technology 185 9 2/2/2022 1:23:27 PM
Broker 2 Technology 185 9 2/2/2022 1:23:27 PM
Broker 3 Technology 185 9 2/2/2022 1:23:27 PM
Broker 4 Technology 185 9 2/2/2022 1:23:27 PM
Broker 2 Commodity 573 9 2/2/2022 1:23:27 PM
Broker 4 Commodity 573 9 2/2/2022 1:23:27 PM
Broker 2 Banking 109 9 2/2/2022 1:23:27 PM
Broker 3 Banking 109 9 2/2/2022 1:23:27 PM
Broker 4 Banking 109 9 2/2/2022 1:23:27 PM
Broker 1 Retail 40 10 2/2/2022 1:23:27 PM
Broker 4 Retail 40 10 2/2/2022 1:23:27 PM
Broker 1 Technology 188 10 2/2/2022 1:23:27 PM
Broker 2 Technology 188 10 2/2/2022 1:23:27 PM
Broker 3 Technology 188 10 2/2/2022 1:23:27 PM
Broker 4 Technology 188 10 2/2/2022 1:23:27 PM
Broker 2 Commodity 581 10 2/2/2022 1:23:27 PM
Broker 4 Commodity 581 10 2/2/2022 1:23:27 PM
Broker 2 Banking 111 10 2/2/2022 1:23:27 PM
Broker 3 Banking 111 10 2/2/2022 1:23:27 PM
Broker 4 Banking 111 10 2/2/2022 1:23:27 PM
Broker 1 Retail 41 11 2/2/2022 1:23:28 PM
Broker 4 Retail 41 11 2/2/2022 1:23:28 PM

Broker 2 Commodity 587 11 2/2/2022 1:23:28 PM
Broker 4 Commodity 587 11 2/2/2022 1:23:28 PM
Broker 1 Technology 189 11 2/2/2022 1:23:28 PM
Broker 2 Technology 189 11 2/2/2022 1:23:28 PM
Broker 3 Technology 189 11 2/2/2022 1:23:28 PM
Broker 4 Technology 189 11 2/2/2022 1:23:28 PM
Broker 2 Banking 114 11 2/2/2022 1:23:28 PM
Broker 3 Banking 114 11 2/2/2022 1:23:28 PM
Broker 4 Banking 114 11 2/2/2022 1:23:28 PM
Broker 1 Retail 42 12 2/2/2022 1:23:28 PM
Broker 4 Retail 42 12 2/2/2022 1:23:28 PM
Broker 2 Commodity 597 12 2/2/2022 1:23:28 PM
Broker 4 Commodity 597 12 2/2/2022 1:23:28 PM
Broker 1 Technology 192 12 2/2/2022 1:23:28 PM
Broker 2 Technology 192 12 2/2/2022 1:23:28 PM
Broker 3 Technology 192 12 2/2/2022 1:23:28 PM
Broker 4 Technology 192 12 2/2/2022 1:23:28 PM
Broker 2 Banking 116 12 2/2/2022 1:23:28 PM
Broker 3 Banking 116 12 2/2/2022 1:23:28 PM
Broker 4 Banking 116 12 2/2/2022 1:23:28 PM
Broker 1 Retail 43 13 2/2/2022 1:23:29 PM
Broker 4 Retail 43 13 2/2/2022 1:23:29 PM
Broker 2 Commodity 609 13 2/2/2022 1:23:29 PM
Broker 4 Commodity 609 13 2/2/2022 1:23:29 PM
Broker 1 Technology 196 13 2/2/2022 1:23:29 PM
Broker 2 Technology 196 13 2/2/2022 1:23:29 PM
Broker 3 Technology 196 13 2/2/2022 1:23:29 PM
Broker 4 Technology 196 13 2/2/2022 1:23:29 PM
Broker 2 Banking 117 13 2/2/2022 1:23:29 PM
Broker 3 Banking 117 13 2/2/2022 1:23:29 PM
Broker 4 Banking 117 13 2/2/2022 1:23:29 PM
Broker 1 Retail 44 14 2/2/2022 1:23:29 PM
Broker 4 Retail 44 14 2/2/2022 1:23:30 PM
Broker 1 Technology 200 14 2/2/2022 1:23:30 PM
Broker 2 Technology 200 14 2/2/2022 1:23:30 PM
Broker 3 Technology 200 14 2/2/2022 1:23:30 PM
Broker 4 Technology 200 14 2/2/2022 1:23:30 PM
Broker 2 Commodity 626 14 2/2/2022 1:23:30 PM
Broker 4 Commodity 626 14 2/2/2022 1:23:30 PM
Broker 2 Banking 120 14 2/2/2022 1:23:30 PM
Broker 3 Banking 120 14 2/2/2022 1:23:30 PM
Broker 4 Banking 120 14 2/2/2022 1:23:30 PM
Broker 1 Retail 45 15 2/2/2022 1:23:31 PM
Broker 4 Retail 45 15 2/2/2022 1:23:31 PM
Broker 2 Commodity 639 15 2/2/2022 1:23:31 PM
Broker 4 Commodity 639 15 2/2/2022 1:23:31 PM

Broker 1 Technology 203 15 2/2/2022 1:23:31 PM
Broker 2 Technology 203 15 2/2/2022 1:23:31 PM
Broker 3 Technology 203 15 2/2/2022 1:23:31 PM
Broker 4 Technology 203 15 2/2/2022 1:23:31 PM
Broker 2 Banking 122 15 2/2/2022 1:23:31 PM
Broker 3 Banking 122 15 2/2/2022 1:23:31 PM
Broker 4 Banking 122 15 2/2/2022 1:23:31 PM
Broker 1 Retail 46 16 2/2/2022 1:23:31 PM
Broker 4 Retail 46 16 2/2/2022 1:23:31 PM
Broker 2 Commodity 651 16 2/2/2022 1:23:31 PM
Broker 4 Commodity 651 16 2/2/2022 1:23:31 PM
Broker 1 Technology 206 16 2/2/2022 1:23:31 PM
Broker 2 Technology 206 16 2/2/2022 1:23:31 PM
Broker 3 Technology 206 16 2/2/2022 1:23:31 PM
Broker 4 Technology 206 16 2/2/2022 1:23:31 PM
Broker 2 Banking 123 16 2/2/2022 1:23:31 PM
Broker 3 Banking 123 16 2/2/2022 1:23:31 PM
Broker 4 Banking 123 16 2/2/2022 1:23:31 PM
Broker 2 Commodity 663 17 2/2/2022 1:23:32 PM
Broker 4 Commodity 663 17 2/2/2022 1:23:40 PM
Broker 1 Technology 210 17 2/2/2022 1:23:40 PM
Broker 2 Technology 210 17 2/2/2022 1:23:40 PM
Broker 3 Technology 210 17 2/2/2022 1:23:40 PM
Broker 4 Technology 210 17 2/2/2022 1:23:40 PM
Broker 1 Retail 47 17 2/2/2022 1:23:40 PM
Broker 4 Retail 47 17 2/2/2022 1:23:40 PM
Broker 2 Banking 124 17 2/2/2022 1:23:40 PM
Broker 3 Banking 124 17 2/2/2022 1:23:40 PM
Broker 4 Banking 124 17 2/2/2022 1:23:40 PM
Broker 1 Technology 213 18 2/2/2022 1:23:40 PM
Broker 2 Technology 213 18 2/2/2022 1:23:40 PM
Broker 3 Technology 213 18 2/2/2022 1:23:40 PM
Broker 4 Technology 213 18 2/2/2022 1:23:40 PM
Broker 1 Retail 48 18 2/2/2022 1:23:40 PM
Broker 4 Retail 48 18 2/2/2022 1:23:40 PM
Broker 2 Banking 125 18 2/2/2022 1:23:40 PM
Broker 3 Banking 125 18 2/2/2022 1:23:40 PM
Broker 4 Banking 125 18 2/2/2022 1:23:40 PM
Broker 2 Commodity 668 18 2/2/2022 1:23:40 PM
Broker 4 Commodity 668 18 2/2/2022 1:23:40 PM
Broker 1 Technology 215 19 2/2/2022 1:23:41 PM
Broker 2 Technology 215 19 2/2/2022 1:23:41 PM
Broker 3 Technology 215 19 2/2/2022 1:23:41 PM
Broker 4 Technology 215 19 2/2/2022 1:23:41 PM
Broker 2 Commodity 682 19 2/2/2022 1:23:41 PM
Broker 4 Commodity 682 19 2/2/2022 1:23:41 PM

Broker 2 Banking 126 19 2/2/2022 1:23:41 PM
Broker 3 Banking 126 19 2/2/2022 1:23:41 PM
Broker 4 Banking 126 19 2/2/2022 1:23:41 PM
Broker 1 Retail 49 19 2/2/2022 1:23:41 PM
Broker 4 Retail 49 19 2/2/2022 1:23:41 PM
Broker 1 Technology 217 20 2/2/2022 1:23:41 PM
Broker 2 Technology 217 20 2/2/2022 1:23:41 PM
Broker 3 Technology 217 20 2/2/2022 1:23:41 PM
Broker 4 Technology 217 20 2/2/2022 1:23:41 PM
Broker 2 Commodity 689 20 2/2/2022 1:23:41 PM
Broker 4 Commodity 689 20 2/2/2022 1:23:41 PM
Broker 2 Banking 129 20 2/2/2022 1:23:41 PM
Broker 3 Banking 129 20 2/2/2022 1:23:41 PM
Broker 4 Banking 129 20 2/2/2022 1:23:41 PM
Broker 1 Retail 50 20 2/2/2022 1:23:41 PM
Broker 4 Retail 50 20 2/2/2022 1:23:41 PM
Broker 1 Technology 221 21 2/2/2022 1:23:42 PM
Broker 2 Technology 221 21 2/2/2022 1:23:42 PM
Broker 3 Technology 221 21 2/2/2022 1:23:42 PM
Broker 4 Technology 221 21 2/2/2022 1:23:42 PM
Broker 2 Commodity 692 21 2/2/2022 1:23:42 PM
Broker 4 Commodity 692 21 2/2/2022 1:23:42 PM
Broker 2 Banking 131 21 2/2/2022 1:23:42 PM
Broker 3 Banking 131 21 2/2/2022 1:23:42 PM
Broker 4 Banking 131 21 2/2/2022 1:23:42 PM
Broker 1 Retail 51 21 2/2/2022 1:23:42 PM
Broker 4 Retail 51 21 2/2/2022 1:23:42 PM
Broker 1 Technology 223 22 2/2/2022 1:23:42 PM
Broker 2 Technology 223 22 2/2/2022 1:23:42 PM
Broker 3 Technology 223 22 2/2/2022 1:23:42 PM
Broker 4 Technology 223 22 2/2/2022 1:23:42 PM
Broker 2 Commodity 700 22 2/2/2022 1:23:42 PM
Broker 4 Commodity 700 22 2/2/2022 1:23:42 PM
Broker 1 Retail 52 22 2/2/2022 1:23:42 PM
Broker 4 Retail 52 22 2/2/2022 1:23:42 PM
Broker 2 Banking 134 22 2/2/2022 1:23:42 PM
Broker 3 Banking 134 22 2/2/2022 1:23:42 PM
Broker 4 Banking 134 22 2/2/2022 1:23:42 PM
Broker 1 Technology 225 23 2/2/2022 1:23:43 PM
Broker 2 Technology 225 23 2/2/2022 1:23:43 PM
Broker 3 Technology 225 23 2/2/2022 1:23:43 PM
Broker 4 Technology 225 23 2/2/2022 1:23:43 PM
Broker 2 Commodity 701 23 2/2/2022 1:23:43 PM
Broker 4 Commodity 701 23 2/2/2022 1:23:43 PM
Broker 1 Retail 53 23 2/2/2022 1:23:43 PM
Broker 4 Retail 53 23 2/2/2022 1:23:43 PM

Broker 2 Banking 135 23 2/2/2022 1:23:43 PM
Broker 3 Banking 135 23 2/2/2022 1:23:43 PM
Broker 4 Banking 135 23 2/2/2022 1:23:43 PM
Broker 1 Technology 229 24 2/2/2022 1:23:43 PM
Broker 2 Technology 229 24 2/2/2022 1:23:46 PM
Broker 3 Technology 229 24 2/2/2022 1:23:46 PM
Broker 4 Technology 229 24 2/2/2022 1:23:46 PM
Broker 2 Commodity 715 24 2/2/2022 1:23:46 PM
Broker 4 Commodity 715 24 2/2/2022 1:23:46 PM
Broker 2 Banking 137 24 2/2/2022 1:23:46 PM
Broker 3 Banking 137 24 2/2/2022 1:23:46 PM
Broker 4 Banking 137 24 2/2/2022 1:23:46 PM
Broker 1 Retail 54 24 2/2/2022 1:23:46 PM
Broker 4 Retail 54 24 2/2/2022 1:23:46 PM
Broker 2 Banking 140 25 2/2/2022 1:23:46 PM
Broker 3 Banking 140 25 2/2/2022 1:23:46 PM
Broker 4 Banking 140 25 2/2/2022 1:23:46 PM
Broker 1 Technology 233 25 2/2/2022 1:23:46 PM
Broker 2 Technology 233 25 2/2/2022 1:23:46 PM
Broker 3 Technology 233 25 2/2/2022 1:23:46 PM
Broker 4 Technology 233 25 2/2/2022 1:23:46 PM
Broker 2 Commodity 729 25 2/2/2022 1:23:46 PM
Broker 4 Commodity 729 25 2/2/2022 1:23:46 PM
Broker 1 Retail 55 25 2/2/2022 1:23:46 PM
Broker 4 Retail 55 25 2/2/2022 1:23:46 PM

//stock.cs
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Stock
{

//———————————————————————————–

public class Stock
{

public event EventHandler StockEvent; //Name of our stock.
private string _name;
//Starting value of the stock.
private int _initialValue;
//Max change of the stock that is possible.
private int _maxChange;
//Threshold value where we notify subscribers to the event.
private int _threshold;
//Amount of changes the stock goes through.
private int _numChanges;
//Current value of the stock.
private int _currentValue;
private readonly Thread _thread;
public string StockName { get => _name; set => _name = value; } public int InitialValue

public int CurrentValue
public int MaxChange
public int Threshold
public int NumChanges
//—————————————————————————–
///

/// Stock class that contains all the information and changes of the stock
///

/// Stock name
/// Starting stock value
/// The max value change of the stock
/// The range for the stock
public Stock(string name, int startingValue, int maxChange, int threshold)
{
_name = name;
_initialValue = startingValue;
_currentValue = InitialValue;
_maxChange = maxChange;
_threshold = threshold;
this._thread = new Thread(new ThreadStart(______________________));
_thread.________________;
}
//———————————————————————————–

///

/// Activates the threads synchronizations
///

public void Activate()
{
for (int i = 0; i
/// Changes the stock value and also raising the event of stock value changes
///

public void ChangeStockValue()
{
var rand = new Random();
CurrentValue += rand.Next(1, MaxChange);
NumChanges++;
if ((CurrentValue – InitialValue) > Threshold)
{ //RAISE THE EVENT
_____ Invoke _____________________________________________________
}
}
//——————————————————————————————
——
}
}
//stockbroker.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace Stock
{
//!NOTE!: Class StockBroker has fields broker name and a list of Stock named stocks. // addStock method registers the Notify listener with the stock (in addition to

// adding it to the lsit of stocks held by the broker). This notify method outputs
// to the console the name, value, and the number of changes of the stock whose
// value is out of the range given the stock’s notification threshold.
public class StockBroker
{
public string BrokerName { get; set; }
public List stocks = new List();
public static ReaderWriterLockSlim myLock = new ReaderWriterLockSlim(); //readonly string docPath = @”C:UsersDocumentsCECS 475Lab3_output.txt”; readonly string destPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “Lab1_output.txt”);
public string titles = “Broker”.PadRight(10) + “Stock”.PadRight(15) + “Value”.PadRight(10) + “Changes”.PadRight(10) + “Date and Time”;

//——————————————————————————————
————————
///

/// The stockbroker object
///

/// The stockbroker’s name public StockBroker(string brokerName)
{
BrokerName = brokerName;
}
//——————————————————————————————
————————
///

/// Adds stock objects to the stock list
///

/// Stock object
public void AddStock(Stock stock)
{
stocks._____________________________
stock.____________________________________
}
//——————————————————————————————
—————————
///

/// The eventhandler that raises the event of a change
///

/// The sender that indicated a change
/// Event arguments
void EventHandler(Object sender, EventArgs e)
{
try
{ //LOCK Mechanism
______________________________
Stock newStock = (Stock)sender; //string statement;
//!NOTE!: Check out C#events, pg.4
// Display the output to the console windows

Console.WriteLine(BrokerName.PadRight(16) ______________________________________________);
//Display the output to the file
using (StreamWriter outputFile =________________________________________________)
{
________________________________________________
}
//RELEASE the lock
____________________
}
finally
{
}
}
//——————————————————————————————
—————————-
}
}
Stocknotification.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Stock
{
public class StockNotification : EventArgs
{
public string StockName { get; set; }
public int CurrentValue { get; set; }
public int NumChanges { get; set; }
///

/// Stock notification attributes that are set and changed
///

/// Name of stock
/// Current vallue of the stock
/// Number of changes the stock goes through public StockNotification(string stockName, int currentValue, int numChanges)
{
// !NOTE!: Fill in below of what the notification will do using the comments above this.StockName = stockName;
this.CurrentValue = currentValue; this.NumChanges = numChanges;
}
}
}
Extras:

string titles = “Broker”.PadRight(16) + “Stock”.PadRight(16) + “Value”.PadRight(16) + “Changes”.PadRight(10) + “Date and Time”; Console.WriteLine(titles);

string destPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “Lab1_output.txt”); using (StreamWriter outputFile = new StreamWriter(destPath, false))

Need Help With Your Assignment Projects?

X
× How can I help you?