switch case 多个处理方式涉及到销售优惠折扣方案处理
定义一个销售接口 ICarSalsecs public interface ICarSalsecs { /// <summary> /// 优惠折扣方案接口 /// </summary> /// <param name="policType">打折类型</param> /// <param name="SalsePrice">折前价格</param> /// <returns></returns> decimal Discount(short policType, decimal SalsePrice); }
定义一个销售类
public class CarSalse: ICarSalsecs { private readonly Dictionary<short, Func<decimal, decimal>> _dic; public CarSalse() { _dic = new Dictionary<short, Func<decimal, decimal>> { {0, m => m * (decimal)0.1}, {1, m => m * (decimal)0.3}, {2, m => m * (decimal)0.5}, {3, m => m * (decimal)0.7}, {4, m => m * (decimal)0.9} }; } //把各种打折方案封装在字典集合中。 public decimal Discount(short policyType, decimal SalsePrice) { if (_dic.ContainsKey(policyType)) { return _dic[policyType].Invoke(SalsePrice); } return SalsePrice / 2; } } /// <summary> /// 折扣枚举类型 /// </summary> public enum PolicyType { 一折 = 0, 三折 = 1, 五折 = 2, 七折 = 3, 九折 = 4 }
调用方法实现折扣处理
static void Main(string[] args) { Console.WriteLine("请输入打折策略,0表示1折,1表示3折,2表示5折,3表示7折,4表示9折:"); string policy = Console.ReadLine(); decimal SalsePrice = (decimal)10000.00; Console.WriteLine("打折前的价格为:" + SalsePrice); CarSalse Salse = new CarSalse(); if (!string.IsNullOrWhiteSpace(policy)) { Console.WriteLine("打折后的价格为:" + Salse.Discount(short.Parse(policy), SalsePrice)); } Console.ReadKey(); }