Product calculation

 import java.text.DecimalFormat;


// Product excluding tax class

class ProductExcludingTax {

    private String productName;

    private int quantity;

    private double price;

    private double taxRate; // Tax rate as a decimal (e.g., 0.1 for 10% tax)


    // Constructor

    public ProductExcludingTax(String productName, int quantity, double price, double taxRate) {

        this.productName = productName;

        this.quantity = quantity;

        this.price = price;

        this.taxRate = taxRate;

    }


    // Calculate total amount including tax

    public double calculateTotalAmount() {

        double totalAmount = quantity * price;

        double taxAmount = totalAmount * taxRate;

        return totalAmount + taxAmount;

    }


    // Display method

    public void display() {

        DecimalFormat df = new DecimalFormat("#.##");

        double totalAmount = calculateTotalAmount();

        

        System.out.println("Product name: " + productName);

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

        System.out.println("Price per unit: $" + df.format(price));

        System.out.println("Tax rate: " + (taxRate * 100) + "%");

        System.out.println("Total amount (including tax): $" + df.format(totalAmount));

    }

}


// Product including tax class

class ProductIncludingTax {

    private String productName;

    private int quantity;

    private double originalPrice; // Price excluding tax

    private double taxAmount; // Tax amount

    private double taxRate; // Tax rate as a decimal (e.g., 0.1 for 10%)


    // Constructor

    public ProductIncludingTax(String productName, int quantity, double originalPrice, double taxRate) {

        this.productName = productName;

        this.quantity = quantity;

        this.originalPrice = originalPrice;

        this.taxRate = taxRate;

        this.taxAmount = originalPrice * taxRate;

    }


    // Display method

    public void display() {

        DecimalFormat df = new DecimalFormat("#.##");

        

        System.out.println("Product name: " + productName);

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

        System.out.println("Tax included price per unit: $" + df.format(originalPrice));

        System.out.println("Tax rate: " + (taxRate * 100) + "%");

        System.out.println("Tax amount: $" + df.format(taxAmount));

        System.out.println("Original price (excluding tax): $" + df.format(originalPrice - taxAmount));

    }

}


// Main class

public class Main {

    public static void main(String[] args) {

        // Example usage of ProductExcludingTax class

        ProductExcludingTax product1 = new ProductExcludingTax("Book", 2, 25.0, 0.08); // Tax rate of 8%

        product1.display();

        System.out.println(); // Empty line for separation

        

        // Example usage of ProductIncludingTax class

        ProductIncludingTax product2 = new ProductIncludingTax("Laptop", 1, 1500.0, 0.10); // Tax rate of 10%

        product2.display();

    }

}

Comments

Popular posts from this blog

Java. One

Bio Data

File operations