public static void Main()
BusRouteRepository repository = new BusRouteRepository();
BusTimes times5 = repository.BusTimesRoute5;
BusRoute route5 = times5.Route;
for (int iPlace = 0; iPlace < route5.PlacesServed.Length; iPlace++)
Console.Write(route5.PlacesServed[iPlace].PadRight(12));
for (int iJourney = 0; iJourney < times5.Times.GetLength(1); iJourney++)
Console.Write(times5.Times[iPlace, iJourney] + " ");
public class BusRouteRepository
private readonly BusRoute[] _allRoutes;
public BusRouteRepository()
_allRoutes = new BusRoute[] {
new BusRoute(40, new string[] {
"Morecambe", "Lancaster", "Garstang", "Preston" }),
new BusRoute(42, new string[] { "Lancaster", "Garstang", "Blackpool" }),
new BusRoute(100, new string[] { "University", "Lancaster", "Morecambe" }),
new BusRoute(555, new string[] {
"Lancaster", "Carnforth", "Kendal", "Windermere", "Keswick" }),
new BusRoute(5, new string[] { "Overton", "Morecambe", "Carnforth"})
{ "15:40", "16:40", "17:40", "18:40" },
{ "16:08", "17:08", "18:08", "19:08" },
{ "16:35", "17:35", "18:35", "19:35" }
BusTimesRoute5 = new BusTimes(
Array.Find(_allRoutes, x => x.Number == 5), timesRoute5);
public BusTimes BusTimesRoute5 { get; }
public BusRoute[] FindBusesTo(string location)
return Array.FindAll(_allRoutes, route => route.Serves(location));
public BusRoute[] FindBusesBetween(string location1, string location2)
return Array.FindAll(_allRoutes,
route => route.Serves(location1) && route.Serves(location2));
public int Number { get; }
public string Origin => PlacesServed[0];
public string Destination => PlacesServed[^1];
public string[] PlacesServed { get; }
public BusRoute(int number, string[] placesServed)
this.PlacesServed = placesServed;
public override string ToString() => $"{Number}: {Origin} -> {Destination}";
public bool Serves(string destination)
return Array.Exists(PlacesServed, place => place == destination);
public string[,] Times { get; }
public BusRoute Route { get; }
public BusTimes(BusRoute route, string[,] times)