2020 - Process multiple days in one go, ie process a week

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

The Issue

The current program can only process one day at a time, and you might want to run the simulation for a number of days in one go.

The Solution

The current 'Run' method is what gets the user input and runs the appropriate method in response. We need to create the repetition structure within the run to automatically run 'ProcessDayEnd' for 7 days.

Example

Firstly, we will need a new integer variable to count the number of days we have processed in one go. So declare the following at the start of the 'Run' method:

int week = 0;

Next, inside the while loop create the following if statement:

if (week != 0)
{
   Console.WriteLine("Process day " + week);
   ProcessDayEnd();
   week++;
   if (week > 7)
       week = 0;
}
else
{
                    
}

Now copy the existing code into the new else statement. I have added an entry into the switch case statement for number 7, and all you need to do is set week to 1. You should get the following final 'Run' method:

public void Run()
        {
            string choice = "";
            int index;
            int week = 0;
            while (choice != "Q")
            {
                if (week != 0)
                {
                    Console.WriteLine("Process day " + week);
                    ProcessDayEnd();
                    week++;
                    if (week > 7)
                        week = 0;
                }
                else
                {
                    DisplayMenu();
                    choice = Console.ReadLine();
                    switch (choice)
                    {
                        case "1":
                            simulationSettlement.DisplayHouseholds();
                            break;
                        case "2":
                            DisplayCompanies();
                            break;
                        case "3":
                            string companyName;
                            index = -1;
                            while (index == -1)
                            {
                                Console.Write("Enter company name: ");
                                companyName = Console.ReadLine();
                                index = GetIndexOfCompany(companyName);
                            }
                            ModifyCompany(index);
                            break;
                        case "4":
                            AddCompany();
                            break;
                        case "6":
                            ProcessDayEnd();
                            break;
                        case "7":
                            week = 1;
                            break;
                        case "Q":
                            Console.WriteLine("Simulation finished, press Enter to close.");
                            Console.ReadLine();
                            break;
                    }
                }
            }
        }
    }

Finally we need to add a menu option for the Process Week function, so find the 'DisplayMenu' method:

        public void DisplayMenu()
        {
            Console.WriteLine("\n*********************************");
            Console.WriteLine("**********    MENU     **********");
            Console.WriteLine("*********************************");
            Console.WriteLine("1. Display details of households");
            Console.WriteLine("2. Display details of companies");
            Console.WriteLine("3. Modify company");
            Console.WriteLine("4. Add new company");
            Console.WriteLine("6. Advance to next day");
            Console.WriteLine("Q. Quit");
            Console.Write("\n Enter your choice: ");
        }

Change the method above by adding this line, before the Console.WriteLine for the quit option:

Console.WriteLine("7. Advance to next week");