# 求解购买不同产品时计算最少运费的算法

0

A001，重量为100g，支持的递送方式为：免运费平邮、EMS、DHL、FedEx；
A002，重量为200g，支持的递送方式为：EMS、DHL、UPS；
A003，重量为150g，支持的递送方式为：DHL、UPS；
A004，重量为500g，支持的递送方式为：免运费平邮、FedEx、UPS。

EMS：每100g收费10元快递费，不足100g按100g计算；
DHL：每200g收费20元快递费，不足200g按200g计算；
UPS：每300g收费25元快递费，不足300g按300g计算；
FedEx：每200g收费20元快递费，不足200g按200g计算；

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 处理产品表
{
ExpressList = new List<IExpress>()
{
new NormalExpress(),
new EMS(),
new DHL(),
new FedEx()
}
}
);

{
ExpressList = new List<IExpress>()
{
new EMS(),
new DHL(),
new UPS()
}
}
);

{
ExpressList = new List<IExpress>()
{
new DHL(),
new UPS()
}
}
);

{
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;
}
}
}

qfzhang | 初学一级 | 园豆：156

您需要登录以后才能回答，未注册用户请先注册