Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# PROGRAM In this lab assignment , you create an application that models the up

ID: 3753084 • Letter: C

Question

C# PROGRAM

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 of selling stock is generated each time a stock changes more than a specified number of units above or below its intial value. A collection of customers who own 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 customers who own the stocks 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 delegates, events and threads. You begin by defining a delegate class StockNotification. This is shown below.

public delegate void StockNotification(String stockName, currentValue, initial value, stock quantity);

This delegate is designed so that when an event of this type is fired, the stock's name, current value, initial value, and stock quantity can be sent to the listener.

You are required to use Func or Action C# delegate.

Create the class Stock with the following attributes:

Stock name

Stock intial value

Stock quantity

Maximum change (the range within a stock can change every time unit)

Notification threshold (the threshold above or below which the collection of customers who own 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 stockEvent (of event-type StockNotification) and multicasts a notification to all listeners who have registered with stockEvent.

Create another event to notify saving the following information to a file when the stock's threshold is reached: date and time, stock name, inital value, current value, and amount of loss or gain.

Create the class StockCustomer which has fields customer 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 owned by a given customer. The addStock method registers the Notify listerner with the stock (in addition to adding it to the list of stocks held by the customer). This Notify method outputs to the console the customer name, the intial value, and the current value, amount of loss or gain. .

Explanation / Answer

ANSWER :

StockApplication.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment3
{
    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);
        }
    }
}

Stock.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Assignment3
{
    public class ThresholdReachedEventargs : EventArgs
    {
        public string stockName { get; set; }
        public int currentValue { get; set; }
        public int numberChanges { get; set; }
    }

    public class FileReachedEventargs : EventArgs
    {
        public DateTime dateAndTime { get; set; }
        public string stockName { get; set; }
        public int initialValue { get; set; }
        public int currentValue { get; set;}
    }

    class Stock
    {
        public string name;
        public int initialValue; //stock price
        public int maxChange; //the range within a stock can change every time unit
        public int notificationThreshold; //the value to measure the change of the stock for raising the event

        public int currentValue;
        public int noofchanges=0;//number of changes in value can be sent to the listener.

        Thread thread = Thread.CurrentThread;
      

        public Stock(string name, int startingValue, int maxchange, int threshold)
        {
            this.name = name;
            this.initialValue = startingValue;
            this.currentValue = initialValue;
            this.maxChange = maxchange;   //currentValue = initialValue
            this.notificationThreshold = threshold;
            thread = new Thread(new ThreadStart(Activate));
        }

        public void Activate()
        {
            for (;;)
            {
                //This thread causes the stock's value to be modified every 500 milliseconds.
                Thread.Sleep(500);
                ChangeStockValue();
            }
        }
        public void ChangeStockValue()
        {
            Random random = new Random();
            currentValue +=random.Next(1,maxChange);
            noofchanges++;

            if((currentValue-initialValue)>notificationThreshold)
            {
                ThresholdReachedEventargs args = new ThresholdReachedEventargs();
                //create event Data
                args.stockName = name;
                args.currentValue = currentValue;
                args.numberChanges = noofchanges;

                onStockChangeReached(args);
            }
        }

        public delegate void StockNotification(String stockName, int currentValue, int numberChanges);


        public event StockNotification StockThresholdReachedEvent;

        protected virtual void onStockChangeReached(ThresholdReachedEventargs e)
        {
            /* StockNotification handler = StockThresholdReachedEvent;
             if (handler != null)
                 handler(this, e);*/
            StockThresholdReachedEvent?.DynamicInvoke(this, e);
        }

     

        //another event to notify saving the following information to a file
        //when the stock's threshold is reached: date and time, stock name, inital value and current value

        public delegate void FileDelegate(Object sender ,EventArgs e);

        public event FileDelegate FileEventHandler;

        protected virtual void OnStockChangeFileCreated(FileReachedEventargs e)
        {
            FileDelegate filehandler = FileEventHandler;
            if (filehandler != null)
            {
                filehandler(this, e);
            }

        }

        public class ThresholdReachedEventargs : EventArgs
        {
            public string stockName { get; set; }
            public int currentValue { get; set; }
            public int numberChanges { get; set; }
        }


    }
}

StockBroker.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment3
{
    class StockBroker
    {
        public string brokerName;
        List<Stock> stocks = new List<Stock>();

        public StockBroker(string name)
        {
            this.brokerName = name;
        }

        public event BrokerDelegateHandler BrokerEventHandler;
      
        public virtual void onBrokerEventChanged()
        {
            BrokerDelegateHandler handler = BrokerEventHandler;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        public void Notify()
        {
            Console.WriteLine("{0} {1} {2} {3}", brokerName);
        }
        public void AddStock(Stock s)
        {
            //add stocks to the list
            stocks.Add(s);

            //call stock event
            s.StockThresholdReachedEvent += StockNotification();
            Console.WriteLine("Broker Stock Value Changes ");
        }

        private Stock.StockNotification StockNotification()
        {
            throw new NotImplementedException();
        }

        public delegate void BrokerDelegateHandler(Object sender, EventArgs e);
    }


}