From e45e5784b0ed01dd89723d83ad0996d05fd537cd Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:06:14 -0600 Subject: [PATCH 01/24] Initial Commit --- spec/candidate_spec.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 spec/candidate_spec.rb diff --git a/spec/candidate_spec.rb b/spec/candidate_spec.rb new file mode 100644 index 0000000..e69de29 From 861ac647eca6f9da2d2902d72d6856175f54244e Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:10:33 -0600 Subject: [PATCH 02/24] Add initialize function to democrat --- lib/candidate.rb | 6 ++++++ spec/candidate_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/candidate.rb b/lib/candidate.rb index a847ba9..5bc5c04 100644 --- a/lib/candidate.rb +++ b/lib/candidate.rb @@ -1,2 +1,8 @@ class Candidate + attr_reader :name, :party + + def initialize(info_hash) + @name = info_hash[:name] + @party = info_hash[:party] + end end diff --git a/spec/candidate_spec.rb b/spec/candidate_spec.rb index e69de29..9a5571e 100644 --- a/spec/candidate_spec.rb +++ b/spec/candidate_spec.rb @@ -0,0 +1,22 @@ +require 'rspec' +require './lib/candidate' + +describe Candidate do + before do + @diana = Candidate.new({name: "Diana D", party: :democrat}) + end + + describe '#initialize' do + it 'exists' do + expect(@diana).to be_a(Candidate) + end + + it 'has a name' do + expect(@diana.name).to eq("Diana D") + end + + it 'has a party' do + expect(@diana.party).to eq(:democrat) + end + end +end \ No newline at end of file From 8f8c95166ea646587d0bedb627053e8a7b6ceecb Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:11:35 -0600 Subject: [PATCH 03/24] Add votes attribute --- lib/candidate.rb | 4 +++- spec/candidate_spec.rb | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/candidate.rb b/lib/candidate.rb index 5bc5c04..16eb1e1 100644 --- a/lib/candidate.rb +++ b/lib/candidate.rb @@ -1,8 +1,10 @@ class Candidate - attr_reader :name, :party + attr_reader :name, :party, :votes def initialize(info_hash) @name = info_hash[:name] @party = info_hash[:party] + + @votes = 0 end end diff --git a/spec/candidate_spec.rb b/spec/candidate_spec.rb index 9a5571e..111e39d 100644 --- a/spec/candidate_spec.rb +++ b/spec/candidate_spec.rb @@ -18,5 +18,9 @@ it 'has a party' do expect(@diana.party).to eq(:democrat) end + + it 'starts with 0 votes' do + expect(@diana.votes).to eq(0) + end end end \ No newline at end of file From 4088d7ecfda1a76344b1cca8f155a29f29e8fc1a Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:13:17 -0600 Subject: [PATCH 04/24] Add vote_for method to candidate --- lib/candidate.rb | 4 ++++ spec/candidate_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/candidate.rb b/lib/candidate.rb index 16eb1e1..6fca2a7 100644 --- a/lib/candidate.rb +++ b/lib/candidate.rb @@ -7,4 +7,8 @@ def initialize(info_hash) @votes = 0 end + + def vote_for + @votes += 1 + end end diff --git a/spec/candidate_spec.rb b/spec/candidate_spec.rb index 111e39d..fd1e26e 100644 --- a/spec/candidate_spec.rb +++ b/spec/candidate_spec.rb @@ -23,4 +23,12 @@ expect(@diana.votes).to eq(0) end end + + describe '#vote_for' do + it 'can be voted for' do + 3.times { @diana.vote_for } + + expect(@diana.votes).to eq(3) + end + end end \ No newline at end of file From 71b63daa67c2c427266579e247e981feaaf53ef6 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:14:29 -0600 Subject: [PATCH 05/24] Add extra test to vote_for method --- spec/candidate_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/candidate_spec.rb b/spec/candidate_spec.rb index fd1e26e..9b4da7a 100644 --- a/spec/candidate_spec.rb +++ b/spec/candidate_spec.rb @@ -29,6 +29,10 @@ 3.times { @diana.vote_for } expect(@diana.votes).to eq(3) + + @diana.vote_for + + expect(@diana.votes).to eq(4) end end end \ No newline at end of file From 32da88c37dd8556632dcadfbd43d1de844a341b1 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:17:48 -0600 Subject: [PATCH 06/24] Initial Commit --- spec/race_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spec/race_spec.rb diff --git a/spec/race_spec.rb b/spec/race_spec.rb new file mode 100644 index 0000000..f4b09b4 --- /dev/null +++ b/spec/race_spec.rb @@ -0,0 +1,7 @@ +require 'rspec' +require './lib/race' +require './lib/candidate' + +describe Race do + +end \ No newline at end of file From c2955298a2a28702fcef066b9e18915f3d076d4c Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:19:47 -0600 Subject: [PATCH 07/24] Add initialize function to race class --- lib/race.rb | 3 +++ spec/race_spec.rb | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/race.rb b/lib/race.rb index b2843fa..2cc1cc5 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -1,2 +1,5 @@ class Race + def initialize(office) + + end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index f4b09b4..f9fea89 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -3,5 +3,13 @@ require './lib/candidate' describe Race do + before do + @race = Race.new("Texas Governor") + end + describe '#initialze' do + it 'exists' do + expect(@race).to be_a(Race) + end + end end \ No newline at end of file From a58803fa47da17389d741ce7bcb427d73eeb6c97 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:21:00 -0600 Subject: [PATCH 08/24] Add office attribute --- lib/race.rb | 4 +++- spec/race_spec.rb | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/race.rb b/lib/race.rb index 2cc1cc5..a6b6a99 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -1,5 +1,7 @@ class Race + attr_reader :office + def initialize(office) - + @office = office end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index f9fea89..729cdd7 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -11,5 +11,9 @@ it 'exists' do expect(@race).to be_a(Race) end + + it 'has an office' do + expect(@race.office).to eq("Texas Governor") + end end end \ No newline at end of file From 883d8a3f5d2bf52c1e904bd057c9b12a53291e90 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:22:01 -0600 Subject: [PATCH 09/24] Add candidates attribute --- lib/race.rb | 3 ++- spec/race_spec.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/race.rb b/lib/race.rb index a6b6a99..435e9f2 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -1,7 +1,8 @@ class Race - attr_reader :office + attr_reader :office, :candidates def initialize(office) @office = office + @candidates = [] end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index 729cdd7..9297f4f 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -15,5 +15,9 @@ it 'has an office' do expect(@race.office).to eq("Texas Governor") end + + it 'begins with no candidates' do + expect(@race.candidates).to eq([]) + end end end \ No newline at end of file From ace082a6f2e7824949ce6e6f418d5d821eaa6cdb Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:25:35 -0600 Subject: [PATCH 10/24] Add register_candidate method --- lib/race.rb | 4 ++++ spec/race_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/race.rb b/lib/race.rb index 435e9f2..f88f9c0 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -5,4 +5,8 @@ def initialize(office) @office = office @candidates = [] end + + def register_candidate(info) + Candidate.new(info) + end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index 9297f4f..86799fc 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -20,4 +20,14 @@ expect(@race.candidates).to eq([]) end end + + describe '#register_candidate' do + it 'can register a candidate' do + candidate1 = @race.register_candidate({name: "Diana D", party: :democrat}) + + expect(candidate1).to be_a(Candidate) + expect(candidate1.name).to eq("Diana D") + expect(candidate1.party).to eq(:democrat) + end + end end \ No newline at end of file From ff8f631d042461cd5437df713db7528f70ee9a0a Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:28:06 -0600 Subject: [PATCH 11/24] Change register_candidate to add candidates to array --- lib/race.rb | 4 +++- spec/race_spec.rb | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/race.rb b/lib/race.rb index f88f9c0..8ddb688 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -7,6 +7,8 @@ def initialize(office) end def register_candidate(info) - Candidate.new(info) + candidate = Candidate.new(info) + @candidates << candidate + candidate end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index 86799fc..50e5564 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -29,5 +29,12 @@ expect(candidate1.name).to eq("Diana D") expect(candidate1.party).to eq(:democrat) end + + it 'can store several candidates' do + candidate1 = @race.register_candidate({name: "Diana D", party: :democrat}) + candidate2 = @race.register_candidate({name: "Roberto R", party: :republican}) + + expect(@race.candidates).to eq([candidate1, candidate2]) + end end end \ No newline at end of file From 1763a35e5303ebd3989c4906ec0a53d4ee787f5d Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:30:31 -0600 Subject: [PATCH 12/24] Initital Commit --- spec/election_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 spec/election_spec.rb diff --git a/spec/election_spec.rb b/spec/election_spec.rb new file mode 100644 index 0000000..cc9733c --- /dev/null +++ b/spec/election_spec.rb @@ -0,0 +1,8 @@ +require 'rspec' +require './lib/candidate' +require './lib/race' +require './lib/election' + +describe Election do + +end \ No newline at end of file From 5e5d118d1fb7ed213332a5e88f7530d841e767a1 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:32:24 -0600 Subject: [PATCH 13/24] Add initialze method to election --- lib/election.rb | 3 +++ spec/election_spec.rb | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/election.rb b/lib/election.rb index c756dc9..c5a6be3 100644 --- a/lib/election.rb +++ b/lib/election.rb @@ -1,2 +1,5 @@ class Election + def initialize(year) + + end end diff --git a/spec/election_spec.rb b/spec/election_spec.rb index cc9733c..d332114 100644 --- a/spec/election_spec.rb +++ b/spec/election_spec.rb @@ -4,5 +4,13 @@ require './lib/election' describe Election do - + before do + @election = Election.new(2024) + end + + describe '#initialze' do + it 'exists' do + expect(@election).to be_a(Election) + end + end end \ No newline at end of file From b056c377e92b394dd8876d7ab88a8b51deb7e660 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:33:19 -0600 Subject: [PATCH 14/24] Add year attribute --- lib/election.rb | 4 +++- spec/election_spec.rb | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/election.rb b/lib/election.rb index c5a6be3..d2c413a 100644 --- a/lib/election.rb +++ b/lib/election.rb @@ -1,5 +1,7 @@ class Election + attr_reader :year + def initialize(year) - + @year = year end end diff --git a/spec/election_spec.rb b/spec/election_spec.rb index d332114..f115ccf 100644 --- a/spec/election_spec.rb +++ b/spec/election_spec.rb @@ -12,5 +12,9 @@ it 'exists' do expect(@election).to be_a(Election) end + + it 'has a year' do + expect(@election.year).to eq(2024) + end end end \ No newline at end of file From 335afc1687936d96c544ee0aa913d9c57622c1ab Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:34:33 -0600 Subject: [PATCH 15/24] Add races attribute --- lib/election.rb | 3 ++- spec/election_spec.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/election.rb b/lib/election.rb index d2c413a..8aea21c 100644 --- a/lib/election.rb +++ b/lib/election.rb @@ -1,7 +1,8 @@ class Election - attr_reader :year + attr_reader :year, :races def initialize(year) @year = year + @races = [] end end diff --git a/spec/election_spec.rb b/spec/election_spec.rb index f115ccf..c45ce21 100644 --- a/spec/election_spec.rb +++ b/spec/election_spec.rb @@ -16,5 +16,9 @@ it 'has a year' do expect(@election.year).to eq(2024) end + + it 'begins with no races' do + expect(@election.races).to eq([]) + end end end \ No newline at end of file From 01c1a13f2485ad9916f05f7405bf60bfb81f1dc5 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:36:31 -0600 Subject: [PATCH 16/24] Add add_race method --- lib/election.rb | 4 ++++ spec/election_spec.rb | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/election.rb b/lib/election.rb index 8aea21c..79f122d 100644 --- a/lib/election.rb +++ b/lib/election.rb @@ -5,4 +5,8 @@ def initialize(year) @year = year @races = [] end + + def add_race(race) + @races << race + end end diff --git a/spec/election_spec.rb b/spec/election_spec.rb index c45ce21..9dba5fa 100644 --- a/spec/election_spec.rb +++ b/spec/election_spec.rb @@ -6,6 +6,7 @@ describe Election do before do @election = Election.new(2024) + @race = Race.new("Texas Governor") end describe '#initialze' do @@ -21,4 +22,10 @@ expect(@election.races).to eq([]) end end + + describe '#add_race' do + it 'can add a race' do + expect(@election.add_race(@race)).to eq([@race]) + end + end end \ No newline at end of file From e2878f4f1ebe072c0dcec01d956c647a10c9e984 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:46:19 -0600 Subject: [PATCH 17/24] Add candidates method --- lib/election.rb | 4 ++++ spec/election_spec.rb | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/election.rb b/lib/election.rb index 79f122d..2e826fd 100644 --- a/lib/election.rb +++ b/lib/election.rb @@ -9,4 +9,8 @@ def initialize(year) def add_race(race) @races << race end + + def candidates + @races.map { |race| race.candidates }.flatten + end end diff --git a/spec/election_spec.rb b/spec/election_spec.rb index 9dba5fa..6d3ac12 100644 --- a/spec/election_spec.rb +++ b/spec/election_spec.rb @@ -6,7 +6,8 @@ describe Election do before do @election = Election.new(2024) - @race = Race.new("Texas Governor") + @race1 = Race.new("Texas Governor") + @race2 = Race.new("United States President") end describe '#initialze' do @@ -25,7 +26,23 @@ describe '#add_race' do it 'can add a race' do - expect(@election.add_race(@race)).to eq([@race]) + expect(@election.add_race(@race1)).to eq([@race1]) + expect(@election.add_race(@race2)).to eq([@race1, @race2]) + end + end + + describe '#candidates' do + it 'can list runnung candidates' do + prez_candidate1 = @race2.register_candidate({name: "Joe B", party: :democrat}) + prez_candidate2 = @race2.register_candidate({name: "Donald T", party: :republican}) + + candidate1 = @race1.register_candidate({name: "Diana D", party: :democrat}) + candidate2 = @race1.register_candidate({name: "Roberto R", party: :republican}) + + @election.add_race(@race2) + @election.add_race(@race1) + + expect(@election.candidates).to eq([prez_candidate1, prez_candidate2, candidate1, candidate2]) end end end \ No newline at end of file From 18a60ab479a73b89c513e2cc7c7f539b740e51f9 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:55:47 -0600 Subject: [PATCH 18/24] Add vote_counts method --- lib/election.rb | 6 ++++++ spec/election_spec.rb | 27 ++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/election.rb b/lib/election.rb index 2e826fd..d106079 100644 --- a/lib/election.rb +++ b/lib/election.rb @@ -13,4 +13,10 @@ def add_race(race) def candidates @races.map { |race| race.candidates }.flatten end + + def vote_counts + votes = {} + candidates.each { |candidate| votes[candidate.name] = candidate.votes } + votes + end end diff --git a/spec/election_spec.rb b/spec/election_spec.rb index 6d3ac12..be66044 100644 --- a/spec/election_spec.rb +++ b/spec/election_spec.rb @@ -6,8 +6,15 @@ describe Election do before do @election = Election.new(2024) + @race1 = Race.new("Texas Governor") @race2 = Race.new("United States President") + + @prez_candidate1 = @race2.register_candidate({name: "Joe B", party: :democrat}) + @prez_candidate2 = @race2.register_candidate({name: "Donald T", party: :republican}) + + @candidate1 = @race1.register_candidate({name: "Diana D", party: :democrat}) + @candidate2 = @race1.register_candidate({name: "Roberto R", party: :republican}) end describe '#initialze' do @@ -33,16 +40,26 @@ describe '#candidates' do it 'can list runnung candidates' do - prez_candidate1 = @race2.register_candidate({name: "Joe B", party: :democrat}) - prez_candidate2 = @race2.register_candidate({name: "Donald T", party: :republican}) + @election.add_race(@race2) + @election.add_race(@race1) - candidate1 = @race1.register_candidate({name: "Diana D", party: :democrat}) - candidate2 = @race1.register_candidate({name: "Roberto R", party: :republican}) + expect(@election.candidates).to eq([@prez_candidate1, @prez_candidate2, @candidate1, @candidate2]) + end + end + describe '#vote_counts' do + it 'can count votes' do @election.add_race(@race2) @election.add_race(@race1) - expect(@election.candidates).to eq([prez_candidate1, prez_candidate2, candidate1, candidate2]) + 4.times { @election.candidates.each(&:vote_for) } + + votes = @election.vote_counts + + expect(votes["Donald T"]).to eq(4) + expect(votes["Joe B"]).to eq(4) + expect(votes["Diana D"]).to eq(4) + expect(votes["Roberto R"]).to eq(4) end end end \ No newline at end of file From 0dd196747e593548f3cfe07a9e5de6fb9393857e Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 10:59:27 -0600 Subject: [PATCH 19/24] Add open attribute --- lib/race.rb | 5 +++++ spec/race_spec.rb | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lib/race.rb b/lib/race.rb index 8ddb688..99827b3 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -4,6 +4,7 @@ class Race def initialize(office) @office = office @candidates = [] + @open = true end def register_candidate(info) @@ -11,4 +12,8 @@ def register_candidate(info) @candidates << candidate candidate end + + def open? + @open + end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index 50e5564..d3de9a2 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -19,6 +19,10 @@ it 'begins with no candidates' do expect(@race.candidates).to eq([]) end + + it 'begins open' do + expect(@race.open?).to eq(true) + end end describe '#register_candidate' do From 3b839d77ad933018da94ce0d6566c0b95dd5a87d Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 11:00:39 -0600 Subject: [PATCH 20/24] add close! method --- lib/race.rb | 4 ++++ spec/race_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/race.rb b/lib/race.rb index 99827b3..91d2930 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -16,4 +16,8 @@ def register_candidate(info) def open? @open end + + def close! + @open = false + end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index d3de9a2..64f0023 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -41,4 +41,12 @@ expect(@race.candidates).to eq([candidate1, candidate2]) end end + + describe '#close!' do + it 'can close' do + @race.close! + + expect(@race.open?).to eq(false) + end + end end \ No newline at end of file From e49e5a5623087521c602afcd8e57fa0577d2fd2e Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 11:13:27 -0600 Subject: [PATCH 21/24] Add winner method functionality --- lib/race.rb | 9 +++++++++ spec/race_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/race.rb b/lib/race.rb index 91d2930..de999f5 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -20,4 +20,13 @@ def open? def close! @open = false end + + def winner + return false if @open + + winners = @candidates.find_all { |candidate| candidate.votes == @candidates.max_by(&:votes).votes } + + return winners[0] if winners.size == 1 + return winners + end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index 64f0023..6e96586 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -49,4 +49,31 @@ expect(@race.open?).to eq(false) end end + + describe '#winner' do + it 'returns false if race is open' do + expect(@race.winner).to eq(false) + end + + it 'returns the winner of a closed race' do + candidate1 = @race.register_candidate({name: "Diana D", party: :democrat}) + candidate2 = @race.register_candidate({name: "Roberto R", party: :republican}) + + candidate1.vote_for + @race.close! + + expect(@race.winner).to eq(candidate1) + end + + it 'returns multiple winners if there is a tie' do + candidate1 = @race.register_candidate({name: "Diana D", party: :democrat}) + candidate2 = @race.register_candidate({name: "Roberto R", party: :republican}) + + candidate1.vote_for + candidate2.vote_for + @race.close! + + expect(@race.winner).to eq([candidate1, candidate2]) + end + end end \ No newline at end of file From ad80fcbbbdae175e3a4f36f8ec504e0a47e6c04a Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 11:15:53 -0600 Subject: [PATCH 22/24] Add tie? method --- lib/race.rb | 4 ++++ spec/race_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/race.rb b/lib/race.rb index de999f5..94c71f8 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -29,4 +29,8 @@ def winner return winners[0] if winners.size == 1 return winners end + + def tie? + winner.class == Array + end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index 6e96586..f7aab79 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -76,4 +76,27 @@ expect(@race.winner).to eq([candidate1, candidate2]) end end + + describe '#tie?' do + it 'can determine if there is a tie' do + candidate1 = @race.register_candidate({name: "Diana D", party: :democrat}) + candidate2 = @race.register_candidate({name: "Roberto R", party: :republican}) + + candidate1.vote_for + candidate2.vote_for + @race.close! + + expect(@race.tie?).to eq(true) + end + + it 'can determine if there is not a tie' do + candidate1 = @race.register_candidate({name: "Diana D", party: :democrat}) + candidate2 = @race.register_candidate({name: "Roberto R", party: :republican}) + + candidate1.vote_for + @race.close! + + expect(@race.tie?).to eq(false) + end + end end \ No newline at end of file From 41a349e7d226e0a9183a98003ced6b17c26d8e8f Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 11:28:14 -0600 Subject: [PATCH 23/24] Add winner method --- lib/election.rb | 4 ++++ spec/election_spec.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/election.rb b/lib/election.rb index d106079..012f4af 100644 --- a/lib/election.rb +++ b/lib/election.rb @@ -19,4 +19,8 @@ def vote_counts candidates.each { |candidate| votes[candidate.name] = candidate.votes } votes end + + def winners + @races.find_all { |race| race.winner.class == Candidate }.map(&:winner) + end end diff --git a/spec/election_spec.rb b/spec/election_spec.rb index be66044..5e39242 100644 --- a/spec/election_spec.rb +++ b/spec/election_spec.rb @@ -62,4 +62,19 @@ expect(votes["Roberto R"]).to eq(4) end end + + describe '#winners' do + it 'can returns the winners of all races' do + @election.add_race(@race2) + @election.add_race(@race1) + @election.add_race(Race.new("race")) + + @prez_candidate1.vote_for + + @race1.close! + @race2.close! + + expect(@election.winners).to eq([@prez_candidate1]) + end + end end \ No newline at end of file From d1874474126adcebcbd9dd36ac8b1a7e386320f9 Mon Sep 17 00:00:00 2001 From: Sebastian McKee Date: Tue, 7 Jan 2025 12:15:36 -0600 Subject: [PATCH 24/24] Change wimmer method to ouput only a songle candidate --- lib/election.rb | 2 +- lib/race.rb | 5 ++--- spec/race_spec.rb | 11 ----------- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/lib/election.rb b/lib/election.rb index 012f4af..a9c7faa 100644 --- a/lib/election.rb +++ b/lib/election.rb @@ -21,6 +21,6 @@ def vote_counts end def winners - @races.find_all { |race| race.winner.class == Candidate }.map(&:winner) + @races.find_all { |race| !race.tie? && !race.open? }.map(&:winner) end end diff --git a/lib/race.rb b/lib/race.rb index 94c71f8..a3fb2c8 100644 --- a/lib/race.rb +++ b/lib/race.rb @@ -26,11 +26,10 @@ def winner winners = @candidates.find_all { |candidate| candidate.votes == @candidates.max_by(&:votes).votes } - return winners[0] if winners.size == 1 - return winners + return winners[0] end def tie? - winner.class == Array + @candidates.find_all { |candidate| candidate.votes == @candidates.max_by(&:votes).votes }.size > 1 end end diff --git a/spec/race_spec.rb b/spec/race_spec.rb index f7aab79..c00334d 100644 --- a/spec/race_spec.rb +++ b/spec/race_spec.rb @@ -64,17 +64,6 @@ expect(@race.winner).to eq(candidate1) end - - it 'returns multiple winners if there is a tie' do - candidate1 = @race.register_candidate({name: "Diana D", party: :democrat}) - candidate2 = @race.register_candidate({name: "Roberto R", party: :republican}) - - candidate1.vote_for - candidate2.vote_for - @race.close! - - expect(@race.winner).to eq([candidate1, candidate2]) - end end describe '#tie?' do