I have an Airport class where Plane objects can take off and land, depending on the weather. If it's stormy, they can't take off/land and if it's sunny they can. I have the following Airport constructor:
public string AirportName { get; set; }
public List<Plane> planes;
public Airport(string airportName)
{
planes = new List<Plane>();
AirportName = airportName;
Weather weather = new Weather();
}
I have a Weather class that randomises the weather:
public class Weather
{
public Weather()
{
}
public string Forecast()
{
Random random = new Random();
var weather = random.Next(1, 11);
if (weather == 1 || weather == 2)
{
return "stormy";
}
else
{
return "sunny";
}
}
}
This is how I use the airport in Main:
static void Main()
{
var gatwick = new Airport("London Gatwick");
}
As the Weather information is provided by a separate class I would like to inject it into Airport as a dependency. However, I am struggling to do this in C# as I'm really new to the language. Would it be something such as: public Airport(string airportName, Weather weather)
?
Would be really grateful if someone could help me understand how to inject as a dependency and how to then call this in Main. Thank you!