Friday, April 5, 2013

Sale Calculator program

The SaleCalulator program allows you to input quantity of items sold, unit price, discount rate, and tax rate. Then the program calculates and outputs the subtotal, discount amount, taxable amount, tax amount, and total of sales.

SaleCalculator source code:

1 import java.util.Scanner;
2  class SaleCalculator{
3     public static void main(String[] args){
4       int qty;
5          double unitPrice;
6          double subtotal;
7          double discountRate;
8          double discountAmount;
9          double taxRate;
10        double taxableAmount;
11        double taxAmount;
12        double total;
13        Scanner sc=new Scanner(System.in);

14        System.out.print("\nEnter quantity of items sold:");
15        qty=sc.nextInt();
16        System.out.print("Enter unit price:");
17        unitPrice=sc.nextDouble();
17        System.out.print("Enter discount rate:");
19        discountRate=sc.nextDouble();
20        System.out.print("Enter tax rate:");
21        taxRate=sc.nextDouble();

22        subtotal=qty*unitPrice;
23        discountAmount=subtotal*discountRate/100.0;
24        taxableAmount=subtotal-discountAmount;
25        taxAmount=taxableAmount*taxRate/100.0;
26        total=taxableAmount+taxAmount;
27        System.out.println();
28        System.out.format("%-25s %-10d\n", "Quantity of items sold:",qty);
29        System.out.format("%-25s %-10.3f\n", "Unit price:",unitPrice);
30        System.out.println("--------------------------------------\n");
31        System.out.format("%-25s %10.3f\n", "Subtotal:",subtotal);
32        System.out.format("%-25s-%10.3f\n","Discount amount:",discountAmount);
32        System.out.println("--------------------------------------");
33        System.out.format("%-25s %10.3f\n","Taxable amount:",taxableAmount);
34        System.out.format("%-25s+%10.3f\n","Tax amount:+",taxAmount);
35        System.out.format("--------------------------------------\n");
36        System.out.format("%-25s %10.3f\n","Total:",total);  
37    }
38 }

Program Output
Sale calculator program in Java



Code Explanation:
1 include Scanner class to the program.
2 open the SaleCalculator class block.
3 open the main method block.
4 declare local variable qty to store quantity of items sold.
5 declare the local variable unitPrice to store the unit price of the product.
6 declare the local variable subtotal to store the sales subtotal.
7 declare the local variable discountRate to store the discount rate of sales.
8 declare the local variable discountAmount to store the mount of sales discount.
9 declare the local variable taxRate to store the tax rate of sales.
10 declare the local variable taxableAmount to store the discounted total.
11 declare the local variable taxAmount to store the amount to tax.
12 declare the local variable total to store the total of sales.
13 create Scanner object sc.
14-21 get quantity, unit price, discount rate, and tax rate input by the user.
22 calculate subtoal.
23 calculate discoutn amount.
24 calculate taxable amount or discounted total.
25 calculate tax amount.
26 calculate total
27 print a new line.
28-36 display quantity of items sold, unit price, subtotal, discount amount, taxable amount, tax amount, and total of sales on the screen.
37 close the main method.
38 close the SaleCalculator class.

No comments:

Post a Comment