Example #1
0
        public void ShowHarborInfo()
        {
            var boatsInHarbor  = Harbor.BoatsInHarbor();
            var availablePorts = 0;

            Console.WriteLine("Plats\tBåttyp\t\tReg.Nr.");
            for (int port = 0; port < boatsInHarbor.Length; port++)
            {
                if (boatsInHarbor[port] == null)
                {
                    availablePorts += 1;
                    Console.WriteLine($"{port + 1}\tTom");
                }
                // Checks the boat is unique and avoids index out of bounds by excluding second to last port
                else if (port != boatsInHarbor.Length - 1 && boatsInHarbor[port] != boatsInHarbor[port + 1])
                {
                    // Separates boats taking up multiple ports
                    if (boatsInHarbor[port].PortSpaceOccupied.Count > 1)
                    {
                        Console.WriteLine($"{boatsInHarbor[port].PortSpaceOccupied.FirstOrDefault() + 1}-" +
                                          $"{boatsInHarbor[port].PortSpaceOccupied.LastOrDefault() + 1}" +
                                          $"\t{boatsInHarbor[port].Type}\t{boatsInHarbor[port].Name}");
                    }
                    else
                    {
                        Console.WriteLine($"{boatsInHarbor[port].PortSpaceOccupied.FirstOrDefault() + 1}" +
                                          $"\t{boatsInHarbor[port].Type}\t{boatsInHarbor[port].Name}");
                    }
                }
                // Checks if it's the second to last port
                else if (port == boatsInHarbor.Length - 1)
                {
                    if (boatsInHarbor[port].PortSpaceOccupied.Count > 1)
                    {
                        // Separates boats taking up multiple ports
                        Console.WriteLine($"{boatsInHarbor[port].PortSpaceOccupied.FirstOrDefault() + 1}-" +
                                          $"{boatsInHarbor[port].PortSpaceOccupied.LastOrDefault() + 1}" +
                                          $"\t{boatsInHarbor[port].Type}\t{boatsInHarbor[port].Name}");
                    }
                    else
                    {
                        Console.WriteLine($"{boatsInHarbor[port].PortSpaceOccupied.FirstOrDefault() + 1}" +
                                          $"\t{boatsInHarbor[port].Type}\t{boatsInHarbor[port].Name}");
                    }
                }
            }
            Console.WriteLine($"Lediga platser i hamnen: {availablePorts}");
            Console.WriteLine($"Antalet avvisade båtar: {RejectedBoats.Count}");
        }