using System;
// Base/Parent Class (abstract class - can't be instantiated here)
public abstract class PublicServant
{
// Property
public int PensionAmount { get; set; }
// Method
public abstract void DriveToPlaceOfInterest();
}
//interface creation
public interface IPerson
{
string Name { get; set; }
int Age { get; set; }
}
// Sublclassing from the Parent Abstract class and implementing the interface
public class Firefighter : PublicServant , IPerson
{
public Firefighter(string name, int age) //constructor
{
this.Name = name;
this.Age = age;
}
public string Name { get; set; }
public int Age { get; set; }
//override method to define abstract class
public override void DriveToPlaceOfInterest()
{
GetInFiretruck();
TurnOnSiren();
FollowDirections();
}
private void GetInFiretruck() {}
private void TurnOnSiren() {}
private void FollowDirections() {}
}
// Sublclassing from the Parent Abstract class and implementing the interface along with a bool op to turn on siren if true.
public class PoliceOfficer : PublicServant , IPerson
{
private bool _hasEmergency;
public PoliceOfficer(string name, int age)
{
this.Name = name;
this.Age = age;
_hasEmergency = false;
}
public string Name { get; set; }
public int Age { get; set; }
public bool HasEmergency
{
get { return _hasEmergency; }
set { _hasEmergency = value; }
}
public override void DriveToPlaceOfInterest()
{
GetInPoliceCar();
if (this.HasEmergency)
TurnOnSiren();
FollowDirections();
}
private void GetInPoliceCar() {}
private void TurnOnSiren() {}
private void FollowDirections() {}
}
namespace ch1_the_fighters
{
public class MainClass
{
public static void Main(string[] args)
{
//Instantiating classes
Firefighter firefighter = new Firefighter("blossom" , 23);
firefighter.PensionAmount = 5000;
PrintNameAndAge(firefighter);
PrintPensionAmount(firefighter);
firefighter.DriveToPlaceOfInterest();
PoliceOfficer officer = new PoliceOfficer("bubbles" , 25);
officer.PensionAmount = 10000;
officer.HasEmergency = true;
PrintNameAndAge(officer);
PrintPensionAmount(officer);
officer.DriveToPlaceOfInterest();
}
static void PrintNameAndAge(IPerson person)
{
Console.WriteLine("Name: " + person.Name);
Console.WriteLine("Age :" + person.Age);
}
static void PrintPensionAmount(PublicServant servant)
{
if (servant is Firefighter)
Console.WriteLine("Pension of firefighter: " + servant.PensionAmount);
else if (servant is PoliceOfficer)
Console.WriteLine("Pension of officer: " + servant.PensionAmount);
}
}
}