### [ASP.NET]重構之路系列v8 &ndash;合併重複的條件片段

[ASP.NET]重構之路系列v8 –合併重複的條件片段

Spec是這樣寫的：

1. 乘客是女生的話，低於60歲，收費為原價的8折；超過60歲則不收費。
2. 乘客是男生的話，低於50歲，收費為原價的9折；50～60歲的，收費為原價的95折；超過60歲則不收費。


public class Bus
{
public double Charge(Person customer)
{
const double ticketCost = 100;
double result = 0;

if (customer.IsFemale)
{
if (customer.Age <= 60)
{
result = ticketCost * 0.8;
}
else
{
return 0;
}
}
else
{
if (customer.Age <= 50)
{
result = ticketCost * 0.9;
}
else if (customer.Age <= 60)
{
result = ticketCost * 0.85;
}
else
{
return 0;
}
}

return result;
}
}


public double Charge(Person customer)
{
////超過60歲 => 老人，票價為0
if (customer.Age > 60)
{
return 0;
}

const double ticketCost = 100;
double result = 0;

if (customer.IsFemale)
{
if (customer.Age <= 60)
{
result = ticketCost * 0.8;
}
}
else
{
if (customer.Age <= 50)
{
result = ticketCost * 0.9;
}
else if (customer.Age <= 60)
{
result = ticketCost * 0.85;
}
}

return result;
}


public class Bus
{
private const double olderPrice = 0;
private const double youngLadyDiscount = 0.8;
private const double youngManDiscount = 0.9;
private const double strongManDiscount = 0.85;

private const int olderAge = 60;
private const int strongAge = 50;

private const double ticketCost = 100;

public double Charge(Person customer)
{
////超過60歲 => 老人，票價為0
if (customer.Age > olderAge)
{
return olderPrice;
}

////折扣
double discount = 0;

if (customer.IsFemale)
{
if (customer.Age <= olderAge)
{
}
}
else
{
if (customer.Age <= strongAge)
{
discount = youngManDiscount;
}
else if (customer.Age <= olderAge)
{
discount = strongManDiscount;
}
}

////計價方式=票價*折扣
double result = ticketCost * discount;
return result;
}
}

 對敏捷開發有興趣的朋友，可以參考我的粉絲專頁：91敏捷開發之路 若需要聯絡我，可以透過粉絲專頁私訊或是側欄的關於我。