0% found this document useful (0 votes)
12 views4 pages

For Multiple Products Practical 2

This Java program allows users to input multiple products for purchase, capturing details such as name, price, and quantity. It calculates the total cost and applies discounts based on the total amount spent, providing an invoice with the final amount after discounts. The program utilizes a Product class to manage product details and cost calculations.

Uploaded by

shinushinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

For Multiple Products Practical 2

This Java program allows users to input multiple products for purchase, capturing details such as name, price, and quantity. It calculates the total cost and applies discounts based on the total amount spent, providing an invoice with the final amount after discounts. The program utilizes a Product class to manage product details and cost calculations.

Uploaded by

shinushinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

For Multiple Products

import java.util.Scanner;

// Product class

class Product {

String name;

double price;

int quantity;

// Constructor to initialize product

Product(String name, double price, int quantity) {

this.name = name;

this.price = price;

this.quantity = quantity;

// Method to calculate cost

double getCost() {

return price * quantity;

// Main class

public class MultiProductECommerce {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of products you want to buy: ");

int n = sc.nextInt();

sc.nextLine(); // consume leftover newline

// Array to store products

Product[] products = new Product[n];

// Loop to take input for each product

for (int i = 0; i < n; i++) {

System.out.println("\nEnter details of product " + (i + 1) + ":");

System.out.print("Product name: ");

String name = sc.nextLine();

System.out.print("Product price: ");

double price = sc.nextDouble();

System.out.print("Product quantity: ");

int quantity = sc.nextInt();

sc.nextLine(); // consume newline

// Create product and store in array


products[i] = new Product(name, price, quantity);

// Calculate total cost of all products

double total = 0;

for (int i = 0; i < n; i++) {

total += products[i].getCost();

// Apply discount

double finalAmount;

if (total > 5000) {

finalAmount = total * 0.90; // 10% discount

} else if (total > 2000) {

finalAmount = total * 0.95; // 5% discount

} else {

finalAmount = total; // no discount

// Print invoice

System.out.println("\n------ Invoice ------");

for (int i = 0; i < n; i++) {

Product p = products[i];

System.out.println(p.name + " | Price: " + p.price + " | Qty: " + p.quantity + " | Cost: " +
p.getCost());

}
System.out.println("---------------------");

System.out.println("Total (before discount): " + total);

System.out.println("Final Amount (after discount): " + finalAmount);

System.out.println("---------------------");

sc.close();

You might also like