I have read all the above answers, All good but I would like to try another approach to achieve a car with radio by considering a car which can either be fitted with Generic Radio OR a boss radio. I have used Interface , dependency injection principle, Polymorphism and Inheritance concept to achieve this. In this approach we have implemented the Generic and Boss radio from IRadio interface.
Please see below piece of code for reference
namespace CarAndRadioProgramExample
{
class Program
{
static void Main(string[] args)
{
GeneralRadio gRadio = new GeneralRadio();
gRadio.MaximumVolume = 100;
gRadio.MinimumVolume = 0;
Car carWithGRadio = new Car(gRadio);
carWithGRadio.SwitchOnCarRadio();
BossRadio bRadio = new BossRadio();
bRadio.MaximumVolume = 200;
bRadio.MinimumVolume = 0;
Car carWithBRadio = new Car(bRadio);
carWithBRadio.SwitchOnCarRadio();
Console.ReadLine();
}
}
class Car
{
IRadio carRadio;
public Car(IRadio radioObject)
{
carRadio = radioObject;
}
internal void SwitchOnCarRadio()
{
carRadio.RadioOn();
}
}
interface IRadio
{
int MaximumVolume { get; set; }
int MinimumVolume { get; set; }
void RadioOn();
}
class GeneralRadio :IRadio
{
public int MaximumVolume
{
get;
set;
}
public int MinimumVolume
{
get;
set;
}
public void RadioOn()
{
Console.WriteLine("Switching On Generic Radio with maximum volume" + MaximumVolume + "and minimum volume" + MinimumVolume);
}
}
class BossRadio : IRadio
{
public int MaximumVolume
{
get;
set;
}
public int MinimumVolume
{
get;
set;
}
public void RadioOn()
{
Console.WriteLine("Switching On Boss Radio with maximum volume" + MaximumVolume + "and minimum volume" + MinimumVolume);
}
}
}
When running the above code, the output going to be
