using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace RoomsHere.BusinessLayer { public class ProductSeasonList : List { public ProductSeasonList() { } public ProductSeasonList(IList productSeasonList) { foreach(ProductSeason ps in productSeasonList ) { this.Add(ps); } } /// /// Initializes a new instance of the class. /// /// The product season. public ProductSeasonList(ProductSeason productSeason) { this.Add(productSeason); } /// /// Projects a season onto another to flatten it out /// /// The product season list. /// public ProductSeasonList ProjectOnto(params ProductSeasonList[] ProductSeasonLists) { List ProductSeasonList = new List(this); foreach(ProductSeasonList l in ProductSeasonLists) ProductSeasonList.Add(l); List slicePoints = new List(); foreach (ProductSeason p in ProductSeasonList) { if (!slicePoints.Contains(p.StartDate)) slicePoints.Add(p.StartDate); if (!slicePoints.Contains(p.EndDate)) slicePoints.Add(p.EndDate); } slicePoints.Sort(); ProductSeasonList results = new ProductSeasonList(); ProductSeason current = null; foreach (DateTime i in slicePoints) { ProductSeason best = SelectBest(i, ProductSeasonList); if (current != null) { if (best == null || best != current) { current.EndDate = i; results.Add(current); current = null; } } // no else here if (current == null) { if (best != null) { current = best; // or copy $ or whatever current.StartDate = i; } } } return results; } /// /// Selects the preferred period /// /// The time. /// The product season list. /// public ProductSeason SelectBest(DateTime time, List ProductSeasonList) { List results = ProductSeasonList.FindAll ( delegate(ProductSeason p) { return (p.StartDate <= time && time < p.EndDate); } ) if(results.Count ==0) return null; results.Sort(); return results[0].Copy(); } }