starbuzz cafee içinde, ürünlerin hepsi temel olarak kahveden türemektedir. Mocha, cafee late, espresso, americano... Bu durumda bir abstract class yazarak, ürünlerin hepsini burdan türetmek mümkün gibi. bir espresso,americano,cafelate alan adamın hesabı:)
namespace StarbuzzCofee
{
class Program
{
static void Main(string[] args)
{
Service s = new Service();
s.ServiceAdd(
new Espresso());
s.ServiceAdd(
new Americano());
s.ServiceAdd(
new CafeLate());
s.ServiceAdd(
new Espresso());
s.PaymentToServicedCost();
}
}
public abstract class Beverages
{
public Beverages()
{
description =
"Bilinmeyen İçecek";
}
public string description;
public string getDescription()
{
return description;
}
public abstract double Cost();
}
//Temel kahve
public class Espresso : Beverages
{
public Espresso()
{
description =
"Espresso";
}
public override double Cost()
{
return 3D;
}
}
//Espresodan türeyen kahveler su ve espresso
public class Americano : Espresso
{
public Americano()
{
base.description = base.getDescription() + " to Americano";
}
public override double Cost()
{
return base.Cost() + 2;
}
}
//Espressodan türeyen late süt ve espresso
public class CafeLate : Espresso
{
public CafeLate()
{
base.description = base.getDescription() + " to CafeLate";
}
public override double Cost()
{
return base.Cost() + 3.5D;
}
}
public class Service
{
private System.Collections.ArrayList Serviced;
public Service()
{
Serviced =
new System.Collections.ArrayList();
}
public void ServiceAdd(Beverages bv)
{
Serviced.Add(bv);
}
public void PaymentToServicedCost()
{
double totalcost = 0;
foreach (Beverages bv in Serviced)
{
Console.WriteLine(bv.getDescription());
Console.WriteLine(bv.Cost());
totalcost += bv.Cost();
Console.WriteLine("--:--");
Console.WriteLine(totalcost);
}
Console.WriteLine("---------------");
Console.WriteLine("Total Cost :" + totalcost);
Console.ReadLine();
}
}
}