Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #Candidate INFO Class
  2. class INFO
  3.  
  4. attr_accessor :name, :votes, :type
  5.  
  6. end
  7.  
  8. #Setting Up Default Candidates
  9. candidates = []
  10. candidates[0] = INFO.new
  11. candidates[0] = {
  12. :name => "Kanye West",
  13. :votes => 0,
  14. :type => "Default"
  15. }
  16.  
  17. candidates[1] = INFO.new
  18. candidates[1] = {
  19. :name => "Oprah Winfrey",
  20. :votes => 0,
  21. :type => "Default"
  22. }
  23.  
  24. candidates[2] = INFO.new
  25. candidates[2] = {
  26. :name => "Ellen DeGeneres",
  27. :votes => 0,
  28. :type => "Default"
  29. }
  30.  
  31. #Program Introduction
  32. puts "Election Voting Program"
  33. choice = 0
  34.  
  35. while choice == 0
  36. #Prints Candidates List
  37. print "\n\nCandiates: "
  38. count = 0
  39.  
  40. while count < candidates.length
  41. print "#{candidates[count][:name]}"
  42.  
  43. if count < candidates.length-1
  44. print ", "
  45. end
  46.  
  47. count += 1
  48. end
  49.  
  50. #Candidate Change/Add Menu
  51. puts "\n\nTo Change Candidates, Type 1. \nOtherwise TYPE ANY KEY TO CONTINUE"
  52. choice = gets.chomp.to_i
  53.  
  54. while choice == 1
  55. #Prints Out Current Candidate List
  56. count = 0
  57. puts "\nCandidates: "
  58.  
  59. while count < candidates.length
  60. puts "#{count+1}) #{candidates[count][:name]}"
  61. count += 1
  62. end
  63. puts "#{count+1}) Add New"
  64.  
  65. #Add/Edit Candiate
  66. print "\n\nType of Number of the Candidate You Would Like to Change\nor -1 to Exit: "
  67. change = gets.chomp.to_i
  68.  
  69. if change < 0 || change > candidates.length + 1
  70. choice = 0
  71. else
  72. count = 1
  73. until change==count
  74. count += 1
  75. end
  76. end
  77.  
  78. if choice !=0
  79. if change-1 == candidates.length
  80. print "Enter New Candidate: "
  81. candidates[candidates.length] = { :name => gets.chomp.to_s.downcase.gsub(/\b\w/, &:capitalize),
  82. :votes => 0,
  83. :type => "Default"
  84. }
  85. else
  86. print "Edit Candidate: "
  87. candidates[change-1][:name] = gets.chomp.to_s.downcase.gsub(/\b\w/, &:capitalize)
  88. end
  89. end
  90.  
  91. end
  92.  
  93. end
  94.  
  95.  
  96. #Vote Collection
  97. vote_count = 1
  98. vote = "0"
  99. puts "\n\nVOTING"
  100. puts "\nType Candidate's Name or Type 1 to Stop"
  101. print "\nVote ##{vote_count}: "
  102. vote = gets.chomp.to_s.downcase.gsub(/\b\w/, &:capitalize)
  103. while vote != "1"
  104. count = 0
  105. #Candidate Write In - Check and Add
  106. until vote == candidates[count][:name] || count == candidates.length-1
  107. count += 1
  108. end
  109.  
  110. if vote == candidates[count][:name]
  111. candidates[count][:votes] += 1.to_i
  112. else
  113. candidates[count+1] = INFO.new
  114. candidates[count+1] = {
  115. :name => vote.downcase.gsub(/\b\w/, &:capitalize),
  116. :votes => 1.to_i,
  117. :type => "Write In"
  118. }
  119. end
  120.  
  121. vote_count += 1
  122. print "\nVote ##{vote_count}: "
  123. vote = gets.chomp.to_s.downcase.gsub(/\b\w/, &:capitalize)
  124. end
  125.  
  126. #Election Results
  127. puts "\n\nElection Results..."
  128. puts "\nVote Summary:"
  129. count = 0
  130. while count < candidates.length
  131. print "#{candidates[count][:name]} - #{candidates[count][:votes]} "
  132.  
  133. if candidates[count][:votes] > 1 || candidates[count][:votes]==0
  134. print "votes"
  135. else
  136. print "vote"
  137. end
  138.  
  139. if candidates[count][:type]=="Write In"
  140. print ", WRITE IN CANDIDATE\n"
  141. else
  142. print "\n"
  143. end
  144.  
  145. count +=1
  146. end
  147.  
  148. #Finding Max Vote
  149. count = 0
  150. winner = []
  151. winner_vote = candidates.max_by {|x| x[:votes]} [:votes]
  152.  
  153. #Finding Winner(s)
  154. while count < candidates.length
  155. if candidates[count][:votes] == winner_vote
  156. winner << candidates[count][:name]
  157. end
  158.  
  159. count += 1
  160. end
  161.  
  162. #Printing Results
  163. if winner.length > 1
  164. print "\n\nWINNERS: \n"
  165. else
  166. print "\n\nWINNER: "
  167. end
  168.  
  169. winner.each do |e|
  170. puts e
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement