计算板坯率

时间:2016-07-25 10:12:04

标签: c#

我想根据使用C#的平板计算电费,如下表所示

 SlabFrom    SlabTo      Rate

   0           100         5.00
  101          500         10.00
  501          Null        15.00

平板数量可以改变,需要一个最佳解决方案来计算总成本
根据总使用量。如果总使用量为1,000,则应计算:

(100×5)+(400×10)+(500×15)= 12,000

public class Cost
{
    public decimal  SlabFrom { get; set; }  

    public decimal SlabTo { get; set; }  

    public bool SlabHour { get; set; }

    public decimal RatePerUnit { get; set; }
}

        var rates = new List<Cost>
        {
            new Cost()
            {
                SlabFrom = 0,
                SlabTo = 100,
                RatePerUnit = 5
            },
            new Cost()
            {
                SlabFrom = 101,
                SlabTo = 500,
                RatePerUnit = 10
            },
            new Cost()
            {
                SlabFrom = 501,
                SlabTo = 1000,
                RatePerUnit = 15
            },
            new Cost()
            {
                SlabFrom = 1001,
                SlabTo = 1500,
                RatePerUnit = 20
            }
        };

5 个答案:

答案 0 :(得分:1)

你可以这样做:

  NumberFormat numberFormat = NumberFormat.getCurrencyInstance(new Locale(
                "CO", "es"));

答案 1 :(得分:1)

如果你只想要前3个

int usage = 503;
int total = usage < 100 ? usage * 5 : 500 + (usage < 500 ? (usage - 100) * 10 : 4000 + (usage - 500) * 15);
 //4545

如果你想要一个更通用的答案
顺便说一句,我用你的Cost类来测试这个

decimal used = 1000;
decimal LastSlabto = 0;
decimal total = 0;

foreach (Cost rate in rates)
{
   if (used > rate.SlabTo - LastSlabto)
      total += (rate.SlabTo - LastSlabto) * rate.RatePerUnit;
   else
   {
      total += used * rate.RatePerUnit;
      used = 0;
      break;
   }
   used -= rate.SlabTo - LastSlabto;
   LastSlabto = rate.SlabTo;             
} 

答案 2 :(得分:0)

试试这个

input

答案 3 :(得分:0)

101-500范围计算为(500-101)* 10.但是,可以通过进行微小更改来修改它以符合您的要求。

private static double CalculateRate(int totalUsage)
{
    var applicableSlabRates = sr.Where(r => r.SlabFrom <= totalUsage).OrderBy(r => r.SlabFrom);

        double cost = 0;
        foreach (var r in applicableSlabRates)
        {
            int range = 0;
            if ((r.SlabFrom < totalUsage && totalUsage <= r.SlabTo) || r.SlabTo == null)
            {
                range = totalUsage;
            }
            else
            {
                range = Convert.ToInt32( r.SlabTo ) - r.SlabFrom;
            }

            cost += range * r.Rate;

            totalUsage = totalUsage - range;
        }

        return cost;
}


 public class SlabRate
 {
        public int SlabFrom { get; set; }
        public int? SlabTo { get; set; }
        public double Rate { get; set; }
 }

 static List<SlabRate> sr = new List<SlabRate>();
    sr.Add(new SlabRate() { SlabFrom = 0, SlabTo = 100, Rate = 5.00 });
    sr.Add(new SlabRate() { SlabFrom = 101, SlabTo = 500, Rate = 10.00 });
    sr.Add(new SlabRate() { SlabFrom = 501, SlabTo = null, Rate = 15.00 });

答案 4 :(得分:0)

从上面的回答我说得对,谢谢。请参阅

 private static double GenericCostCalculation(double units)
    {
        var rates = new List<Cost>
        {
            new Cost { SlabFrom = 0, SlabTo = 100, RatePerUnit = 10 },

            new Cost { SlabFrom = 101, SlabTo = 500, RatePerUnit = 20 },

            new Cost { SlabFrom = 501, SlabTo = 1000,RatePerUnit = 30 },

            new Cost { SlabFrom = 1001, SlabTo = 2000, RatePerUnit = 40 }
        };

        double cost = 0;
        foreach (var rate in rates)
        {
            if (units > 0)
            {
                if ((rate.SlabTo == rates.Max(x => x.SlabTo)) || (units < rate.SlabTo))
                {
                    cost += units * rate.RatePerUnit;
                    units = 0;
                }
                else
                {
                    cost += (rate.SlabTo - rate.SlabFrom) * rate.RatePerUnit;
                    units -= (rate.SlabTo - rate.SlabFrom);
                }
            }
        }
        return cost;
    }