0% found this document useful (0 votes)
48 views6 pages

Ecommerce Order Processing Full Explained

The document provides a complete Java program for e-commerce order processing, detailing the use of constructors, discounts, and invoice generation. It includes the full code with explanations for each command, the functionality of the Product and Order classes, and the program's flow from product creation to invoice generation. Additionally, it outlines how user input is handled and how discounts are calculated based on total order amounts.

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)
48 views6 pages

Ecommerce Order Processing Full Explained

The document provides a complete Java program for e-commerce order processing, detailing the use of constructors, discounts, and invoice generation. It includes the full code with explanations for each command, the functionality of the Product and Order classes, and the program's flow from product creation to invoice generation. Additionally, it outlines how user input is handled and how discounts are calculated based on total order amounts.

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
You are on page 1/ 6

Java Program – E-commerce Order Processing (Constructors, Discounts,

Invoice)
This document contains the full Java program, a simple explanation of every command used,
and the program flow.

1) Full Program Code (with Line Numbers)


1 | import java.util.Scanner;
2 |
3 | class Product {
4 | String name;
5 | double price;
6 | int quantity;
7 |
8 | // Default constructor
9 | Product() {
10 | name = "Unknown";
11 | price = 0.0;
12 | quantity = 0;
13 | }
14 |
15 | // Overloaded constructor with 2 parameters
16 | Product(String n, double p) {
17 | name = n;
18 | price = p;
19 | quantity = 1; // Default quantity
20 | }
21 |
22 | // Overloaded constructor with 3 parameters
23 | Product(String n, double p, int q) {
24 | name = n;
25 | price = p;
26 | quantity = q;
27 | }
28 |
29 | // Method to calculate cost of one product type
30 | double getCost() {
31 | return price * quantity;
32 | }
33 | }
34 |
35 | class Order {
36 | Product[] products;
37 | int count;
38 |
39 | Order(int size) {
40 | products = new Product[size];
41 | count = 0;
42 | }
43 |
44 | void addProduct(Product p) {
45 | if (count < products.length) {
46 | products[count] = p;
47 | count++;
48 | } else {
49 | System.out.println("Order is full. Cannot add
more products.");
50 | }
51 | }
52 |
53 | double calculateTotal() {
54 | double total = 0.0;
55 | for (int i = 0; i < count; i++) {
56 | total += products[i].getCost();
57 | }
58 | return total;
59 | }
60 |
61 | double calculateDiscount(double total) {
62 | double discount = 0.0;
63 | if (total > 50000) {
64 | discount = total * 0.15; // 15%
65 | } else if (total > 20000) {
66 | discount = total * 0.10; // 10%
67 | } else if (total > 5000) {
68 | discount = total * 0.05; // 5%
69 | }
70 | return discount;
71 | }
72 |
73 | void generateInvoice() {
74 | System.out.println("\n----- INVOICE -----");
75 | for (int i = 0; i < count; i++) {
76 | System.out.println(products[i].name + " x " +
products[i].quantity +
77 | " = Rs. " + products[i].getCost());
78 | }
79 | double total = calculateTotal();
80 | double discount = calculateDiscount(total);
81 | double finalAmount = total - discount;
82 |
83 | System.out.println("Total: Rs. " + total);
84 | System.out.println("Discount: Rs. " + discount);
85 | System.out.println("Final Amount after discount:
Rs. " + finalAmount);
86 | System.out.println("-------------------\n");
87 | }
88 | }
89 |
90 | public class EcommerceOrderProcessing {
91 | public static void main(String[] args) {
92 | Scanner sc = new Scanner(System.in);
93 |
94 | Order order = new Order(5);
95 |
96 | // Example of products using different constructors
97 | Product p1 = new Product("Laptop", 45000, 1);
98 | Product p2 = new Product("Mouse", 500); //
quantity defaults to 1
99 | Product p3 = new Product(); //
Unknown, 0.0, 0 (not added)
100 |
101 | // Add products initialized using constructors
102 | order.addProduct(p1);
103 | order.addProduct(p2);
104 |
105 | // Taking user input for product
106 | System.out.print("Enter Product Name: ");
107 | String name = sc.nextLine();
108 | System.out.print("Enter Price: ");
109 | double price = sc.nextDouble();
110 | System.out.print("Enter Quantity: ");
111 | int quantity = sc.nextInt();
112 |
113 | Product userProduct = new Product(name, price,
quantity);
114 | order.addProduct(userProduct);
115 |
116 | // Generate invoice
117 | order.generateInvoice();
118 |
119 | sc.close();
120 | }
121 | }

2) Meaning of Each Command (in Simple Words)


import java.util.Scanner; → Brings the Scanner tool so we can read input from the
keyboard.

class Product { ... } → Defines a blueprint for a product (what data it has and what it can
do).

String / double / int → Data types: text, decimal number, whole number.

// comment → A note for humans; ignored by the computer.

Product() { ... } → Default constructor: sets safe default values when no inputs are given.

Product(String n, double p) → Overloaded constructor: sets name and price; quantity


becomes 1 by default.

Product(String n, double p, int q) → Overloaded constructor: sets name, price, and


quantity from inputs.

this → Refers to the current object (used often to avoid confusion between field names and
parameters).

double getCost() { return price * quantity; } → A method that multiplies price and
quantity and returns the result.

class Order { ... } → A basket to hold multiple Product items and do billing work.

Product[] products → An array (list) that can store many Product objects.

int count → How many products are actually added into the order.

Order(int size) → Constructor that creates an empty array with a fixed capacity.

void addProduct(Product p) → Adds a product into the order if there is space.

if (count < products.length) { ... } → Checks if there is room left in the array.

System.out.println(...) → Prints a line of text on the screen (moves to next line).

System.out.print(...) → Prints text but stays on the same line.

for (int i = 0; i < count; i++) → A loop that repeats from 0 up to count-1 to visit each
product added.

total += products[i].getCost(); → Accumulate the total cost by adding each product's cost.
double calculateDiscount(double total) → Figures out how much discount to give based
on the total.

if / else if / else → Decision making: run different blocks depending on the condition.

return value; → Sends a value back from a method to the caller.

new Type(...) → Creates a new object using a constructor.

Scanner sc = new Scanner(System.in); → Prepares a Scanner that reads from the


keyboard.

sc.nextLine() → Reads a whole line of text from the user.

sc.nextDouble() → Reads a decimal number from the user.

sc.nextInt() → Reads a whole number from the user.

order.generateInvoice(); → Calls the invoice printing method.

sc.close(); → Closes the Scanner to free resources.

3) What Each Block Does


• Product class: holds item details and can compute its own cost.

• Order class: stores products, sums their costs, computes discount, and prints the invoice.

• Main method: creates products (via constructors), reads one product from the user, and
generates the invoice.

4) Flow of the Program


1. Start program (main method runs).

2. Create an Order with capacity 5.

3. Create p1 (name+price+qty), p2 (name+price), and p3 (default).

4. Add p1 and p2 to the order (p3 is just to show default constructor).

5. Ask user to enter product name, price, quantity.

6. Create userProduct from input and add to order.

7. Order.calculateTotal() adds cost of each added product.

8. Order.calculateDiscount(total) decides discount slab (15%, 10%, 5%, or none).

9. Invoice prints each product line, total, discount, and final amount.

10. End program and close Scanner.


5) Textual Flow Diagrams
A) Constructor Flow

[Start] → [Product Class] → [Choose Constructor]


• Default (no inputs)
• Overloaded (name, price)
• Overloaded (name, price, qty)
→ [Product Object Ready]

B) Invoice Processing Flow

[Input Product(s)] → [Compute Each Cost] → [Sum Total]


→ [Apply Discount Slab] → [Final Amount] → [Print Invoice]

You might also like