Created
March 5, 2014 22:30
-
-
Save MonicaDev/9378017 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # When you are finished with this challenge you should be able to: | |
| # Explain what "require_relative" does and why you would use it | |
| # Demonstrate how to iterate through a hash | |
| # Easily recognize and refactor repetitive code | |
| # Objectives | |
| # 1) Run the code. Look at the output. Look at the input (it's in the other file). Explain what the program is doing. | |
| # 2) Write a comment explaining the require_relative statement below | |
| # 3) Comment each method and define it's responsibility | |
| # 4) New Feature: create a report for all 50 states, not just the 4 listed below. Is there a DRY way of doing this? #case statements. | |
| # 5) Refactor the virus_effects method. (HINT: what is the scope of instance variables?) | |
| # 6) What is the purpose of "private". What happens if you move it elsewhere in the class? | |
| # 7) Refactor the private methods predicted_deaths and speed_of_spread. How can you make them more DRY? #Case method | |
| # 8) BONUS: Access the population by calling it on the instance. | |
| require_relative 'state_data' | |
| # a way to look for the file if the file is close like in the same folder other wise you have to give the path directory. | |
| #require _give it the full path name is another way to associate it with another file. | |
| class VirusPredictor | |
| def initialize(state_of_origin, population_density, population, region, regional_spread) | |
| @state = state_of_origin | |
| @population = population | |
| @population_density = population_density | |
| @region = region | |
| @next_region = regional_spread | |
| end | |
| def virus_effects # How can I refactor the virus_effects method? | |
| predicted_deaths | |
| speed_of_spread | |
| end | |
| private #what is this? # keeps predicted_deaths and speed_of_spread from being called outside of VirusPredictor | |
| def predicted_deaths | |
| #begin= ________ | |
| number_of_deaths = case @population_density | |
| when 0..50 | |
| (@population * 0.05).floor | |
| when 50..100 | |
| (@population * 0.1).floor | |
| when 100..150 | |
| (@population * 0.2).floor | |
| when 150..200 | |
| (@population * 0.3).floor | |
| else | |
| (@population * 0.4).floor | |
| end | |
| print "#{@state} will lose #{number_of_deaths} people in this outbreak" | |
| end | |
| def speed_of_spread #in months | |
| speed = 0.0 | |
| speed = case @population_density | |
| when 0..50 | |
| speed += 2 | |
| when 50..100 | |
| speed += 1.5 | |
| when 100..150 | |
| speed += 1 | |
| when 150..200 | |
| speed += 0.5 | |
| else | |
| speed += 2.5 | |
| end | |
| puts " and will spread across the state in #{speed} months.\n\n" | |
| end | |
| end | |
| #======================================================================= | |
| # DRIVER CODE | |
| # initialize VirusPredictor for each stat | |
| STATE_DATA.each do |state, value| | |
| state_report = VirusPredictor.new(state, value[:population_density], value[:population], value[:region], value[:regional_spread]) | |
| state_report.virus_effects | |
| end | |
| STATE_DATA.each do |state, value| | |
| alabama = VirusPredictor.new("Alabama", STATE_DATA["Alabama"][:population_density], STATE_DATA["Alabama"][:population], STATE_DATA["Alabama"][:region], STATE_DATA["Alabama"][:regional_spread]) | |
| alabama.population #=> 4822023 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment