In this section, you will learn how to calculate the sales tax and print out the receipt details for the purchased items.
In this section, you will learn how to calculate the sales tax and print out the receipt details for the purchased items.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(System.in); ArrayListlist = 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 = input.next(); tax.prodName = prod; System.out.print("Price: "); float p = input.nextFloat(); tax.price = p; System.out.print("Is it Imported[y/n]: "); String imp = input.next(); if (imp.toLowerCase().equals("y")) tax.imported = true; System.out.print("Is it Exempted[y/n]: "); String exe = input.next(); if (exe.toLowerCase().equals("y")) tax.exempted = true; tax.calculateSalesTax(); list.add(tax); no++; System.out.print("Add More Products [y/n]: "); String add = input.next(); if (add.toLowerCase().equals("n")) break; } 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 += tax.tax; System.out.println(tax); } System.out.println("Sales Taxes:" + tt); System.out.println("Total: " + (tp + tt)); } }
Output: