Created
February 8, 2016 05:14
-
-
Save RodneyPerez/aba3f8ff0cf8a73098d2 to your computer and use it in GitHub Desktop.
Voter Sim Weekend 2 project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Voter | |
def initialize | |
@list_of_voters = {"Roger"=> "Tea Party" , "Auston"=>"Anarchist", "Bianca"=>"Liberal"} | |
end | |
def puts_out_information(information) | |
puts information | |
end | |
def create | |
puts_out_information("Name of Voter you would add?") | |
@name = gets.chomp | |
if voter_name_checker | |
puts_out_information("Politics? | |
(L)iberal, (C)onservative, (T)ea Party, (S)ocialist, (N)eutral or type in custom") | |
party_user_selection = gets.chomp.downcase | |
party_returned= voter_party_selection(party_user_selection) | |
@list_of_voters[@name]= party_returned | |
puts_out_information("The voter has been added!") | |
list | |
else | |
puts_out_information("This voter already exists") | |
end | |
end | |
def voter_name_checker | |
if @list_of_voters.key?(@name) | |
false | |
else | |
true | |
end | |
end | |
def list | |
puts_out_information ("This is the list of voters and their politics: ") | |
@list_of_voters.each do |key, value| | |
puts "#{key}, #{value}" | |
end | |
end | |
def voter_party_selection(selection) | |
case selection | |
when "l" | |
return "Liberal" | |
when "c" | |
return "Conservative" | |
when "t" | |
return "Tea Party" | |
when "s" | |
return "Socialist" | |
when "n" | |
return "neurtal" | |
else | |
return selection | |
end | |
end | |
def remove | |
list | |
puts_out_information("What name would you like to delete?") | |
@name = gets.chomp | |
if !voter_name_checker | |
puts_out_information("Are you sure you want to delete this person?(Y)es or (N)o?") | |
remove_confirmation = gets.chomp | |
if remove_confirmation == "y" | |
@list_of_voters.delete @name | |
puts_out_information("The voter has been removed") | |
elsif remove_confirmation == "n" | |
puts_out_information("Name not deleted") | |
else | |
puts_out_information("Not a valid selection") | |
remove | |
end | |
else | |
puts_out_information("Name not in database") | |
end | |
end | |
def update | |
list | |
puts_out_information("Who is the voter you would like to update?") | |
@name = gets.chomp | |
if !voter_name_checker | |
puts_out_information("Would you like to update a name? (Y)es or (N)o?") | |
name_change_answer = gets.chomp.downcase | |
if name_change_answer == "y" | |
puts_out_information("Enter new name") | |
@new_name = gets.chomp | |
if !voter_name_checker | |
@list_of_voters[@new_name] = @list_of_voters.delete @name | |
puts_out_information("Name has been updated") | |
list | |
update_voter_party(@new_name) | |
else | |
puts "Name already in use" | |
update | |
end | |
elsif name_change_answer == "n" | |
update_voter_party(@name) | |
else | |
"That is not a valid selection" | |
update | |
end | |
else | |
puts_out_information ("That name is not in the database") | |
end | |
end | |
def update_voter_party(name) | |
puts_out_information("Would you like to update party?") | |
new_party_confirmation = gets.chomp | |
if new_party_confirmation == "y" | |
puts_out_information("Enter new party. (L)iberal, (C)onservative, (T)ea Party, (S)ocialist, (N)eutral or type in custom") | |
@new_party = gets.chomp | |
@list_of_voters.delete name | |
@list_of_voters[name] = voter_party_selection(@new_party) | |
puts_out_information("Politics has been updated") | |
list | |
elsif new_party_confirmation == "n" | |
else | |
puts_out_information("This is not a valid selection") | |
update | |
end | |
end | |
end | |
class Politician < Voter | |
def initialize | |
@list_of_politicians = {"Bernie Sanders"=> "Democrat", "Ted Cruz"=>"Republican", "Marco Rubio"=> "Republican", "Hilary Clinton"=>"Democrat"} | |
end | |
def create | |
puts_out_information("Name of the Politician you would add?") | |
@name = gets.chomp | |
if politician_name_checker | |
puts_out_information("Party? | |
(D)emocrat or (R)epublican") | |
party_user_selection = gets.chomp.downcase | |
party_returned= politician_party_selection(party_user_selection) | |
@list_of_politicians[@name]= party_returned | |
puts_out_information("The politician has been added!") | |
list | |
else | |
puts_out_information("This Politician already exists") | |
end | |
end | |
def politician_name_checker | |
if @list_of_politicians.key?(@name) | |
false | |
else | |
true | |
end | |
end | |
def politician_party_selection(selection) | |
case selection | |
when "d" | |
return "Democrat" | |
when "r" | |
return "Republican" | |
else | |
puts_out_information( "This is not a valid party and has no chance of winning please pick an established party") | |
create | |
end | |
end | |
def list | |
puts_out_information ("This is the list of polticians and their party: ") | |
@list_of_politicians.each do |key, value| | |
puts "#{key}, #{value}" | |
end | |
end | |
def remove | |
list | |
puts_out_information("What name would you like to delete?") | |
@name = gets.chomp | |
if !politician_name_checker | |
puts_out_information("Are you sure you want to delete this person?(Y)es or (N)o?") | |
remove_confirmation = gets.chomp | |
if remove_confirmation == "y" | |
@list_of_politicians.delete @name | |
puts_out_information("The Politican has been removed") | |
elsif remove_confirmation == "n" | |
puts_out_information("Name not deleted") | |
else | |
puts_out_information("Not a valid selection") | |
remove | |
end | |
else | |
puts_out_information("Name not in database") | |
end | |
end | |
def update | |
list | |
puts_out_information("Who is the Politician you would like to update?") | |
@name = gets.chomp | |
if !politician_name_checker | |
puts_out_information("Would you like to update a name? (Y)es or (N)o?") | |
name_change_answer = gets.chomp.downcase | |
if name_change_answer == "y" | |
puts_out_information("Enter new name") | |
@new_name = gets.chomp | |
if !politician_name_checker | |
@list_of_politicians[@new_name] = @list_of_politicians.delete @name | |
puts_out_information("Name has been updated") | |
list | |
update_politician_party(@new_name) | |
else | |
puts "Name already in use" | |
update_politician_party | |
end | |
elsif name_change_answer == "n" | |
update_politician_party(@name) | |
else | |
"That is not a valid selection" | |
update | |
end | |
else | |
puts_out_information ("That name is not in the database") | |
end | |
end | |
def update_politician_party(name) | |
puts_out_information("Would you like to update party?") | |
new_party_confirmation = gets.chomp | |
if new_party_confirmation == "y" | |
puts_out_information("Enter new party. | |
(D)emocrat or (R)epublican") | |
@new_party = gets.chomp | |
@list_of_politicians.delete name | |
@list_of_politicians[name] = politician_party_selection(@new_party) | |
puts_out_information("Politics has been updated") | |
list | |
elsif new_party_confirmation == "n" | |
else | |
puts_out_information("This is not a valid selection") | |
update | |
end | |
end | |
end | |
class World | |
@@voters_in_world = Voter.new | |
@@politicians_in_world = Politician.new | |
def initialize | |
voter_simulation_main_menu | |
end | |
def information_then_selection(information_presented) | |
puts information_presented | |
selection = gets.chomp.downcase | |
selection[0] | |
end | |
def voter_simulation_main_menu | |
action = information_then_selection("What would you like to do? | |
(C)reate, (L)ist, (U)pdate, (D)elete or (E)xit") | |
case action | |
when "c" | |
create_selection_voter_or_politician | |
when "l" | |
@@politicians_in_world.list | |
@@voters_in_world.list | |
voter_simulation_main_menu | |
when "u" | |
update_selection | |
voter_simulation_main_menu | |
when "d" | |
remove_selection | |
voter_simulation_main_menu | |
when "e" | |
puts "Thank you for using this voter simulator" | |
exit | |
else | |
puts "that's not a valid action" | |
voter_simulation_main_menu | |
end | |
end | |
def create_selection_voter_or_politician | |
create_selection = information_then_selection("What would you like to create? | |
(P)olitician, (V)oter or (R)eturn?") | |
case create_selection | |
when "p" | |
@@politicians_in_world.create | |
voter_simulation_main_menu | |
when "v" | |
@@voters_in_world.create | |
voter_simulation_main_menu | |
when "r" | |
voter_simulation_main_menu | |
else | |
puts "please pick a valid selection" | |
voter_simulation_main_menu | |
end | |
end | |
def list | |
@@voters_in_world.list | |
@@politicians_in_world.list | |
end | |
def remove_selection | |
list | |
user_remove_selection = information_then_selection("What would you like to remove? | |
(P)olitician, (V)oter or (R)eturn?") | |
case user_remove_selection | |
when "p" | |
@@politicians_in_world.remove | |
list | |
when "v" | |
@@voters_in_world.remove | |
list | |
when "r" | |
voter_simulation_main_menu | |
else | |
puts "That is not a valid selection please pick again" | |
remove_selection | |
end | |
end | |
def update_selection | |
list | |
politician_or_voter = information_then_selection("Would you like to update a (P)olitician or a (V)oter") | |
if politician_or_voter == "p" | |
@@politicians_in_world.update | |
elsif politician_or_voter == "v" | |
@@voters_in_world.update | |
else | |
puts "That is not a valid selection" | |
end | |
end | |
end | |
World.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment