我正在处理一些处理日期范围的代码。我的定价活动具有开始日期和结束日期,以便为该范围设定特定价格。有多个具有交叉日期范围的定价活动。
我最终需要的是能够查询日期范围内的有效价格。我传入 (jan1,jan31) 并返回一个列表,上面写着 jan1-->jan10 $4 , jan11-->jan19 $3 jan20-->jan31 $4。
定价活动之间存在优先级。某些类型的定价活动具有高优先级,因此它们优先于其他定价活动,并且对于某些类型的定价活动,最低价格获胜等。
我目前有一个班级,负责举办这些定价活动并保留已解决的定价日历。当我添加新的定价活动时,我会更新已解决的日历。随着我编写更多的测试/代码,它开始变得非常复杂,因为所有不同的情况(定价活动以不同的方式相交)。我最终得到了非常复杂的代码,我正在解决一个新添加的定价活动。见AddPricingActivity()
下面的方法。
有人能想出一个更简单的方法来处理这个问题吗?那里有类似的代码吗?
Public class PricingActivity()
{
DateTime startDate;
DateTime endDate;
Double price;
Public bool StartsBeforeEndsAfter (PricingActivity pAct)
{
// pAct covers this pricing activity
}
Public bool StartsMiddleEndsAfter (PricingActivity pAct){
// early part of pAct Itersects with later part of this pricing activity
}
//more similar methods to cover all the combinations of intersecting
}
Public Class PricingActivityList()
{
List<PricingActivity> activities;
SortedDictionary<Date, PricingActivity> resolvedPricingCalendar;
Public void AddPricingActivity(PricingActivity pAct)
{
//update the resolvedCalendar
//go over each activity and find intersecting ones
//update the resolved calendar correctly
//depending on the type of the intersection
//this part is getting out of hand…..
}
}