Skip to content

Instantly share code, notes, and snippets.

@RodneyPerez
Created February 14, 2016 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RodneyPerez/8c43f0d3a54dfbe801cc to your computer and use it in GitHub Desktop.
Save RodneyPerez/8c43f0d3a54dfbe801cc to your computer and use it in GitHub Desktop.
# don't initialize class with existing data
# don't use a hash to store your voters
# don't store store your list of voters in the voter class
# store them in the simulation class
# all putsing and user input should be done outside of classes
#this class initializes the person with a name and an affilation
#something both a politician and a voter share
class Person
attr_accessor :affiliation, :name
def initialize(name, affiliation)
@name = name
@affiliation = affiliation
end
def to_s
"#{name.to_s} #{affiliation.to_s}"
end
end
#This class contains the all the direct actions that will occur in the program
# These are Create Delete List
class VoterSimulation
attr_reader :voters_in_world, :politicians_in_world
def initialize
@voters_in_world = []
@politicians_in_world = []
end
def create(kind, name, affiliation)
if kind == "voter"
@voters_in_world << Person.new(name, affiliation)
elsif kind == "politician"
@politicians_in_world << Person.new(name, affiliation)
end
end
def update(kind, old_name, old_affiliation, new_name, new_affiliation)
if kind == "voter"
@voters_in_world.each do |voter|
if voter.name == old_name
delete("voter", old_name)
create("voter", new_name, new_affiliation)
end
end
elsif kind == "politician"
@politicians_in_world.each do |politician|
if politician.name == old_name
delete("politician", old_name)
create("politician", new_name, new_affiliation)
end
end
end
end
def delete(kind, name)
if kind == "voter"
i = 0
@voters_in_world.each do |person|
if person.name == name
voters_in_world.delete_at(i)
end
i+=1
end
elsif kind == "politician"
i =0
@politicians_in_world.each do |politico|
if politico.name == name
politicians_in_world.delete_at(i)
end
i+=1
end
end
end
end
#This module contains the interaction with user
#Calls an instance of the Class VoterSimulation inside and calls on the methods inside VoterSimulation
#to return the information requested by the user
module AppRunner
def self.run
@@sim = VoterSimulation.new
loop do
input = ask_and_recieve_answer("What would you like to do?
(C)reate, (L)ist, (U)pdate, (D)elete or (E)xit")
case input[0]
when "c"
create_questions
when "l"
list
when "d"
delete
when "u"
update
when "e"
puts "Thank you for using the voter sim!"
exit
end
end
end
def self.user_input
gets.chomp.downcase
end
def self.returning_text(dialogue)
puts dialogue
end
def self.ask_and_recieve_answer(dialogue)
returning_text(dialogue)
user_input
end
def self.asking_voter_or_politician
entity_selection = ask_and_recieve_answer("(V)oter or (P)olitician")
if entity_selection[0] == "v"
"voter"
elsif entity_selection[0]=="p"
"politician"
else
puts "not a valid selection"
run
end
end
def self.create_party_selection(entity)
if entity[0] == "v"
party_selection = ask_and_recieve_answer("Politics?
(L)iberal, (C)onservative, (T)ea Party, (S)ocialist, or (N)eutral")
case party_selection
when "l"
return "Liberal"
when "c"
"Conservative"
when "t"
"Tea Party"
when "s"
"Socialist"
when "n"
"Neutral"
else
puts "Not a valid selection"
run
end
elsif entity[0] == "p"
party_selection = ask_and_recieve_answer("Party?
(D)emocrat or (R)epublican")
case party_selection
when "d"
"Democrat"
when "r"
"Republican"
else
returning_text ("Not a valid selection")
run
end
end
end
def self.create_questions
entity = asking_voter_or_politician
name = ask_and_recieve_answer("Name?")
party = create_party_selection(entity)
@@sim.create(entity, name, party)
returning_text("The name has been added. This the current list of #{entity}s and their parties")
list_after_action(entity)
end
def self.list
kind = asking_voter_or_politician
if kind[0] == "v"
@@sim.voters_in_world.each do |politico|
returning_text( "#{politico.name.capitalize} is a #{politico.affiliation}")
end
elsif kind[0] == "p"
@@sim.politicians_in_world.each do |politico|
returning_text("#{politico.name.capitalize} is a #{politico.affiliation}")
end
end
end
def self.list_after_action(kind)
if kind[0] == "v"
@@sim.voters_in_world.each do |politico|
returning_text("#{politico.name.capitalize} is a #{politico.affiliation}")
end
elsif kind[0] == "p"
@@sim.politicians_in_world.each do |politico|
returning_text("#{politico.name.capitalize} is a #{politico.affiliation}")
end
end
end
def self.update
entity = asking_voter_or_politician
name = ask_and_recieve_answer("Name you would like change?")
old_party = create_party_selection(entity)
new_name = ask_and_recieve_answer("Type in the new name you would like")
new_party = create_party_selection(entity)
@@sim.update(entity, name, old_party, new_name, new_party).blank?
returning_text("The name has been updated!")
list_after_action(entity)
end
def self.delete
entity = asking_voter_or_politician
returning_text("This is the current list of #{entity}s")
list_after_action(entity)
name = ask_and_recieve_answer("Name you would like to delete?")
delete_answer= ask_and_recieve_answer("Are you sure you would like to delete #{name} from the database? Y/N?")
if delete_answer[0]=="y"
@@sim.delete(entity, name)
puts "The name has been deleted"
list_after_action(entity)
else
returning_text("The name has not been deleted")
end
end
end
AppRunner.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment