Node CLI App - Tax Calculator
Tags: javascript, node, webdev, beginners
Published: 2023-04-14 12:26:36

Hey what's going on guys, my name is Amir from Bek Brace channel, and in this post I am going to share with you how to create a retail sales tax calculator, it's a command line interface application using Node.js.
Have you ever gone on a shopping spree only to be hit with a hefty sales tax bill at checkout? If so, you might appreciate this handy little tool that calculates the tax and total cost of your purchase based on your state's tax rate; and by the way that is very relevant if you live in the United States of America, if you happen to live away of the US like myself, you can tweak in the code and change it based on your retail sales tax laws.

The code uses several libraries, including Chalk, Inquirer, and SQLite3, to prompt the user for the purchase amount and the state where the purchase was made, retrieve the corresponding tax rate from a SQLite database, and calculate the tax and total cost of the purchase.

```javascript
import chalk from 'chalk';
import inquirer from 'inquirer';
import sqlite3 from 'sqlite3';

const db = new sqlite3.Database(':memory:');
db.serialize(() => {
  db.run(`
    CREATE TABLE IF NOT EXISTS tax_rates (
      state TEXT,
      rate FLOAT
    )
  `);
```
At the beginning of the code, we import three libraries: Chalk, Inquirer, and SQLite3. Chalk is a library that allows us to add colors to the console output, while Inquirer is a library that provides an easy way to prompt the user for input. SQLite3 is a library that provides a simple way to interact with a SQLite database.

Here's the video tutorial if you want to watch it directly:

{%youtube SMOVh6f2MuY%}


After all that hard work creating the tax_rates table and setting up our database, it's finally time to calculate some taxes!

Let's take a closer look at the inquirer prompts that we've set up to take in user input. The first prompt is asking for the amount of the purchase, and it has a validation function to ensure that the user enters a valid number. If the user enters a non-number value, it will prompt them to try again.

The second prompt is asking for the state where the purchase was made, and it has a validation function to ensure that the user enters a valid two-letter state code. This validation function uses a regular expression to ensure that the input only consists of two letters.

Once we have the user's input for both prompts, it's time to retrieve the tax rate from our database. We're using the db.get() method of our database object to execute an SQL query to retrieve the rate column from the tax_rates table where the state column matches the state variable provided as a parameter.

If an error occurs during the database query, we log the error message to the console in red color using the console.error() method and then return. If no error occurs, the row parameter will contain the result of the database query with the rate value for the given state.

Now that we have the tax rate, we can calculate the tax and total amount of the purchase. We do this by multiplying the amount by the tax rate to get the tax amount, and then adding that tax amount to the original purchase amount to get the total.

Finally, we log the amount, tax, and total values to the console in green color using the chalk package. The chalk package allows us to colorize our console output, which makes it easier to read and understand.

Overall, this code is a great example of how we can use Node.js and various packages to create powerful and flexible command-line applications. With the power of SQLite and inquirer, we can create a database-backed application that can perform complex calculations with ease. And with the help of chalk, we can make our console output look beautiful while we're at it!