Challenge 6: Retirement Calculator

Challenge number 6 asks the dev to create a retirement calculator. The program should ask a user’s age, and what age they want to retire, calculates how many years are left before retirement, and what the year will be. It’s pretty straightforward, but there are some interesting ways to approach the puzzle. 

My strategy when first approaching a challenge is to make the program work in the dumbest way possible, and then make it better in the refactor. The first time around, my code looked something like this: 

`puts “What is your current age?”

current_age = gets.chomp.to_i

puts “At what age would you like to retire?”

retirement_age = gets.chomp.to_i

years_to_retirement = retirement_age – current_age

this_year = Time.now.year

retirement_date = this_year + years_to_retirement

puts “\n You have #{years_to_retirement} years left until you can retire. \n It’s #{this_year}, so you can retire in #{retirement_date}.”`

I decided to calculate the current year once and save it as a variable, that way I wouldn’t have to do it multiple times. It’s very simple, readable, etc. The extra credit challenge was to make it give a different answer if the years_to_retirement value was negative, which I accomplished with an if/else block:

if years_to_retirement <= 0

     puts “You can already retire!”

Else

puts “\n You have #{years_to_retirement} years left until you can retire. \n It’s #{this_year}, so you can retire in #{retirement_date}.”

end

The final extra credit that wasn’t actually a part of the instructions asked the question “what if the user tries to say their age and retirement age in words instead of numbers?” I wanted to practice creating error messages, and enforcing standards, so I created a method that relies on a rescue/retry block:

def getInteger(prompt)

    begin

        puts prompt

        integer = Integer(gets.chomp)

    rescue ArgumentError => error

        puts “Please use numerals”

        retry

    end

    return integer    

End

Where the method getInteger() takes a string argument, defines a variable integer, and when used in this configuration will only take an actual integer without raising an error. The rescue attempt looks for the ArgumentError, and when raised, will clarify the problem for the user, and retry the block. The way this method is used is by taking the information that’s returned at the end, saving it to a variable, and running through the program as usual:

current_age = getInteger(“What is your current age?”)

I enjoyed working on this one because it gave me an excuse to work with the rescue/retry error blocks. Before looking that up, I entertained the idea of using a while loop with a condition of `defined?(variable) == nil`, but that didn’t work due to a scope problem, and the way to make that work would have made the solution a little less eloquent, and the Retry method is built for this situation. And that’s it for today! I hope you enjoyed my explanation, and there will be more tomorrow.

Published by corbettbw

I am a Ruby developer in Phoenix, AZ. I'm interested in the intersection of technology and social justice, love weird science facts, and my dog, Coco; a cute black lab/pit bull mix, who won't stop eating rocks.

Leave a comment