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
Revision as of 17:33, 14 December 2019 by Admin (talk | contribs) (Created page with "=The Issue= Find the following method in the company class: <syntaxhighlight lang=csharp> public void AddVisitToNearestOutlet(int x, int y) { int nearestO...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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++;
            }
        }