日付範囲を扱うコードに取り組んでいます。その範囲の特定の価格を設定するために、開始日と終了日を持つ価格設定アクティビティがあります。交差する日付範囲を持つ複数の価格設定アクティビティがあります。
最終的に必要なのは、日付範囲の有効な価格を照会する機能です。(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…..
}
}