Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sebastian IC week 4 #3

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e45e578
Initial Commit
0nehundr3d Jan 7, 2025
861ac64
Add initialize function to democrat
0nehundr3d Jan 7, 2025
8f8c951
Add votes attribute
0nehundr3d Jan 7, 2025
4088d7e
Add vote_for method to candidate
0nehundr3d Jan 7, 2025
71b63da
Add extra test to vote_for method
0nehundr3d Jan 7, 2025
342a17b
Merge pull request #1 from 0nehundr3d/candidate
0nehundr3d Jan 7, 2025
32da88c
Initial Commit
0nehundr3d Jan 7, 2025
c295529
Add initialize function to race class
0nehundr3d Jan 7, 2025
a58803f
Add office attribute
0nehundr3d Jan 7, 2025
883d8a3
Add candidates attribute
0nehundr3d Jan 7, 2025
ace082a
Add register_candidate method
0nehundr3d Jan 7, 2025
ff8f631
Change register_candidate to add candidates to array
0nehundr3d Jan 7, 2025
0354760
Merge pull request #2 from 0nehundr3d/race
0nehundr3d Jan 7, 2025
1763a35
Initital Commit
0nehundr3d Jan 7, 2025
5e5d118
Add initialze method to election
0nehundr3d Jan 7, 2025
b056c37
Add year attribute
0nehundr3d Jan 7, 2025
335afc1
Add races attribute
0nehundr3d Jan 7, 2025
01c1a13
Add add_race method
0nehundr3d Jan 7, 2025
e2878f4
Add candidates method
0nehundr3d Jan 7, 2025
18a60ab
Add vote_counts method
0nehundr3d Jan 7, 2025
99b3cdb
Merge pull request #3 from 0nehundr3d/election
0nehundr3d Jan 7, 2025
0dd1967
Add open attribute
0nehundr3d Jan 7, 2025
3b839d7
add close! method
0nehundr3d Jan 7, 2025
e49e5a5
Add winner method functionality
0nehundr3d Jan 7, 2025
ad80fcb
Add tie? method
0nehundr3d Jan 7, 2025
72a5636
Merge pull request #4 from 0nehundr3d/race-finish
0nehundr3d Jan 7, 2025
41a349e
Add winner method
0nehundr3d Jan 7, 2025
a73015f
Merge pull request #5 from 0nehundr3d/election-winner
0nehundr3d Jan 7, 2025
d187447
Change wimmer method to ouput only a songle candidate
0nehundr3d Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/candidate.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
class Candidate
attr_reader :name, :party, :votes

def initialize(info_hash)
@name = info_hash[:name]
@party = info_hash[:party]

@votes = 0
end

def vote_for
@votes += 1
end
end
24 changes: 24 additions & 0 deletions lib/election.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
class Election
attr_reader :year, :races

def initialize(year)
@year = year
@races = []
end

def add_race(race)
@races << race
end

def candidates
@races.map { |race| race.candidates }.flatten
end

def vote_counts
votes = {}
candidates.each { |candidate| votes[candidate.name] = candidate.votes }
votes
end

def winners
@races.find_all { |race| !race.tie? && !race.open? }.map(&:winner)
end
end
33 changes: 33 additions & 0 deletions lib/race.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
class Race
attr_reader :office, :candidates

def initialize(office)
@office = office
@candidates = []
@open = true
end

def register_candidate(info)
candidate = Candidate.new(info)
@candidates << candidate
candidate
end

def open?
@open
end

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]
end

def tie?
@candidates.find_all { |candidate| candidate.votes == @candidates.max_by(&:votes).votes }.size > 1
end
end
38 changes: 38 additions & 0 deletions spec/candidate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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

it 'starts with 0 votes' do
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)

@diana.vote_for

expect(@diana.votes).to eq(4)
end
end
end
80 changes: 80 additions & 0 deletions spec/election_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'rspec'
require './lib/candidate'
require './lib/race'
require './lib/election'

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
it 'exists' do
expect(@election).to be_a(Election)
end

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

describe '#add_race' do
it 'can add a race' do
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
@election.add_race(@race2)
@election.add_race(@race1)

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)

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

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
91 changes: 91 additions & 0 deletions spec/race_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require 'rspec'
require './lib/race'
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

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

it 'begins open' do
expect(@race.open?).to eq(true)
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

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

describe '#close!' do
it 'can close' do
@race.close!

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
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