Share on Google+Share on Google+

Calculate Sales Tax using Java Program

In this section, you will learn how to calculate the sales tax and print out the receipt details for the purchased items.

Calculate Sales Tax using Java Program

In this section, you will learn how to calculate the sales tax and print out the receipt details for the purchased items. To evaluate this, we have taken the following things into considerations:
1) Basic sales tax is applicable at a rate of 10% on all goods, except books,food, and medical products that are exempt.
2) Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions.
Here we have allowed the user to enter the quantity of item, name, price,whether they are exempted or not and whether they are imported or not. From this data, we have calculated the sales tax and created a receipt  which lists the name of all the purchased items and their price (including tax), finishing with the total cost of the items, and the total amounts of sales taxes paid.

Here is the code:

import java.util.*;

class Tax {
	int qty = 0;
	String prodName = null;
	float price = 0.0f;
	boolean imported = false;
	boolean exempted = false;
	float tax = 0.0f;

	void calculateSalesTax() {
		float totalTax = 0.0f;
		if (imported)
			totalTax = 0.05f;
		if (!exempted)
			totalTax = .1f;
		if ((imported) && (!exempted))
			totalTax = .15f;
		tax = totalTax * price;

	public String toString() {
		float p = price + tax;
		return qty + " " + prodName + " at " + p;

public class SalesTax {
	public static void main(String[] args) throws Exception {
		SalesTax st = new SalesTax();
		Scanner input = new Scanner(;
		ArrayList list = new ArrayList();
		int no = 1;
		while (true) {
			Tax tax = new Tax();
			System.out.println("Add Products: " + no);
			System.out.print("Quantity: ");
			int qty = input.nextInt();
			tax.qty = qty;
			System.out.print("Product Name: ");
			String prod =;
			tax.prodName = prod;
			System.out.print("Price: ");
			float p = input.nextFloat();
			tax.price = p;
			System.out.print("Is it Imported[y/n]: ");
			String imp =;
			if (imp.toLowerCase().equals("y"))
				tax.imported = true;
			System.out.print("Is it Exempted[y/n]: ");
			String exe =;
			if (exe.toLowerCase().equals("y"))
				tax.exempted = true;
			System.out.print("Add More Products [y/n]: ");
			String add =;
			if (add.toLowerCase().equals("n"))
		float tp = 0.0f;
		float tt = 0.0f;
		for (int i = 0; i < list.size(); i++) {
			Tax tax = list.get(i);
			tp += tax.price;
			tt +=;

		System.out.println("Sales Taxes:" + tt);
		System.out.println("Total: " + (tp + tt));




Add Prducts: 1
Quantity: 1
Product Name: Chocolate
Price: 15.00
Is it Imported[y/n]: y
Is it Exempted[y/n]: y
Add More Products [y/n]: n
1 Chocolate at 15.75(including tax)
Sales Taxes: 0.75
Total: 15.75



Posted on: November 17, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.