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(ProductSeasonList ProductSeasonList) { List slicePoints = new List(); foreach (ProductSeason p in this) { if (!slicePoints.Contains(p.StartDate)) slicePoints.Add(p.StartDate); if (!slicePoints.Contains(p.EndDate)) slicePoints.Add(p.EndDate); } 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, ProductSeasonList ProductSeasonList) { ProductSeason x = this.Find ( delegate(ProductSeason p) { return (p.StartDate <= time && time < p.EndDate); } ) ; if (x != null) return x.Copy(); ProductSeason y = ProductSeasonList.Find ( delegate(ProductSeason p) { return (p.StartDate <= time && time < p.EndDate); } ) ; if (y != null) return y.Copy(); else return null; } } }