根据另一个价格改变价格

时间:2016-04-08 12:43:31

标签: c# math

我有一个名为LogCalculator的课程,在这个课程中,我计算了一个日志的Board Feet和一个日志的总成本(boardFeet*PricePerFoot),这一切都很有效。

我的问题是我什么时候回去:

  • 1200英尺* 1.25美元= 1500
  • 100英尺* 3.25 = 325
  • 200英尺* 4 = 800
  • 总计= $ 2625

当日志销售商想要总购买金额为3000美元时,我需要按百分比提高每英尺的成本。

我有一个名为Process的方法:

public List<mLog> Process(List<mLog> logList, decimal? adjustedFeet, decimal? adjustedTotal, decimal? startPricePerFoot,
    decimal? startTotal, mLog selectedLog)
{
    logList = UpdateList(logList, adjustedFeet, adjustedTotal, selectedLog.AdjCostPerFoot, startTotal);
    _saveRepository.SaveLogs(logList);
    logList = _selectRepository.LogsByParcel(selectedLog.parcel);
    selectedLog.ProducerPricePerFoot = _calculator.CalculateCostPerFoot(selectedLog.NetCost,
        selectedLog.CutbackFeet);
    _eventAggregator.GetEvent<RefreshLogListEvent>().Publish(logList);
    return logList;
}

要求更新列表:

  public List<mLog> UpdateList(List<mLog> logList, decimal? adjustedFeet, decimal? adjustedTotal, decimal? startPricePerFoot, decimal? startTotal)
    {
        var i = 0;
        foreach (var log in logList)
        {
            i++;
            var args = adjustedFeet == null
                ? (LogArguments) _calculator.GetLogArgs(log, Convert.ToDecimal(startTotal), Convert.ToDecimal(adjustedTotal))
                : (LogArguments) _calculator.GetLogArgs(log, adjPricePerFoot: Convert.ToDecimal(startPricePerFoot),adjustFeet:adjustedFeet);
            if(args==null)continue;
            log.AdjCostPerFoot = args.CostPerFoot;
            log.AdjNet = args.TotalCost;
          //  if (i != logList.Count) continue;
            if (adjustedFeet == null)
            {
                var amount = logList.Sum(l => l.AdjNet) - Convert.ToDecimal(adjustedTotal);
                log.AdjNet = log.AdjNet - amount;
            }
            else
            {
                var amount = logList.Average(l => l.AdjCostPerFoot) - Convert.ToDecimal(adjustedFeet);
                log.AdjCostPerFoot = log.AdjCostPerFoot - amount;
            }
        }
        return logList;
    }

GetLogArgs:

 public object GetLogArgs(mLog log, decimal oldAmount = 0, decimal newAmount = 0, 
        decimal adjPricePerFoot = 0, decimal? adjustFeet = null)
    {
        decimal changeAmount = 0;
        decimal total = 0;
        if (oldAmount == 0 && newAmount == 0 && adjPricePerFoot == 0 && adjustFeet == null)
        {
            return new LogArguments() { Diameter=Convert.ToInt32(log.Diameter),Length = Convert.ToInt32(log.LogLength)};
        }
        if (adjustFeet == null)
        {
            total = CalculatePercentage(newAmount, oldAmount);
            total = total * log.AdjCostPerFoot * log.CutbackFeet;
            changeAmount = CalculateCostPerFoot(total, log.CutbackFeet);
        }
        else
        {
            changeAmount = log.AdjCostPerFoot + (Convert.ToDecimal(adjustFeet) - adjPricePerFoot);
            total = CalculateTotalCost(log.CutbackFeet, changeAmount);
        }
        return new LogArguments() { CostPerFoot = changeAmount, TotalCost = total, LogId = log.Id };
    }

CalculatePercentage

public decimal CalculatePercentage(decimal newAmount, decimal oldAmount)
{
    return (1 - ((oldAmount - newAmount)/oldAmount));
}

我得到的结果很奇怪,就像我会得到一些完全被一些白金关闭的结果。如果有人能看到我犯了一个很好的错误。

0 个答案:

没有答案