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();