Build a Net Income Calculator App with Ruby

ruby income calculator
Standard

Share this tutorial w/ your followers on Twitter. Click this link to tweet this tutorial: https://goo.gl/visSgs

This tutorial is for beginners who want to learn how to build applications in Ruby programming language.

What will we build?

We will build a net income calculator for freelancers in an imaginary company that serves as a middleman between a freelancer and an employer. To be able to focus on Ruby, we won’t build a web based application so you won’t have to use HTML. What we will build is a very simple command line application.

Requirements

  • The user should be able to input his/her hourly rate.
  • The user should also be able to input the number of hours he/she worked.
  • We will assume that our imaginary company deducts $1 + 10% of the gross income of the user .
  • The gross income, net income and deductions should be shown to the user.

Building the command line application

Create a file and name it income_calculator.rb

Before starting to write code, it’s a good practice to plan how you will build the application first. Let’s do that by adding comments inincome_calculator.rb

# ask the hourly rate of the user
# convert hourly rate to float
# ask the number of hours that the user has worked
# convert hours worked to float
# multiply the hourly rate and the number of hours worked
# get the 10% of the gross income
# add 1 to the deduction
# subtract the deductions to the gross income
# show the gross income, net income and deductions to the user

Now that we have a clear idea on what are the steps that we should code to achieve the expected result, let’s start writing code.

# ask the hourly rate of the user
print “What is your hourly rate?”
hourly_rate = gets

gets is a built-in function in Ruby that we can use to gather inputs from a user. What this block of code basically does is ask the hourly rate of the user and stores the input of the user in the hourly_rate variable.

Learn Ruby from scratch. Build and launch a web app. Start learning forFREE: http://treehouse.7eer.net/c/245500/245636/3944

# convert hourly rate to float
hourly_rate = hourly_rate.to_f

The result of gets is a string so we are converting it to a float for us to be able to use it for math operations later.

# ask the number of hours that the user has worked
print “How many hours did you worked?”
hours_worked = gets
# convert hours worked to float
hours_worked = hours_worked.to_f

What the code above does is similar to the code that I’ve discussed earlier. It just asks the number of hours that the user worked, store it in thehours_worked variable then converts it to a float.

# multiply the hourly rate and the number of hours worked
gross_income = hourly_rate * hours_worked

To get the gross income of the user, we are multiplying the user’s hourly rate and the number of hours worked. We are storing the result in thegross_income variable.

# get the 10% of the gross income
deduction = gross_income * .10

We are multiplying gross income and 0.10 to get the 10% of gross income. We are storing the result in the deduction variable.

# add 1 to the deduction
total_deductions = deduction + 1

Since the company also deducts $1 to the gross income of the user, we are adding 1 to the value of deduction variable and we are storing the result tototal_deductions variable.

# subtract the deductions to the gross income
net_income = gross_income — total_deductions

To compute the net income of the user, we are subtracting the total deductions to the gross income.

# show the gross income, net income and deductions to the user
puts “Gross income: “ + gross_income.to_s
puts “Net income: “ + net_income.to_s
puts “Total deductions: “ + total_deductions.to_s

This block of code exactly does what the comment says. It will show the gross income, net income and deductions to the user. In Ruby, since we can’t concatenate a string (Example: “Gross income”) and a float (gross_income), we have to convert gross_income to a string. We can convert a float to a string by using the to_s method.

The contents of income_calculator.rb should now look like this:

# ask the hourly rate of the user
print “What is your hourly rate?”
hourly_rate = gets
# convert hourly rate to float
hourly_rate = hourly_rate.to_f
# ask the number of hours that the user has worked
print “How many hours did you worked?”
hours_worked = gets
# convert hours worked to float
hours_worked = hours_worked.to_f
# multiply the hourly rate and the number of hours worked
gross_income = hourly_rate * hours_worked
# get the 10% of the gross income
deduction = gross_income * 0.10
# add 1 to the deduction
total_deductions = deduction + 1
# subtract the deductions to the gross income
net_income = gross_income — total_deductions
# show the gross income, net income and deductions to the user
puts “Gross income: “ + gross_income.to_s
puts “Net income: “ + net_income.to_s
puts “Total deductions: “ + total_deductions.to_s

Now let’s run our command line application by running this command in your terminal:

ruby income_calculator.rb

It should ask your hourly rate and number of hours worked then show your gross income, net income and total deductions.

ruby income calculator

Feel free to leave a feedback about the tutorial by leaving a comment. I would really appreciate it if you will recommend and also tweet this tutorial 🙂

Share this tutorial w/ your followers on Twitter. Click this link to tweet this tutorial: https://goo.gl/visSgs

Want to get updated when I publish another tutorial? Subscribe:http://eepurl.com/bVGMwr

Learn Ruby from scratch. Build and launch a web app. Start learning forFREE: http://treehouse.7eer.net/c/245500/245636/3944

Are you looking for a remote/home based job? Visithttp://digitalnomadgigs.com/

You can follow me on Twitter: https://twitter.com/johnkevinmbasco

Looking for a developer? I’m accepting web/mobile development projects. Shoot me an email: [email protected]