ShoppingMall - Mall Class
namespace ShoppingMall
{
public class Mall
{
public string MallName { get; set; }
public IDiscount DiscountBehavor { get; set; }
public decimal calPrice(decimal BillTotal)
{
return DiscountBehavor.Discount(BillTotal);
}
}
public class Disc50 : IDiscount
{
public decimal Discount(decimal BillTotal)
{
return Math.Round(BillTotal * 5 / 10, 0, MidpointRounding.AwayFromZero);
}
public string Descript
{
get { return "5 折"; }
}
}
public class Disc88 :IDiscount
{
public decimal Discount(decimal BillTotal)
{
return Math.Round(BillTotal * 88 / 100, 0, MidpointRounding.AwayFromZero);
}
public string Descript
{
get { return "88 折"; }
}
}
public class Coupon1000 : IDiscount
{
public string Descript
{
get { return "買萬送千"; }
}
public decimal Discount(decimal BillTotal)
{
if (BillTotal > 10000)
BillTotal -= 1000;
return BillTotal;
}
}
public interface IDiscount
{
string Descript { get; }
decimal Discount(decimal BillTotal);
}
}
ShoppingMallDemo - Web.Config<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<appSettings>
<add key="discount" value="ShoppingMall.Coupon1000,ShoppingMall"/>
</appSettings>
</configuration>
ShoppingMallDemo - cs 檔
using ShoppingMall;
public partial class _Default : System.Web.UI.Page
{
protected void btnCal_Click(object sender, EventArgs e)
{
Mall mall = new Mall();
IDiscount disc = GetDiscount();
mall.DiscountBehavor = disc;
lblPay.Text = mall.calPrice(Convert.ToDecimal(txtBillTotal.Text)).ToString();
lblDisc.Text = disc.Descript;
}
IDiscount GetDiscount()
{
string setting = System.Configuration.ConfigurationManager.AppSettings["discount"];
string[] arr = setting.Split(',');
string dllName = arr[1];
string className = arr[0];
IDiscount disc = (IDiscount)System.Reflection.Assembly.Load(dllName).CreateInstance(className);
return disc;
}
}
沒有留言:
張貼留言