2020 - Add a new event called food scandal, this causes the reputation to fall 20 points

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

The Current System

This is the current method (in the Simulation class) which generates events within the simulation.

private void DisplayEventsAtDayEnd()
        {
            Console.WriteLine("\n***********************");
            Console.WriteLine("*****   Events:   *****");
            Console.WriteLine("***********************\n");
            double eventRanNo;
            eventRanNo = rnd.NextDouble();
            if (eventRanNo < 0.25)
            {
                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.25)
                {
                    ProcessAddHouseholdsEvent();
                }
                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.5)
                {
                    ProcessCostOfFuelChangeEvent();
                }
                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.5)
                {
                    ProcessReputationChangeEvent();
                }
                eventRanNo = rnd.NextDouble();
                if (eventRanNo >= 0.5)
                {
                    ProcessCostChangeEvent();
                }
            }
            else
            {
                Console.WriteLine("No events.");
            }
        }

The nested if statements all calculate a probability and run a method coded to produce the event in question. This page will show you how to create a new event for a food scandal.

Add a New Probability

In the code above, copy this code:

                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.5)
                {
                    ProcessReputationChangeEvent();
                }

Paste it underneath this code so you have:

                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.5)
                {
                    ProcessReputationChangeEvent();
                }
                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.5)
                {
                    ProcessReputationChangeEvent();
                }

We are going to edit one of them for our food scandal. Change the decimal from '0.5' to '0.25' for the one you are changing, and change the name of the method run to 'ProcessFoodScandalEvent':

                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.5)
                {
                    ProcessReputationChangeEvent();
                }
                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.25)
                {
                    ProcessFoodScandalEvent();
                }

Now find the 'ProcessReputationChangeEvent' method, you should have:

        private void ProcessReputationChangeEvent()
        {
            double reputationChange = rnd.Next(1, 10) / 10.0;
            int upOrDown = rnd.Next(0, 2);
            int companyNo = rnd.Next(0, companies.Count);
            if (upOrDown == 0)
            {
                Console.WriteLine("The reputation of " + companies[companyNo].GetName() + " has gone up by " + reputationChange.ToString());
            }
            else
            {
                Console.WriteLine("The reputation of " + companies[companyNo].GetName() + " has gone down by " + reputationChange.ToString());
                reputationChange *= -1;
            }
            companies[companyNo].AlterReputation(reputationChange);
        }

Copy this and paste it underneath, and change the name to ProcessFoodScandalEvent:

        private void ProcessFoodScandalEvent()
        {
            double reputationChange = rnd.Next(1, 10) / 10.0;
            int upOrDown = rnd.Next(0, 2);
            int companyNo = rnd.Next(0, companies.Count);
            if (upOrDown == 0)
            {
                Console.WriteLine("The reputation of " + companies[companyNo].GetName() + " has gone up by " + reputationChange.ToString());
            }
            else
            {
                Console.WriteLine("The reputation of " + companies[companyNo].GetName() + " has gone down by " + reputationChange.ToString());
                reputationChange *= -1;
            }
            companies[companyNo].AlterReputation(reputationChange);
        }

We are going to simplify the code, because it currently allows the reputation to go up or down. But in a food scandal the reputation can only go down, so edit it to get just:

private void ProcessFoodScandalEvent()
{
    double reputationChange = 0.2;
    int companyNo = rnd.Next(0, companies.Count);
    Console.WriteLine("The reputation of " + companies[companyNo].GetName() + " has gone down by " + reputationChange.ToString() + " due to a food scandal");
    reputationChange *= -1;

    companies[companyNo].AlterReputation(reputationChange * companies[companyNo].getReputationScore() );
}