public static void AddSomeVotes(Contest ContestEx, int prefsToAdd) { List <int> prefs = new List <int>(); BallotPaper b; b = new BallotPaper(); for (int j = 0; j < ContestEx.Candidates.Count(); j++) { prefs.Add(j); } // prefs = [ 0, 1, 2 ] ShuffleList(prefs); // prefs = [ 2, 0, 1 ] for (int j = 0; j < prefsToAdd; j++) { // j = 0 , 1, 2 b.AddVote(new Vote(ContestEx.Candidates[prefs[j]], j + 1)); } b.Votes.Sort(); ContestEx.AddBallotPaper(b); }
public static Contest ExampleContest1() { Contest ContestEx = new Contest(2); ContestEx.AddCandidate(new Candidate("John")); ContestEx.AddCandidate(new Candidate("Mary")); ContestEx.AddCandidate(new Candidate("Dan ")); BallotPaper b; b = new BallotPaper(); b.AddVote(new Vote(ContestEx.Candidates[0], 1)); b.AddVote(new Vote(ContestEx.Candidates[1], 2)); b.AddVote(new Vote(ContestEx.Candidates[2], 3)); b.Votes.Sort(); ContestEx.AddBallotPaper(b); return(ContestEx); }
private void Btn_LoadJson_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == true) { string json = File.ReadAllText(openFileDialog.FileName); // MessageBox.Show("You said: " + " Btn_Save3: " + json + " " + openFileDialog.FileName); try { MainWindow.ContestCurrent = JsonConvert.DeserializeObject <Contest>(json); // the json imports, // but the objects are not properly related // so they have to be matched by Id and added to a new list List <BallotPaper> ballotPapers = new List <BallotPaper>(); foreach (BallotPaper b in MainWindow.ContestCurrent.BallotPapers) { BallotPaper bNew = new BallotPaper(); foreach (Vote v in b.Votes) { Candidate c = MainWindow.ContestCurrent.GetCandidateById(v.Candidate.CandidateId); Vote vNew = new Vote(c, v.Preference); bNew.AddVote(vNew); } ballotPapers.Add(bNew); } MainWindow.ContestCurrent.BallotPapers = ballotPapers; MainWindow.Txb_Seats.Text = MainWindow.ContestCurrent.Seats + ""; MainWindow.Lsb_Candidates.ItemsSource = MainWindow.ContestCurrent.Candidates; MainWindow.Lsb_Candidates.Items.Refresh(); MainWindow.Lsb_Votes.ItemsSource = MainWindow.ContestCurrent.BallotPapers; MainWindow.Lsb_Votes.Items.Refresh(); } catch (Exception ee) { MessageBox.Show("" + " error decoding file: " + ee.Message); } } }
static BallotPaper SelectVote(Contest contest, int ballotPaperId) { string sql = "Select CandidateId, VotePreference from Votes where BallotPaperId = @BallotPaperId "; try { BallotPaper ballotPaper = new BallotPaper(); using (SqlConnection cnn = new SqlConnection(ConnectionString)) { cnn.Open(); using (SqlCommand cmd = new SqlCommand(sql, cnn)) { cmd.Parameters.Add("@BallotPaperId", SqlDbType.Int).Value = ballotPaperId; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read() != false) { int candidateId = (int)reader["CandidateId"]; int votePreference = (int)reader["VotePreference"]; Candidate c = contest.GetCandidateById(candidateId); Vote v = new Vote(c, votePreference); ballotPaper.AddVote(v); rowCount++; } } } } return(ballotPaper); } catch (Exception ee) { MessageBox.Show("err: " + ee.Message); } return(null); }
private void Btn_Cast_Click(object sender, RoutedEventArgs e) { if (VotedCandidates.Count() < 1) { return; } BallotPaper b; b = new BallotPaper(); int xPref = 1; foreach (Candidate x in VotedCandidates) { b.AddVote(new Vote(x, xPref)); xPref++; } b.Votes.Sort(); MainWindow.ContestCurrent.AddBallotPaper(b); MainWindow.Lsb_Votes.Items.Refresh(); }