How to Create a Simple Sales Tax Application

Erica Ann Basak
4 min readOct 25, 2021

Currently, I’m on the hunt for a job as a software engineer as I recently finished a coding bootcamp. For one of the jobs, I received a take home coding exercise to start up a Rails app. The assignment was to create a sales tax application with Rails, given a list of items including pricing and sales criteria. I created a simple sales tax calculator in Ruby on Rails. Here is the process in which I created my application

First, I started by running the Rails command -

rails new sales_tax_application — database=postgresql

The above command is the starting point to get my Rails application started. Once I ran this command, Rails provides a bunch of boilerplate code to get my application up and running quickly!

Followed by creating a migration — the the rails migration command -

rails generate migration Product name:string price:integer imported:boolean exempt:boolean

After creating the migration, I was able to get my schema. The schema shows my Products Table with several attributes and their data types.

Schema

Next, I created my seed data — here is a sample of some of the seed data. Each product includes a name, price, imported, and exempt attribute. The seed file is created with the Rails command rails db:seed.

Seed data

After seeding, I began working on the controller. I started with creating a show method that would show the list of items. Next, I created the addItem method, which is where the bulk of my logic is placed along with the model. Here in the controller, I needed a way to save the item that is placed in the cart. So I added sessions to my application. Once the user wants to create a new cart, there is a clear sessions button and a user can start with nothing in their cart.

Next, I’ll cover the methods in my model. These methods are the building blocks of the tax app and where the calculations take place.

In this calculate_item_tax method, I am taking the price of the item and multiplying that by a sales tax of 10%. Once I have that number, it gets rounded to the nearest 0.05 cents.

calculate item tax method

In this calculate_tax method, I am filtering out the non-exempt items and looking for only the exempt items because those are the one’s that I am placing a 10% sales tax on.

calculate tax method

In this calculate_import method, I am filtering out the imported items because those are the one’s that will get an additional 0.05% duty tax.

calculate imported tax method

In this calculate_total_price method, essentially the price for the item along with its appropriate tax all get added up here.

In the show page, I have a couple of form_with form helpers. I have one for the drop down which lists all items and its “Save Products” button. The other form_with is for the button “Clear Session” which clears out the cart.

form_with helper
form_with helper
http://localhost:3000/sales/show
drop down of all items

Below is the receipt of the purchases which shows an itemized list of the items in the cart along with their pertinent taxes.

http://localhost:3000/sales/addItem

Below is how I created a simple table to display the items in HTML. This table only contains to columns, which are Name and Price for the item.

table for the items

Hope you enjoyed my blog on how to create a simple sale tax application.

Comments appreciated!

--

--