2020 - Currently no use is made of GetCapacity, make it so an outlet can only have this number of visitor in day

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

The Issue

Find the following method in the company class:

public void AddVisitToNearestOutlet(int x, int y)
        {
            int nearestOutlet = 0;
            double nearestOutletDistance, currentDistance;
            nearestOutletDistance = Math.Sqrt((Math.Pow(outlets[0].GetX() - x, 2)) + (Math.Pow(outlets[0].GetY() - y, 2)));
            for (int current = 1; current < outlets.Count; current++)
            {
                currentDistance = Math.Sqrt((Math.Pow(outlets[current].GetX() - x, 2)) + (Math.Pow(outlets[current].GetY() - y, 2)));
                if (currentDistance < nearestOutletDistance)
                {
                    nearestOutletDistance = currentDistance;
                    nearestOutlet = current;
                }
            }
            outlets[nearestOutlet].IncrementVisits();
        }

This code calculates the magnitude of the vector between the current location (x and y in the parameters) and each outlet. The shortest distance is used to select a particular outlet.

With the nearest outlet, the method 'IncrementVisits' is run to add a visit for this outlet. This fails to check the capacity of the outlet and would allow unlimited visitors dispite having a capacity set.

The Solution

We need to add some logic around the 'outlets[nearestOutlet].IncrementVisits()' line. We should use the GetCapacity method of the outlet to check if we have reached capacity.

Example

We will need to create a new method in outlet to return the number of visitors today. So add the following to the 'Outlet' class:

        public int VisitsToday()
        {
            return visitsToday;
        }

Now add the following logic around the 'outlets[nearestOutlet].IncrementVisits()' line.

            if (outlets[nearestOutlet].GetCapacity() < outlets[nearestOutlet].VisitsToday())
            {
                outlets[nearestOutlet].IncrementVisits();
            }

Example

Instead of the above method, you could just edit the 'IncrementVisits' method in the 'Outlet' class. It should currently be:

        public void IncrementVisits()
        {
            visitsToday++;
        }

We need to add the logic around the 'visitsToday++;' line:

        public void IncrementVisits()
        {
            if (GetCapacity() < visitsToday)
            {
                visitsToday++;
            }
        }

Using this method we could also declare a new variable within the 'Outlet' class, called 'turnAway' to record the number of customers turned away when capacity is reached. So add this to the 'Outlet' Class:

        public int turnAway=0;

Change the logic which checks capacity:

        public void IncrementVisits()
        {
            if (GetCapacity() < visitsToday)
            {
                visitsToday++;
            }
            else
            {
                turnAway++;
            }
        }

Finally we will need to change the 'NewDay' method in the 'Outlet' class, it currently clears the 'visitsToday' to zero and we also need it to set the 'turnAway' to zero also:

        public void NewDay()
        {
            visitsToday = 0;
            turnAway = 0;
        }

You could then add 'turnAway' into the output for the outlet.