using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsolePrj { class ExpressCalc { public void Test() { List<Product> lstProduct = new List<Product>(); #region 处理产品表 lstProduct.Add(new Product("A001", 100) { ExpressList = new List<IExpress>() { new NormalExpress(), new EMS(), new DHL(), new FedEx() } } ); lstProduct.Add(new Product("A002", 200) { ExpressList = new List<IExpress>() { new EMS(), new DHL(), new UPS() } } ); lstProduct.Add(new Product("A003", 100) { ExpressList = new List<IExpress>() { new DHL(), new UPS() } } ); lstProduct.Add(new Product("A004", 100) { ExpressList = new List<IExpress>() { new NormalExpress(), new UPS(), new FedEx() } } ); #endregion //求解算法…… } } #region 快递方式 public abstract class IExpress { public IExpress(string name) { this.Name = name; } public string Name { get; set; } public abstract double GetPrice(double weight); } public class NormalExpress : IExpress { public NormalExpress() : base("免运费平邮") { } public override double GetPrice(double weight) { if (weight <= 300) { return 0; } else { double p = weight - 300; if (p % 300 == 0) { return (p / 300) * 5; } else { return (p / 300 + 1) * 5; } } } } public class EMS : IExpress { public EMS() : base("EMS") { } public override double GetPrice(double weight) { double unitWeight = 100; double everyMoney = 10; if (weight % unitWeight == 0) { return (weight / unitWeight) * everyMoney; } else { return (weight / unitWeight + 1) * everyMoney; } } } public class DHL : IExpress { public DHL() : base("DHL") { } public override double GetPrice(double weight) { double unitWeight = 200; double everyMoney = 20; if (weight % unitWeight == 0) { return (weight / unitWeight) * everyMoney; } else { return (weight / unitWeight + 1) * everyMoney; } } } public class UPS : IExpress { public UPS() : base("UPS") { } public override double GetPrice(double weight) { double unitWeight = 300; double everyMoney = 25; if (weight % unitWeight == 0) { return (weight / unitWeight) * everyMoney; } else { return (weight / unitWeight + 1) * everyMoney; } } } public class FedEx : IExpress { public FedEx() : base("FedEx") { } public override double GetPrice(double weight) { double unitWeight = 200; double everyMoney = 20; if (weight % unitWeight == 0) { return (weight / unitWeight) * everyMoney; } else { return (weight / unitWeight + 1) * everyMoney; } } } #endregion public class Product { public Product(string name, double weight) { this.Name = name; this.Weight = weight; } public string Name { get; set; } public double Weight { get; set; } public List<IExpress> ExpressList { get; set; } } }