Created
April 28, 2016 02:53
-
-
Save charmainetham/cca1c1e8d4c60d6877bfe133157832c9 to your computer and use it in GitHub Desktop.
States & cities
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
#code to find out about cities | |
@states = { | |
OR: "Oregon", | |
FL: "Florida", | |
CA: "California", | |
NY: "New York", | |
MI: "Michigan" | |
} | |
#Task 1 inserting 2 additional states without modifying initial hash | |
@states[:MO] = "Missouri" | |
@states[:CO] = "Colorado" | |
#p states | |
#Task 2 | |
@cities = { | |
OR: ["Portland", "Bend", "Salem" ], | |
FL: ["Miami", "Orlando", "Tampa"], | |
CA: ["Los Angeles", "San Francisco", "Sacramento"], | |
NY: ["Bufflo", "NYC", "Rochester"], | |
MI: ["Detroit", "Flint"], | |
MO: ["St louis", "Springfield", "Columbia"], | |
CO: ["Denver", "Boulder", "Aspen"] | |
} | |
#Task 4 Declare Hash called Taxes | |
@taxes = { | |
OR: 12, | |
FL: 7.5, | |
CA: 5.5, | |
NY: 6, | |
MI: 15, | |
MO: 7, | |
CO: 5 | |
} | |
def list_of_states | |
@states.each {|s,sn| puts "#{s}"} | |
end | |
def get_cities(state) | |
array_of_cities_for_state = @cities[state] | |
array_of_cities_for_state.join(" ,") | |
end | |
def describe_state(state) | |
puts "#{state} is for #{@states[state]}. it has #{@cities[state].size}\ | |
cities : #{get_cities(state)}" | |
end | |
#Defining a method that calculates taxes | |
def calculate_tax(state, dollar) | |
tax_rate = @taxes[state] * dollar/100 | |
end | |
def find_state_for_city(city) | |
statecode='' | |
@cities.each do |state, cityvalue| | |
cityvalue.each do |mycity| | |
if mycity==city | |
statecode=state | |
end | |
end | |
end | |
puts "the state code is #{statecode}" | |
end | |
def get_state(city) | |
@cities.map{|s, c| puts "#{c}"} | |
end | |
#Finding list of cities in states | |
puts "Which STATES did you want to look up" | |
list_of_states | |
puts "Enter your selection" | |
prompt = gets.chomp.to_sym.upcase | |
if @states.include?(prompt) | |
describe_state(prompt) | |
else | |
puts "Invalid selection" | |
end | |
#TAX RATE | |
puts "What is your expenditure?" | |
dollar = gets.chomp.to_f | |
puts "Spending #{dollar} means that your tax rate is\ | |
#{(calculate_tax(prompt, dollar)*100/dollar).to_i}%\ | |
you pay #{calculate_tax(prompt, dollar)} in taxes" | |
#Looking up states when given city | |
puts "which City yould you like to find out about?" | |
city_prompt = gets.chomp | |
find_state_for_city(city_prompt) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment