static void InsertBallotPaper(BallotPaper ballotPaper) { string sql = "insert into BallotPapers ( [Id] ) values( @ballotPaperId )"; using (SqlConnection cnn = new SqlConnection(ConnectionString)) { try { cnn.Open(); using (SqlCommand cmd = new SqlCommand(sql, cnn)) { cmd.Parameters.Add("@ballotPaperId", SqlDbType.Int).Value = ballotPaper.BallotPaperId; int rowsAdded = cmd.ExecuteNonQuery(); if (rowsAdded > 0) { rowCount++; // MessageBox.Show("Row inserted!!" + ""); } else { MessageBox.Show("No row inserted"); } } } catch (Exception ex) { MessageBox.Show("database ERROR:" + ex.Message + "qq " + ballotPaper.BallotPaperId + " ee"); } } foreach (Vote vote in ballotPaper.Votes) { InsertVote(vote, ballotPaper.BallotPaperId); } }
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); }
private void Btn_RemoveVote(object sender, RoutedEventArgs e) { // MessageBox.Show("You said: " + "Btn_Gen_vote: " + ""); BallotPaper b = (BallotPaper)Lsb_Votes.SelectedItem; ContestCurrent.BallotPapers.Remove(b); Lsb_Votes.Items.Refresh(); }
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); } } }
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); }
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(); }
public void gotATransfer(BallotPaper transfer) { Transfers.Add(transfer); }
public void AddBallotPaper(BallotPaper ballotPaper) { BallotPapers.Add(ballotPaper); }