Assignment No: 02
Title:
Develop a Java program for an E-commerce order processing where some products are
initialized through multiple constructors, overloaded constructors, where users can input
some product details manually, the system computes total order cost dynamically, applies
discount policies based on conditions, and presents a detailed invoice summarizing the
purchase.
Objectives:
1. To implement object-oriented concepts like constructor overloading and
encapsulation.
2. To allow user input for product details using Scanner.
3. To compute total order cost dynamically.
4. To apply discount policies based on certain conditions.
5. To generate a detailed invoice for the customer.
Assignment Outcomes:
By the end of this assignment, students will be able to:
1. Understand and implement overloaded constructors in Java.
2. Accept user inputs for custom object creation.
3. Apply conditional logic to compute discounts and totals.
4. Generate a clean and structured invoice summary.
Tools and Environment Used:
• Language: Java
• Compiler/IDE: IntelliJ IDEA / Eclipse / VS Code / Terminal with javac
• JDK Version: JDK 11 or above
• OS: Windows/Linux/macOS
Theory:
Object-Oriented Programming (OOP)
1
This assignment uses several OOP principles:
• Classes and Objects: To represent products and orders.
• Encapsulation: Attributes of the class are private, accessed via public methods.
• Constructor Overloading: Multiple constructors in a class to allow flexible object
creation.
Constructors in Java
Definition:
A constructor in Java is a special method that is called automatically when an object is created.
It is used to initialize the object’s state (i.e., its variables).
Syntax:
class ClassName {
// Constructor
ClassName() {
// initialization code
}
}
Key Features:
• Has the same name as the class.
• No return type, not even void.
• Can be overloaded to provide multiple ways of initialization.
Types of Constructors:
Type Description
Default Constructor No arguments. Java provides one if no constructor is defined.
Parameterized Constructor Accepts arguments to initialize object with custom values.
Copy Constructor (not native A constructor that creates a new object by copying another. Must
in Java) be implemented manually.
2
Example:
class Product
{
String name;
double price;
// Default Constructor
Product()
{
name = "Unknown";
price = 0;
}
// Parameterized Constructor
Product(String name, double price)
{
this.name = name;
this.price = price;
}
}
Constructor Overloading
Definition:
Constructor Overloading occurs when a class has more than one constructor with different
parameter lists.
Syntax:
class Product
{
Product() { }
Product(String name) { }
Product(String name, double price) { }
}
Example:
class Product
{
String name;
double price;
int quantity;
// Constructor 1
Product(String name, double price)
{
this.name = name;
this.price = price;
this.quantity = 1;
}
3
// Constructor 2 (Overloaded)
Product(String name, double price, int quantity)
{
this.name = name;
this.price = price;
this.quantity = quantity;
}
}
Arrays in Java
Definition:
An Array is a fixed-size container that holds multiple elements of the same data type.
Syntax:
int[] numbers = new int[5]; // Declaration + initialization
String[] names = {"A", "B", "C"}; // Direct initialization
Example:
int[] prices = new int[3];
prices[0] = 100;
prices[1] = 200;
prices[2] = 300;
ArrayList in Java
Definition:
ArrayList is a resizable array, part of Java’s Collections Framework.
Syntax:
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
Features:
• Dynamic size
• Can hold objects
• Has many built-in methods like add(), remove(), get(), size()
4
Example:
ArrayList<Integer> nums = new ArrayList<>();
nums.add(10);
nums.add(20);
System.out.println(nums.get(0)); // Output: 10
Loops in the Program
for-each Loop
Definition:
The for-each loop (also known as enhanced for loop) is used to traverse through elements
in an array or collection (like ArrayList).
Syntax:
for (Type variable : collection) {
// use variable inside the loop
}
Example:
ArrayList<Product> products = new ArrayList<>();
// ...
for (Product p : products) {
totalCost += p.getTotalPrice();
}
Explanation:
• The for-each loop goes through each Product object (p) stored in the products
ArrayList.
• The getTotalPrice() method is called on each product to calculate the cumulative
totalCost.
Benefits:
• More readable and concise than traditional for loops.
• Avoids index-related bugs.
• Perfect for iterating over collections like ArrayList.
5
Example from Program:
Loop Through Products Using for-each:
double totalCost = 0;
for (Product p : products)
{
totalCost += p.getTotalPrice();
}
Print Product Details Using for-each:
for (Product p : products) {
p.displayProduct();
}
Conclusion:
This assignment demonstrates how constructor overloading, object-oriented programming, and
basic user interaction in Java can be used to simulate a real-world e-commerce system,
calculating totals dynamically and providing user-friendly output.
Algorithm:
1. Create a Product class with fields: name, price, quantity.
2. Overload constructors to initialize products:
o One with name & price
o One with name, price & quantity
3. In the main() method:
o Ask the user for number of products.
o Use a loop to collect product details.
o Store products in an ArrayList.
4. Calculate total cost of all products.
5. Apply discount rules:
o ₹5000 → 10% discount
o ₹3000 → 5% discount
6. Display invoice with:
o Product details
o Total amount
o Discount
o Final payable amount