2020 - Option 5 is missing, it could be to merge 2 companies

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

Before we can complete this task we need to add a couple of new methods in different classes. Firstly in the 'Company' class we need to add a method to get the current balance:

        public double GetBalance()
        {
            return balance;
        }

Also in the 'Company' class add the following to be able to access the list of outlets for this company:

       public List<Outlet> Outlets()
        {
            return outlets;
        }

To start the main part of this task, find the AddCompany method in the simulation class:

        private void AddCompany()
        {
            int balance, x = 0, y = 0;
            string companyName, typeOfCompany = "9";
            Console.Write("Enter a name for the company: ");
            companyName = Console.ReadLine();
            Console.Write("Enter the starting balance for the company: ");
            balance = Convert.ToInt32(Console.ReadLine());
            while (typeOfCompany != "1" && typeOfCompany != "2" && typeOfCompany != "3")
            {
                Console.Write("Enter 1 for a fast food company, 2 for a family company or 3 for a named chef company: ");
                typeOfCompany = Console.ReadLine();
            }
            if (typeOfCompany == "1")
            {
                typeOfCompany = "fast food";
            }
            else if (typeOfCompany == "2")
            {
                typeOfCompany = "family";
            }
            else
            {
                typeOfCompany = "named chief";
            }
            simulationSettlement.GetRandomLocation(ref x, ref y);
            Company newCompany = new Company(companyName, typeOfCompany, balance, x, y, fuelCostPerUnit, baseCostForDelivery);
            companies.Add(newCompany);
        }

This covers exactly what is required to merge 2 companies together (ie create a new one). So copy this method and paste it underneath, and rename it 'MergeCompanies'.

Add parameters for 'MergeCompanies' for the list of companies and the index number for each company:

        private void AddCompany(List<Company> companies, int index1,int index2)

Now edit the method to declare 2 companies ('comp1' and 'comp2'). I have also removed the question about the balance, because we can add the balance of the two companies together instead:

public void MergeCompanies(List<Company> companies, int index1,int index2)
        {
            Company comp1 = companies[index1];
            Company comp2 = companies[index2];

            int x = 0, y = 0;
            string typeOfCompany = "-1";

            Console.WriteLine("Enter a new name for the merged company");
            string companyName = Console.ReadLine();

            while (typeOfCompany != "1" && typeOfCompany != "2" && typeOfCompany != "3")
            {
                Console.Write("Enter 1 for a fast food company, 2 for a family company or 3 for a named chef company: ");
                typeOfCompany = Console.ReadLine();
            }
            if (typeOfCompany == "1")
            {
                typeOfCompany = "fast food";
            }
            else if (typeOfCompany == "2")
            {
                typeOfCompany = "family";
            }
            else
            {
                typeOfCompany = "named chief";
            }

            
            simulationSettlement.GetRandomLocation(ref x, ref y);

            Company newCompany = new Company(companyName, typeOfCompany, comp1.GetBalance() + comp2.GetBalance(), x, y, fuelCostPerUnit, baseCostForDelivery);
        }

Finally we need to go through each outlet in company 1 and company 2 and add them to the new company. Finally we need to remove the old companies and add the new company:

public void MergeCompanies(List<Company> companies, int index1,int index2)
        {
            Company comp1 = companies[index1];
            Company comp2 = companies[index2];

            int x = 0, y = 0;
            string typeOfCompany = "-1";

            Console.WriteLine("Enter a new name for the merged company");
            string companyName = Console.ReadLine();

            while (typeOfCompany != "1" && typeOfCompany != "2" && typeOfCompany != "3")
            {
                Console.Write("Enter 1 for a fast food company, 2 for a family company or 3 for a named chef company: ");
                typeOfCompany = Console.ReadLine();
            }
            if (typeOfCompany == "1")
            {
                typeOfCompany = "fast food";
            }
            else if (typeOfCompany == "2")
            {
                typeOfCompany = "family";
            }
            else
            {
                typeOfCompany = "named chief";
            }

            
            simulationSettlement.GetRandomLocation(ref x, ref y);

            Company newCompany = new Company(companyName, typeOfCompany, comp1.GetBalance() + comp2.GetBalance(), x, y, fuelCostPerUnit, baseCostForDelivery);
            
            foreach (Outlet o in companies[index1].Outlets())
            {
                newCompany.Outlets().Add(o);
            }

            foreach (Outlet o in companies[index2].Outlets())
            {
                newCompany.Outlets().Add(o);
            }

            companies.Remove(comp1);
            companies.Remove(comp2);
            companies.Add(newCompany);

        }