Exemplo n.º 1
0
        public void UpdatePrice(PriceUpdate updateRequest)
        {
            if (updateRequest.Price == CurrentPrice)
            {
                return;
            }

            PriceHistory.Add(updateRequest);

            MonthlyPrices.Add(updateRequest);
            HistoricalPrices.Last().Price = PriceHistory.Where(pu => pu.Date.Year == updateRequest.Date.Year).Average(value => value.Price);
            YearlyPrices.ElementAt(updateRequest.Date.Month - 1).Price = PriceHistory.Where(pu => pu.Date.Year == updateRequest.Date.Year && pu.Date.Month == updateRequest.Date.Month).Average(value => value.Price);

            CurrentPrice = updateRequest.Price;
        }
Exemplo n.º 2
0
        public void FillPriceHistory()
        {
            m_allPriceUpdates = new Dictionary <DateTime, double>();
            MonthlyPrices.Clear();
            YearlyPrices.Clear();
            HistoricalPrices.Clear();
            CurrentPrice = PriceHistory.Last().Price;

            foreach (PriceUpdate update in PriceHistory)
            {
                m_allPriceUpdates.Add(update.Date, update.Price);
            }

            if (m_allPriceUpdates != null && m_allPriceUpdates.Count != 0)
            {
                List <int> years = new List <int>();
                DateTime   now   = DateTime.Now;

                foreach (DateTime newDate in m_allPriceUpdates.Keys)
                {
                    if (!years.Contains(newDate.Year))
                    {
                        // fill all yearly avg prices
                        years.Add(newDate.Year);

                        if (years.Count == 1)
                        {
                            double yearAvgPrice = m_allPriceUpdates.Where(date => date.Key.Year == newDate.Year).Average(value => value.Value);
                            HistoricalPrices.Add(new PriceUpdate(new DateTime(newDate.Year, 1, 1), yearAvgPrice, "test"));
                        }

                        if (years.Count() >= 2)
                        {
                            // year is increment by 1
                            double oldPriceLastYear = m_allPriceUpdates.Where(date => date.Key.Year == years.ElementAt(years.Count - 2)).Last().Value;

                            Dictionary <DateTime, double> copyAll = new Dictionary <DateTime, double>(m_allPriceUpdates);
                            copyAll.Add(new DateTime(newDate.Year, 1, 1), oldPriceLastYear);

                            double yearAvgPrice = copyAll.Where(date => date.Key.Year == newDate.Year).Average(value => value.Value);
                            HistoricalPrices.Add(new PriceUpdate(new DateTime(newDate.Year, 1, 1), yearAvgPrice, "test"));

                            // if there is no update for a year
                            int yearDif = newDate.Year - years.ElementAt(years.Count - 2);

                            if (yearDif > 1)
                            {
                                double oldPrice = m_allPriceUpdates.Where(date => date.Key.Year == years.ElementAt(years.Count - 2)).Last().Value;

                                for (int i = years.ElementAt(years.Count - 2) + 1; i < newDate.Year; ++i)
                                {
                                    HistoricalPrices.Add(new PriceUpdate(new DateTime(i, 1, 1), oldPrice, "test"));
                                }
                            }
                        }

                        // fill all months average prices for current year
                        if (now.Year == newDate.Year)
                        {
                            double monthAvg = 0.0;

                            for (int i = 1; i <= now.Month; ++i)
                            {
                                Dictionary <DateTime, double> copyAll = new Dictionary <DateTime, double>(m_allPriceUpdates);

                                if (i == 1)
                                {
                                    double oldPriceLastYear = m_allPriceUpdates.Where(date => date.Key.Year == years.ElementAt(years.Count - 2)).Last().Value;
                                    copyAll.Add(new DateTime(newDate.Year, 1, 1), oldPriceLastYear);
                                }

                                else
                                {
                                    double oldPriceLastMonth = m_allPriceUpdates.Where(date => date.Key.Year == now.Year && date.Key.Month == i - 1).Last().Value;
                                    copyAll.Add(new DateTime(newDate.Year, i, 1), oldPriceLastMonth);
                                }

                                var temp = copyAll.Where(date => date.Key.Year == newDate.Year && date.Key.Month == i);

                                if (temp.Count() > 0)
                                {
                                    monthAvg = temp.Average(value => value.Value);
                                    YearlyPrices.Add(new PriceUpdate(new DateTime(newDate.Year, i, 1), monthAvg, "test"));
                                }
                                else
                                {
                                    var lastChanges = m_allPriceUpdates.Where(date => date.Key.Year == newDate.Year && date.Key.Month < i);
                                    if (lastChanges.Count() > 0)
                                    {
                                        YearlyPrices.Add(new PriceUpdate(new DateTime(newDate.Year, i, 1), lastChanges.Last().Value, "test"));
                                    }
                                }
                            }

                            // for days in current month
                            var lastMonthChanges = m_allPriceUpdates.Where(date => date.Key.Year == now.Year && date.Key.Month < now.Month);
                            if (lastMonthChanges.Count() > 0)
                            {
                                MonthlyPrices.Add(new PriceUpdate(new DateTime(now.Year, now.Month, 1), lastMonthChanges.Last().Value, "test"));
                            }

                            var currentMonthPrices = m_allPriceUpdates.Where(date => date.Key.Year == now.Year && date.Key.Month == now.Month);
                            if (currentMonthPrices.Count() > 0)
                            {
                                foreach (KeyValuePair <DateTime, double> pair in currentMonthPrices)
                                {
                                    MonthlyPrices.Add(new PriceUpdate(new DateTime(pair.Key.Year, pair.Key.Month, pair.Key.Day), pair.Value, "test"));
                                }
                            }

                            if (currentMonthPrices.Count() > 0)
                            {
                                MonthlyPrices.Add(new PriceUpdate(new DateTime(now.Year, now.Month, now.Day), currentMonthPrices.Last().Value, "test"));
                            }
                        }
                    }
                }
            }
        }