Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Candidate INFO Class
- class INFO
- attr_accessor :name, :votes, :type
- end
- #Setting Up Default Candidates
- candidates = []
- candidates[0] = INFO.new
- candidates[0] = {
- :name => "Kanye West",
- :votes => 0,
- :type => "Default"
- }
- candidates[1] = INFO.new
- candidates[1] = {
- :name => "Oprah Winfrey",
- :votes => 0,
- :type => "Default"
- }
- candidates[2] = INFO.new
- candidates[2] = {
- :name => "Ellen DeGeneres",
- :votes => 0,
- :type => "Default"
- }
- #Program Introduction
- puts "Election Voting Program"
- choice = 0
- while choice == 0
- #Prints Candidates List
- print "\n\nCandiates: "
- count = 0
- while count < candidates.length
- print "#{candidates[count][:name]}"
- if count < candidates.length-1
- print ", "
- end
- count += 1
- end
- #Candidate Change/Add Menu
- puts "\n\nTo Change Candidates, Type 1. \nOtherwise TYPE ANY KEY TO CONTINUE"
- choice = gets.chomp.to_i
- while choice == 1
- #Prints Out Current Candidate List
- count = 0
- puts "\nCandidates: "
- while count < candidates.length
- puts "#{count+1}) #{candidates[count][:name]}"
- count += 1
- end
- puts "#{count+1}) Add New"
- #Add/Edit Candiate
- print "\n\nType of Number of the Candidate You Would Like to Change\nor -1 to Exit: "
- change = gets.chomp.to_i
- if change < 0 || change > candidates.length + 1
- choice = 0
- else
- count = 1
- until change==count
- count += 1
- end
- end
- if choice !=0
- if change-1 == candidates.length
- print "Enter New Candidate: "
- candidates[candidates.length] = { :name => gets.chomp.to_s.downcase.gsub(/\b\w/, &:capitalize),
- :votes => 0,
- :type => "Default"
- }
- else
- print "Edit Candidate: "
- candidates[change-1][:name] = gets.chomp.to_s.downcase.gsub(/\b\w/, &:capitalize)
- end
- end
- end
- end
- #Vote Collection
- vote_count = 1
- vote = "0"
- puts "\n\nVOTING"
- puts "\nType Candidate's Name or Type 1 to Stop"
- print "\nVote ##{vote_count}: "
- vote = gets.chomp.to_s.downcase.gsub(/\b\w/, &:capitalize)
- while vote != "1"
- count = 0
- #Candidate Write In - Check and Add
- until vote == candidates[count][:name] || count == candidates.length-1
- count += 1
- end
- if vote == candidates[count][:name]
- candidates[count][:votes] += 1.to_i
- else
- candidates[count+1] = INFO.new
- candidates[count+1] = {
- :name => vote.downcase.gsub(/\b\w/, &:capitalize),
- :votes => 1.to_i,
- :type => "Write In"
- }
- end
- vote_count += 1
- print "\nVote ##{vote_count}: "
- vote = gets.chomp.to_s.downcase.gsub(/\b\w/, &:capitalize)
- end
- #Election Results
- puts "\n\nElection Results..."
- puts "\nVote Summary:"
- count = 0
- while count < candidates.length
- print "#{candidates[count][:name]} - #{candidates[count][:votes]} "
- if candidates[count][:votes] > 1 || candidates[count][:votes]==0
- print "votes"
- else
- print "vote"
- end
- if candidates[count][:type]=="Write In"
- print ", WRITE IN CANDIDATE\n"
- else
- print "\n"
- end
- count +=1
- end
- #Finding Max Vote
- count = 0
- winner = []
- winner_vote = candidates.max_by {|x| x[:votes]} [:votes]
- #Finding Winner(s)
- while count < candidates.length
- if candidates[count][:votes] == winner_vote
- winner << candidates[count][:name]
- end
- count += 1
- end
- #Printing Results
- if winner.length > 1
- print "\n\nWINNERS: \n"
- else
- print "\n\nWINNER: "
- end
- winner.each do |e|
- puts e
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement