我有这个代码:打印一个输出,我不想再重复它来计算成本总额:
public void printCost(List<c_flightRotationAircraft> fRAList,
List<c_itineraryDistFra> iDFraList, HashSet<string> distinctAircraft, HashSet<int> distinctItinerary,
List<c_position> positionList, List<c_airportAircraft> aAList, c_config config)
{
c_files f = new c_files();
double pDC = passengerDelayCost(fRAList, iDFraList, distinctItinerary, config); // Passenger delay cost
double oCC = outboundCancelCost(fRAList, iDFraList, distinctItinerary, config); // Outbound cancellation cost
double iCC = inboundCancelCost(fRAList, iDFraList, distinctItinerary, config); // Inbound cancellation cost
double cLC = cancelLegalCost(fRAList, iDFraList, distinctItinerary, config); // Cancellation Legal Compensation Cost
double dLC = delayLegalCost(fRAList, iDFraList, distinctItinerary, config); // Delay legal cost
double fOCD = flightOperatingCostDecrease(fRAList, iDFraList, distinctItinerary, config); //Penalties for non-compliant location of aircraft
/*
f.insertAircraftPositionEndRtw(distinctAircraft, fRAList, config, positionList, aAList); // inserts the aircraft in the airports (aAList) List
double pNcLA = penaltyNonCompliantLocationAircraft(fRAList, iDFraList,
distinctAircraft, positionList, aAList, config); // Penalties for non-compliant location of aircraft
*/
Console.WriteLine();
Console.WriteLine("Passenger delay cost: {0}", pDC.ToString("#.##"));
Console.WriteLine("Outbound cancellation cost: {0}", oCC.ToString("#.##"));
Console.WriteLine("Inbound cancellation cost: {0}", iCC.ToString("#.##"));
Console.WriteLine("Cancellation Legal Compensation Cost: {0}", cLC.ToString("#.##"));
Console.WriteLine("Delay legal cost: {0}", dLC.ToString("#.##"));
Console.WriteLine("Flight operating cost decrease: {0}", fOCD.ToString("#.##"));
//Console.WriteLine("Penalties for non-compliant location of aircraft: {0}", pNcLA.ToString("#.##"));
}
有3种方法的简单方法是什么:一种用于计算值,另一种用于打印成本,第三种用于总计成本?
答案 0 :(得分:1)
通过一些设置,您可以删除大部分代码并在循环中完成所有操作。
首先,您需要定义一个类,该类可以包含每个成本的描述和金额。
class CostRecord
{
public string Name { get; set; }
public double Amount { get; set; }
}
我们需要将每条记录与可以计算成本的函数相关联,因此我们再添加一个属性ComputationFunction
。由于代码的性质,原型非常复杂。
class CostRecord
{
public string Name { get; set; }
public double Amount { get; set; }
public Func
<
List<c_flightRotationAircraft>,
List<c_itineraryDistFra>,
HashSet<string>,
HashSet<int>,
List<c_position>,
List<c_airportAircraft>,
c_config,
double
>
ComputationFunction { get; set; }
}
现在我们需要一个列表,看起来像这样:
private List<CostRecord> _costRecords = new List<CostRecord>();
我们需要在某处初始化它,可能在你的构造函数中:
_costRecords.Add(new CostRecord { Name = "Passenger delay cost",
ComputationFunction = passengerDelayCost });
_costRecords.Add(new CostRecord { Name = "Outbound cancellation cost",
ComputationFunction = outboundCancelCost });
_costRecords.Add(new CostRecord { Name = "Inbound cancellation cost",
ComputationFunction = inboundCancelCost });
_costRecords.Add(new CostRecord { Name = "Cancellation Legal Compensation Cost",
ComputationFunction = cancelLegalCost });
_costRecords.Add(new CostRecord { Name = "Delay legal cost",
ComputationFunction = delayLegalCost });
_costRecords.Add(new CostRecord { Name = "Flight operating cost decrease",
ComputationFunction = flightOperatingCostDecrease });
既然完成了所有设置,我们的解决方案非常简短:
c_files f = new c_files();
foreach(var r in _costRecords)
{
r.Amount = r.ComputationFunction(fRAList, iDFraList, distinctAircraft, distinctItinerary, positionList, aAList, config);
Console.WriteLine("{0}: {1:#.##}", r.Name, r.Amount);
}
Console.WriteLine("Total cost: {0}", _costRecords.Sum( r => r.Amount));
答案 1 :(得分:0)
有点难以确定你遇到麻烦的地方。
但是,为什么不创建一个方法来检索对象(自定义类对象)的值。此方法可以执行任何所需的计算。
然后,创建一个获取该对象并打印值的方法。并制作另一个获取该对象并计算总数。
如果这不能回答你的问题,你能否更详细地了解你的想法?
答案 2 :(得分:0)
如果只想调用方法一次,为什么不将sum作为方法的返回值返回?这样您就可以使用方法之外的sum-value:
public double printCost(List<c_flightRotationAircraft> fRAList,
List<c_itineraryDistFra> iDFraList, HashSet<string> distinctAircraft, HashSet<int> distinctItinerary,
List<c_position> positionList, List<c_airportAircraft> aAList, c_config config)
{
c_files f = new c_files();
double pDC = passengerDelayCost(fRAList, iDFraList, distinctItinerary, config); // Passenger delay cost
double oCC = outboundCancelCost(fRAList, iDFraList, distinctItinerary, config); // Outbound cancellation cost
double iCC = inboundCancelCost(fRAList, iDFraList, distinctItinerary, config); // Inbound cancellation cost
double cLC = cancelLegalCost(fRAList, iDFraList, distinctItinerary, config); // Cancellation Legal Compensation Cost
double dLC = delayLegalCost(fRAList, iDFraList, distinctItinerary, config); // Delay legal cost
double fOCD = flightOperatingCostDecrease(fRAList, iDFraList, distinctItinerary, config); //Penalties for non-compliant location
of aircraft
/*
f.insertAircraftPositionEndRtw(distinctAircraft, fRAList, config, positionList, aAList); // inserts the aircraft in the airports (aAList) List
double pNcLA = penaltyNonCompliantLocationAircraft(fRAList, iDFraList,
distinctAircraft, positionList, aAList, config); // Penalties for non-compliant location of aircraft
*/
Console.WriteLine();
Console.WriteLine("Passenger delay cost: {0}", pDC.ToString("#.##"));
Console.WriteLine("Outbound cancellation cost: {0}", oCC.ToString("#.##"));
Console.WriteLine("Inbound cancellation cost: {0}", iCC.ToString("#.##"));
Console.WriteLine("Cancellation Legal Compensation Cost: {0}", cLC.ToString("#.##"));
Console.WriteLine("Delay legal cost: {0}", dLC.ToString("#.##"));
Console.WriteLine("Flight operating cost decrease: {0}", fOCD.ToString("#.##"));
//Console.WriteLine("Penalties for non-compliant location of aircraft: {0}", pNcLA.ToString("#.##"));
return pDC + oCC + ... // whatever you want to sum.
}