コード例 #1
0
        /// <summary>
        /// Gets the result and displays these results to the user
        /// </summary>
        /// <param name="sender">The handle to the button</param>
        /// <param name="e">The extra messages</param>
        private void CountVotesButton_Click(object sender, EventArgs e)
        {
            // Make sure there's data to calculate
            if (this.VotesGridView.Columns.Count > 1)
            {
                // Reset the lists to remove any existing data
                allVotes   = new VotesList();
                candidates = new List <string>();

                // Goes through and adds the candidates
                foreach (DataGridViewColumn col in this.VotesGridView.Columns)
                {
                    candidates.Add(col.Name);
                }

                // Goes through and adds the votes
                for (int i = 0; i < VotesGridView.Rows.Count - 1; i++)
                {
                    Vote vote = new Vote(); // Holds the current vote

                    // Goes through each cell and matches the candidate with the vote
                    foreach (DataGridViewCell cell in VotesGridView.Rows[i].Cells)
                    {
                        // If the cell is empty, then add a -1, this is important for counting invalid votes
                        if (cell.Value == null)
                        {
                            vote.Add(VotesGridView.Columns[cell.ColumnIndex].Name, -1);
                        }
                        else
                        {
                            int n;

                            // Checks for bad input
                            if (int.TryParse(cell.Value.ToString(), out n))
                            {
                                vote.Add(VotesGridView.Columns[cell.ColumnIndex].Name, n);
                            }

                            // If there's bad input
                            else
                            {
                                MessageBox.Show("Value must be a number");
                                return;
                            }
                        }
                    }

                    // Add the vote
                    allVotes.Add(vote);
                }

                Result finalResult = allVotes.calculateResult(candidates);  // Calculate the result

                // Checks for a result
                if (finalResult != null)
                {
                    // Displays the number of invalid votes
                    this.InvalidVotesLabel.Text = "" + finalResult.NumberOfInvalidVotes;

                    string winnerText = ""; // The text that displays which candidate won the vote

                    // If there's won winner, add the candidate to the string
                    if (finalResult.Winners.Count == 1)
                    {
                        winnerText = finalResult.Winners[0];
                    }

                    // If there's a tie, add each candidate to the string with corresponding English grammar
                    else
                    {
                        for (int i = 0; i < finalResult.Winners.Count; i++)
                        {
                            if (i == finalResult.Winners.Count - 1)
                            {
                                winnerText += finalResult.Winners[i] + " tie";
                            }
                            else
                            {
                                winnerText += finalResult.Winners[i] + " and ";
                            }
                        }
                    }

                    // Display the winner(s) and the chart
                    WinnerLabel.Text = winnerText;
                    Chart chart = new Chart(finalResult);
                    chart.ShowDialog();
                }
            }

            // If there isn't enough candidates
            else
            {
                MessageBox.Show("Must have at least 2 candidates", "Error");
            }
        }
コード例 #2
0
        public void Add <T>(T model)
        {
            var theType = model.GetType().Name;

            switch (theType)
            {
            case "User": UsersList.Add(model as User); return;

            case "News": NewsesList.Add(model as Dal.Models.News); return;

            case "Category": CategoriesList.Add(model as Category); return;

            case "Magazine": MagazinesList.Add(model as Magazine); return;

            case "SmartLink": SmartLinksList.Add(model as SmartLink); return;

            case "Comment": CommentsList.Add(model as Comment); return;

            case "Visit": VisitsList.Add(model as Visit); return;

            case "NewsLetter": NewsLettersList.Add(model as NewsLetter); return;

            case "Slider": SlidersList.Add(model as Slider); return;

            case "Slide": SlidesList.Add(model as Slide); return;

            case "UserMagazine": UserMagazinesList.Add(model as UserMagazine); return;

            case "State": StatesList.Add(model as State); return;

            case "City": CitiesList.Add(model as City); return;

            case "Media": MediasList.Add(model as Media); return;

            case "Report": ReportsList.Add(model as Report); return;

            case "Vote": VotesList.Add(model as Vote); return;

            case "Advertise": AdvertisesList.Add(model as Advertise); return;

            case "AdCategory": AdCategoriesList.Add(model as AdCategory); return;

            case "Galery": GaleriesList.Add(model as Galery); return;

            case "GaleryImage": GaleryImagesList.Add(model as GaleryImage); return;

            case "Image": ImagesList.Add(model as Image); return;

            case "KeyPoint": KeyPointsList.Add(model as KeyPoint); return;

            case "KeyPointsContainer": KeyPointsContainersList.Add(model as KeyPointsContainer); return;

            case "Quiz": QuizesList.Add(model as Quiz); return;

            case "Question": QuestionsList.Add(model as Question); return;

            case "Answer": AnswersList.Add(model as Answer); return;

            case "Option": OptionList.Add(model as Option); return;

            case "ItemList": ItemsListList.Add(model as ItemList); return;

            default: throw new Exception("The type " + theType + " is not supported.");
            }
        }