My task in C#:
Random missile generator generating 2 types of missiles each second:
ThermoNuclearBomb
andFacetBomb
- Missiles have properties Serial Number, Type
Air Defense System, that is identifying the 2 types of missiles and neutralizes each of them with a different gun (
AntiThermoGun
andAntiFacetGun
)- Air Defense system has a method
AcceptMissile
that accepts aMissile
as a parameter - Every
Gun
has a methodNeutralize
that accepts an appropriateMissile
as a parameter and neutralizes only such types of missiles. - The Air Defense system must decide which missile will be neutralized with which gun.
- Air Defense system has a method
I've got two interfaces:
public interface IMissile
{
int SerialNumber { get; set; }
string Type { get; set; }
}
public interface IGun
{
string Name { get; set; }
void Neutrilize(IMissile missile);
}
Of course FacetBomb
and ThermoNuclearBomb
class implement this interface IMissile
:
public class FacetBomb : IMissile
{
public int SerialNumber { get; set; }
private string type;
public string Type { get; set; }
}
public class ThermoNuclearBomb:IMissile
{
public int SerialNumber { get; set; }
public string Type { get ; set ; }
}
Here are my two classes AntiThermoGun
and AntiFacetGun
that implement IGun
:
public class AntiThermoGun : IGun
{
private string name;
public string Name
{ get {return name;}
set { name = "AntiThermoGun"; }
}
public void Neutrilize(ThermoNuclear gun)
{
Console.WriteLine(this.Name + "neutrilizes" + gun.Type);
}
}
public class AntiFacetGun : IGun
{
private string name;
public string Name
{
get { return name; }
set { name = "AntiFacetGun"; }
}
public void Neutrilize(FacetBomb missile)
{
Console.WriteLine(this.Name + "neutrilizes" + missile.Type);
}
}
Here is my AirDefence
class which accepts a missile and decides which missile will be neutralized with which gun:
public class AirDefenceSystem
{
public void InterceptMissile(IMissile missile)
{
IGun igun = null;
switch (missile.Type)
{
case "ThermoNuclear":
igun = new AntiThermoGun();
break;
case "FacetBomb":
igun = new AntiFacetGun();
break;
}
igun.Neutrilize(missile);
}
}
In my program.cs:
static void Main(string[] args)
{
FacetBomb facetBomb = new FacetBomb { SerialNumber = 100, Type = "FacetBomb" };
FacetBomb facetBomb2 = new FacetBomb { SerialNumber = 200, Type = "FacetBomb" };
ThermoNuclear thermoBomb = new ThermoNuclear { SerialNumber = 500, Type = "ThermoNuclear" };
IMissile[] objects = new IMissile[] { facetBomb, facetBomb2, thermoBomb };
AirDefenceSystem system = new AirDefenceSystem();
foreach (var item in objects)
{
system.InterceptMissile(item);
}
}