Java Program – E-commerce Order Processing
1. Introduction
This document explains a Java program for E-commerce Order Processing where:
- Some products are initialized using multiple constructors (default and overloaded).
- Users can manually enter product details at runtime.
- The system calculates total order cost dynamically.
- Discounts are applied based on conditions.
- A detailed invoice is generated summarizing the purchase.
2. Java Program Code
import java.util.Scanner;
class Product {
String name;
double price;
int quantity;
// Default constructor
Product() {
name = "Unknown";
price = 0.0;
quantity = 0;
}
// Overloaded constructor with 2 parameters
Product(String n, double p) {
name = n;
price = p;
quantity = 1; // Default quantity
}
// Overloaded constructor with 3 parameters
Product(String n, double p, int q) {
name = n;
price = p;
quantity = q;
}
// Method to calculate cost of one product type
double getCost() {
return price * quantity;
}
}
class Order {
Product[] products;
int count;
Order(int size) {
products = new Product[size];
count = 0;
}
void addProduct(Product p) {
if (count < products.length) {
products[count] = p;
count++;
} else {
System.out.println("Order is full. Cannot add more products.");
}
}
double calculateTotal() {
double total = 0.0;
for (int i = 0; i < count; i++) {
total += products[i].getCost();
}
return total;
}
double applyDiscount(double total) {
if (total > 5000) {
return total * 0.90; // 10% discount
} else if (total > 2000) {
return total * 0.95; // 5% discount
} else {
return total; // no discount
}
}
void generateInvoice() {
System.out.println("\n----- INVOICE -----");
for (int i = 0; i < count; i++) {
System.out.println(products[i].name + " x " + products[i].quantity +
" = Rs. " + products[i].getCost());
}
double total = calculateTotal();
double finalAmount = applyDiscount(total);
System.out.println("Total: Rs. " + total);
System.out.println("Final Amount after discount: Rs. " + finalAmount);
System.out.println("-------------------\n");
}
}
public class EcommerceOrderProcessing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Order order = new Order(5);
// Example of products using different constructors
Product p1 = new Product("Laptop", 45000, 1);
Product p2 = new Product("Mouse", 500); // quantity defaults to 1
Product p3 = new Product();
// Add products initialized using constructors
order.addProduct(p1);
order.addProduct(p2);
// Taking user input for product
System.out.print("Enter Product Name: ");
String name = sc.nextLine();
System.out.print("Enter Price: ");
double price = sc.nextDouble();
System.out.print("Enter Quantity: ");
int quantity = sc.nextInt();
Product userProduct = new Product(name, price, quantity);
order.addProduct(userProduct);
// Generate invoice
order.generateInvoice();
sc.close();
}
}
3. Explanation of Key Concepts
🔹 Constructors
- Default Constructor → Initializes with default values ('Unknown', 0, 0).
- Overloaded Constructor (2 parameters) → Takes name and price, assumes quantity = 1.
- Overloaded Constructor (3 parameters) → Takes name, price, and quantity.
🔹 Methods
- getCost() → Returns total cost of one product type.
- calculateTotal() → Adds up cost of all products.
- applyDiscount() → Applies discount policy based on total.
- generateInvoice() → Displays all products, subtotal, and final discounted total.
🔹 Discount Policy
- If total > 5000 → 10% discount
- If total > 2000 → 5% discount
- Else → No discount
4. Sample Output
Enter Product Name: Keyboard
Enter Price: 1500
Enter Quantity: 2
----- INVOICE -----
Laptop x 1 = Rs. 45000.0
Mouse x 1 = Rs. 500.0
Keyboard x 2 = Rs. 3000.0
Total: Rs. 48500.0
Final Amount after discount: Rs. 43650.0
-------------------
5. Flow of Execution (Text Diagram)
Constructor Flow
User runs program
↓
Default constructor → Product p3
Overloaded constructor (3 params) → Product p1
Overloaded constructor (2 params) → Product p2
Manual input product → Product userProduct
Invoice Generation Flow
Products added to Order
↓
calculateTotal()
↓
applyDiscount()
↓
generateInvoice() → prints invoice with total and discount