Product calculation

 import java.util.InputMismatchException;

import java.util.Scanner;


public class ProductCalculation {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String productName;

        int quantity = 0;

        double price = 0.0, taxRate = 0.0, taxAmount = 0.0, totalPrice = 0.0;


        // Get product information

        System.out.println("Product Calculation");

        System.out.print("P. Name: ");

        productName = scanner.nextLine();


        // Get quantity with input validation

        while (quantity <= 0) {

            try {

                System.out.print("Quantity: ");

                quantity = scanner.nextInt();

                if (quantity <= 0) {

                    System.out.println("Quantity must be a positive number. Please try again.");

                }

            } catch (InputMismatchException e) {

                System.out.println("Invalid input. Please enter a number for quantity.");

                scanner.nextLine(); // Clear the scanner buffer

            }

        }


        // Get price with input validation

        while (price <= 0) {

            try {

                System.out.print("Price: ");

                price = scanner.nextDouble();

                if (price <= 0) {

                    System.out.println("Price must be a positive number. Please try again.");

                }

            } catch (InputMismatchException e) {

                System.out.println("Invalid input. Please enter a number for price.");

                scanner.nextLine(); // Clear the scanner buffer

            }

        }


        // Get tax rate with input validation (assuming tax is a percentage)

        while (taxRate < 0) {

            try {

                System.out.print("Tax (%): ");

                taxRate = scanner.nextDouble();

                if (taxRate < 0) {

                    System.out.println("Tax rate must be a non-negative number. Please try again.");

                }

            } catch (InputMismatchException e) {

                System.out.println("Invalid input. Please enter a number for tax rate.");

                scanner.nextLine(); // Clear the scanner buffer

            }

        }


        // Calculate tax amount and total price

        taxAmount = price * taxRate / 100;

        totalPrice = price * quantity + taxAmount;


        // Display product information with tax

        System.out.println("\nInclude Tax:");

        System.out.println("P. Name: " + productName);

        System.out.println("Quantity: " + quantity);

        System.out.printf("Tax with price: $%.2f\n", price + taxAmount);

        System.out.printf("Tax: $%.2f\n", taxAmount);

        System.out.printf("Total amount: $%.2f\n", totalPrice);


        // Display product information without tax

        System.out.println("\nWithout Tax:");

        System.out.println("P. Name: " + productName);

        System.out.println("Quantity: " + quantity);

        System.out.printf("Original Price: $%.2f\n", price * quantity);

   }

}



Output 




Comments

Popular posts from this blog

Bio Data

File operation