コード例 #1
0
        public BooksDelta(DateTime date, DateTime startDate)
        {
            Date      = date;
            StartDate = startDate;
            TimeSpan ts = date - startDate;

            DaysSinceStart  = ts.Days;
            BooksReadToDate = new List <BookRead>();

            OverallTally = new DeltaTally();
            LastTenTally = new DeltaTally();
        }
コード例 #2
0
        public void UpdateTallies()
        {
            // start with the overall
            OverallTally = GetDeltaTallyValues(BooksReadToDate);

            // then get the last 10 books in a list
            List <BookRead> lastTenBooks = new List <BookRead>();

            if (BooksReadToDate.Count <= 10)
            {
                lastTenBooks = BooksReadToDate;
            }
            else
            {
                for (int i = BooksReadToDate.Count - 10; i < BooksReadToDate.Count; i++)
                {
                    lastTenBooks.Add(BooksReadToDate[i]);
                }
            }

            // then update the tally based on that list
            LastTenTally = GetDeltaTallyValues(lastTenBooks);
        }
コード例 #3
0
        private DeltaTally GetDeltaTallyValues(List <BookRead> books)
        {
            DeltaTally tally            = new DeltaTally();
            UInt32     totalBooks       = 0;
            UInt32     totalPagesRead   = 0;
            UInt32     totalBookFormat  = 0;
            UInt32     totalComicFormat = 0;
            UInt32     totalAudioFormat = 0;
            UInt32     totalInEnglish   = 0;
            int        daysInTally;

            daysInTally = (books.Last().Date - books.First().Date).Days;
            if (daysInTally < 1)
            {
                daysInTally = 1;
            }

            Dictionary <string, Tuple <UInt32, UInt32> > languageCounts =
                new Dictionary <string, Tuple <UInt32, UInt32> >();
            Dictionary <string, Tuple <UInt32, UInt32> > countryCounts =
                new Dictionary <string, Tuple <UInt32, UInt32> >();

            foreach (var book in books)
            {
                totalBooks++;
                totalPagesRead += book.Pages;
                if (book.Format == BookFormat.Book)
                {
                    totalBookFormat++;
                }
                if (book.Format == BookFormat.Comic)
                {
                    totalComicFormat++;
                }
                if (book.Format == BookFormat.Audio)
                {
                    totalAudioFormat++;
                }
                if (book.OriginalLanguage == "English")
                {
                    totalInEnglish++;
                }
                UpdateLanguageAndCountryCounts(languageCounts, countryCounts, book);
            }

            double percentageInEnglish = GetAsPercentage(totalBooks, totalInEnglish);

            tally.DaysInTally         = daysInTally;
            tally.TotalPages          = totalPagesRead;
            tally.TotalBooks          = totalBooks;
            tally.TotalBookFormat     = totalBookFormat;
            tally.TotalComicFormat    = totalComicFormat;
            tally.TotalAudioFormat    = totalAudioFormat;
            tally.PercentageInEnglish = percentageInEnglish;

            foreach (string language in languageCounts.Keys)
            {
                tally.LanguageTotals.Add(
                    new Tuple <string, UInt32, double, UInt32, double>(
                        language, languageCounts[language].Item1,
                        GetAsPercentage(totalBooks, languageCounts[language].Item1),
                        languageCounts[language].Item2,
                        GetAsPercentage(totalPagesRead, languageCounts[language].Item2))
                    );
            }

            foreach (string country in countryCounts.Keys)
            {
                tally.CountryTotals.Add(
                    new Tuple <string, UInt32, double, UInt32, double>(
                        country, countryCounts[country].Item1,
                        GetAsPercentage(totalBooks, countryCounts[country].Item1),
                        countryCounts[country].Item2,
                        GetAsPercentage(totalPagesRead, countryCounts[country].Item2))
                    );
            }

            return(tally);
        }