PRODUCT CALCULATION

PROGRAM:-

class Product {
    private String name;
    private double price;
    private int quantity;
    private double taxRate;
    public Product(String name, double price, int quantity, double taxRate) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
        this.taxRate = taxRate;
    }
    public double calculateTotalCost() {
        double subtotal = price * quantity;
        double taxAmount = subtotal * taxRate;
        double totalCost = subtotal + taxAmount;
        return totalCost;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    public double getTaxRate() {
        return taxRate;
    }
    public void setTaxRate(double taxRate) {
        this.taxRate = taxRate;
    }
}
public class ProductCalculation {
    public static void main(String[] args) {
        Product product = new Product("Smartphone", 999.99, 2, 0.08); 
        double totalCost = product.calculateTotalCost();
        System.out.println("Product: " + product.getName());
        System.out.println("Price per unit: $" + product.getPrice());
        System.out.println("Quantity: " + product.getQuantity());
        System.out.println("Tax rate: " + (product.getTaxRate() * 100) + "%");
        System.out.println("Total cost (including tax): $" + totalCost);
    }
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class BookPriceCalculator {
    public static void main(String[] args) {
        String productName = "Book";
        int quantity = 5;
        double taxRate = 0.1;
        double originalPrice = 20.0;
        double taxAmount = originalPrice * taxRate;
        double totalPrice = (originalPrice + taxAmount) * quantity;
        System.out.println("Product name: " + productName);
        System.out.println("Quantity: " + quantity);
        System.out.println("Original Price per book: $" + originalPrice);
        System.out.println("Tax Rate: " + (taxRate * 100) + "%");
        System.out.println("Tax Amount per book: $" + taxAmount);
        System.out.println("Total Price including tax: $" + totalPrice);
    }
}

-----------------------------------------------------------------------------------------------------------------------------
Output:-










Comments

Popular Posts