0% found this document useful (0 votes)
28 views11 pages

E ComerceSystem

The document outlines the development of an E-Commerce System led by Lendyl Tyron Estabaya, focusing on managing products and orders through classes like Product, Order, and Customer. It includes requirements for encapsulation, inheritance, polymorphism, and interfaces, along with features for adding, removing, and updating products, placing orders, and applying discounts. The implementation details include various product subclasses, an admin class for product management, and a main class to run the application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views11 pages

E ComerceSystem

The document outlines the development of an E-Commerce System led by Lendyl Tyron Estabaya, focusing on managing products and orders through classes like Product, Order, and Customer. It includes requirements for encapsulation, inheritance, polymorphism, and interfaces, along with features for adding, removing, and updating products, placing orders, and applying discounts. The implementation details include various product subclasses, an admin class for product management, and a main class to run the application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

BSIT 2-D

Leader:

Estabaya, Lendyl Tyron

Members:

Subido, Romnick Jay


Miranda, Joshua
Leonen, Lance Lester
Etrata, Mark Christian

E-Commerce System

Objective: Develop a system for managing an online store with products and orders.

Requirements:

• Classes and Objects: Implement classes for Product, Order, and Customer.

• Encapsulation: Use private fields with getter and setter methods.

• Inheritance: Create subclasses for Electronics, Clothing, etc., extending Product.

• Polymorphism: Implement polymorphic methods for different product types.

• Interfaces: Define an interface Discountable for applying discounts.

• Collections: Use collections to manage products and orders.

Features:

• Add, remove, and update products.

• Place and track orders.

• Apply discounts and generate invoices.


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.text.*;

//Customer Class
class Customer {
private String name;
private String address;

public Customer(String name, String address) {


this.name = name;
this.address = address;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}
}

// Interface
interface Discountable {
double getDiscountedPrice();
}

// Base class for all products


abstract class Product implements Discountable{
private String name;
private double price;
private String Brand;

public Product(String name, double price, String Brand) {


this.name = name;
this.price = price;
this.Brand = Brand;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public double getPrice() {


return price;
}
public void setPrice(double price) {
this.price = price;
}

public String getBrand() {


return Brand;
}

public void setBrand(String Brand) {


this.Brand = Brand;
}

public abstract double getOriginalPrice(); // Polymorphism


}

// Subclass for electronics products


class Electronics extends Product {

public Electronics(String name, double price, String Brand) {


super(name, price, Brand);
}

@Override
public double getDiscountedPrice() {
return getPrice() * 0.9;
}

public double getOriginalPrice() {


return getPrice();
}
}

// Subclass for clothing products


class Clothing extends Product {
private String size;

public Clothing(String name, double price, String size, String Brand) {


super(name, price, Brand);
this.size = size;
}

@Override
public double getDiscountedPrice() {
return getPrice() * 0.95;
}

public double getOriginalPrice() {


return getPrice();
}

public void setSize(String newSize) {


this.size = newSize;
}

public String getSize() {


return size;
}
}

// Subclass for other types of products


class OtherProducts extends Product {
private String description;

public OtherProducts(String name, double price, String description,


String Brand) {
super(name, price, Brand);
this.description = description;
}

@Override
public double getDiscountedPrice() {
return getPrice() * 0.85; // Example: 15% discount for other
products
}

public double getOriginalPrice() {


return getPrice();
}

public String getDescription() {


return description;
}

public void setDescription(String description) {


this.description = description;
}
}

// Order class
class Order {
private static int orderCounter = 1;
private int orderId;
private Customer customer;
private List<Product> items;

public Order(Customer customer) {


this.customer = customer;
this.items = new ArrayList<>();
this.orderId = orderCounter++;
}

public void addItem(Product product) {


items.add(product);
}

public double getTotalPrice() {


double totalPrice = 0;
for (Product product : items) {
totalPrice += product.getDiscountedPrice();
}
return totalPrice;
}

public double getOrigTotalPrice() {


double OrigtotalPrice = 0;
for (Product product : items) {
OrigtotalPrice += product.getOriginalPrice();
}
return OrigtotalPrice;
}
public int getOrderId() {
return orderId;
}

public void printInvoice() {


System.out.println("-----------------------------------------------
------------------------");
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Order ID: " + orderId);
System.out.println("Customer: " + customer.getName());
System.out.println("Address: " + customer.getAddress());
System.out.println("Items:");
System.out.println("Original Price:");
for (Product product : items) {
System.out.println("- " + product.getName() + ": $" +
(df.format(product.getOriginalPrice())));
}
System.out.println("Total: $" + getOrigTotalPrice());
System.out.println("-----------------------------------------------
------------------------");
System.out.println("Discounted Price: ");
for (Product product : items) {
System.out.println("- " + product.getName() + ": $" +
(df.format(product.getDiscountedPrice())));
}
System.out.println("Total: $" + (df.format(getTotalPrice())));
System.out.println("-----------------------------------------------
------------------------");
}
}

// Admin class to manage products


class Admin {
private String username;
private String password;

// Constructor to initialize admin credentials


public Admin(String username, String password) {
this.username = username;
this.password = password;
}

// Method to authenticate admin login


public boolean authenticate(String inputUsername, String inputPassword)
{
return username.equals(inputUsername) &&
password.equals(inputPassword);
}

// Add a new product to the available products list


public void addProduct(List<Product> availableProducts, Product
product) {
availableProducts.add(product);
System.out.println(product.getName() + " has been added to the
product list.");
}

// Remove an existing product from the available products list


public void removeProduct(List<Product> availableProducts, Product
product) {
if (availableProducts.contains(product)) {
availableProducts.remove(product);
System.out.println(product.getName() + " has been removed from
the product list.");
} else {
System.out.println("Product not found in the product list.");
}
}

// Update an existing product's details


public void updateProduct(List<Product> availableProducts) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the product name to update: ");
String productName = scanner.nextLine();

Product productToUpdate = null;


// Search for the product
for (Product product : availableProducts) {
if (product.getName().equalsIgnoreCase(productName)) {
productToUpdate = product;
break;
}
}

// If the product is found, update its details


if (productToUpdate != null) {
System.out.println("Product found: " +
productToUpdate.getName());

// Ask for the new details


System.out.print("Enter new name (leave empty to keep current):
");
String newName = scanner.nextLine();
if (!newName.isEmpty()) {
productToUpdate.setName(newName);
}

System.out.print("Enter new price (leave empty to keep


current): ");
String newPrice = scanner.nextLine();
if (!newPrice.isEmpty()) {
productToUpdate.setPrice(Double.parseDouble(newPrice));
}

if (productToUpdate instanceof Electronics) {


System.out.print("Enter new brand (leave empty to keep
current): ");
String newBrand = scanner.nextLine();
if (!newBrand.isEmpty()) {
productToUpdate.setBrand(newBrand);
}
} else if (productToUpdate instanceof Clothing) {
System.out.print("Enter new size (leave empty to keep
current): ");
String newSize = scanner.nextLine();
if (!newSize.isEmpty()) {
((Clothing) productToUpdate).setSize(newSize);
}
}

System.out.println("Product updated successfully!");


} else {
System.out.println("Product not found.");
}
}
}

public class Main {


private List<Order> activeOrders = new ArrayList<>();
private List<Product> availableProducts = new ArrayList<>();

public Main() {
availableProducts.add(new Electronics("iPhone 14", 999, "Apple"));
availableProducts.add(new Clothing("T-Shirt", 29.99, "M",
"Uniqlo"));
availableProducts.add(new Electronics("Samsung Galaxy S23", 899,
"Samsung"));
availableProducts.add(new Clothing("Jeans", 49.99, "L", "Levi"));
availableProducts.add(new OtherProducts("Book", 19.99, "Paperback
edition of a novel", "Generic"));
}

// The rest of the methods remain unchanged


public void placeOrder(Customer customer) {
Order order = new Order(customer);
Scanner scanner = new Scanner(System.in);

// Display electronics products


System.out.println("-----------------------------------------------
------------------------");
System.out.println("Electronics:");
for (int i = 0; i < availableProducts.size(); i++) {
Product product = availableProducts.get(i);
if (product instanceof Electronics) {
System.out.println((i + 1) + ". " + product.getBrand() + "
" + product.getName() + " - $" + product.getPrice());
}
}

// Display clothing products


System.out.println("-----------------------------------------------
------------------------");
System.out.println("Clothing:");
for (int i = 0; i < availableProducts.size(); i++) {
Product product = availableProducts.get(i);
if (product instanceof Clothing) {
System.out.println((i + 1) + ". " + product.getBrand() + "
" + product.getName() + " - $" + product.getPrice());
}
}

// Display other products


System.out.println("-----------------------------------------------
------------------------");
System.out.println("Other Products:");
for (int i = 0; i < availableProducts.size(); i++) {
Product product = availableProducts.get(i);
if (product instanceof OtherProducts) {
System.out.println((i + 1) + ". " + product.getBrand() + "
" + product.getName() + " - $" + product.getPrice() + " (" +
((OtherProducts) product).getDescription() + ")");
}
}

System.out.println("Enter the product numbers to add to your order


(comma-separated), or 0 to finish:");
String input = scanner.nextLine();
String[] selectedProducts = input.split(",");
for (String selection : selectedProducts) {
int index;
try {
index = Integer.parseInt(selection.trim()) - 1;
if (index >= 0 && index < availableProducts.size()) {
order.addItem(availableProducts.get(index));
}
} catch (NumberFormatException e) {
System.out.println("Invalid selection: " + selection);
}
}

if (order.getTotalPrice() > 0) {
activeOrders.add(order);
System.out.println("Order placed successfully! Order ID: " +
order.getOrderId());
order.printInvoice();
System.out.println("-------------------------------------------
----------------------------");
} else {
System.out.println("No items selected. Order not placed.");
}
}

public void trackOrder(int orderId) {


for (Order order : activeOrders) {
if (order.getOrderId() == orderId) {
System.out.println("---------------------------------------
--------------------------------");
System.out.println("Order Details:");
order.printInvoice();
System.out.println("---------------------------------------
--------------------------------");
return;
}
}
System.out.println("Order with ID " + orderId + " not found.");
}

public void adminLogin() {


Scanner scanner = new Scanner(System.in);
Admin admin = new Admin("admin", "admin123");

System.out.print("Enter Admin Username: ");


String username = scanner.nextLine();
System.out.print("Enter Admin Password: ");
String password = scanner.nextLine();

if (admin.authenticate(username, password)) {
System.out.println("Admin login successful!");
adminMenu(admin);
} else {
System.out.println("Invalid credentials. Access denied.");
}
}
public void adminMenu(Admin admin) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nAdmin Menu:");
System.out.println("1. Add Product");
System.out.println("2. Remove Product");
System.out.println("3. Update Product");
System.out.println("4. Return to Main Menu");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();

// Admin options to add/remove/update products


switch (choice) {
case 1:
System.out.println("Enter product type (1: Electronics,
2: Clothing): ");
int type = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter product name: ");
String name = scanner.nextLine();
System.out.print("Enter product price: ");
double price = scanner.nextDouble();

// Add new product based on type


if (type == 1) {
System.out.print("Enter brand: ");
String Brand = scanner.next();
admin.addProduct(availableProducts, new
Electronics(name, price, Brand));
} else if (type == 2) {
System.out.print("Enter Brand: ");
String Brand = scanner.next();
System.out.print("Enter size: ");
String size = scanner.next();
admin.addProduct(availableProducts, new
Clothing(name, price, size, Brand));
}
break;

case 2:
System.out.println("Enter the product name to remove:
");
scanner.nextLine(); // Consume newline
String productName = scanner.nextLine();
Product productToRemove = null;
// Find the product to remove
for (Product product : availableProducts) {
if
(product.getName().equalsIgnoreCase(productName)) {
productToRemove = product;
break;
}
}
// Remove the product if found
if (productToRemove != null) {
admin.removeProduct(availableProducts,
productToRemove);
} else {
System.out.println("Product not found.");
}
break;

case 3:
admin.updateProduct(availableProducts); // Update
product details
break;

case 4:
return; // Return to the main menu
default:
System.out.println("Invalid choice. Please try
again.");
}
}
}

public void run() {


Scanner scanner = new Scanner(System.in);

System.out.println("Welcome to the E-Commerce System");


System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Enter your address:");
String address = scanner.nextLine();
Customer customer = new Customer(name, address);

while (true) {
System.out.println("\nMain Menu:");
System.out.println("1. Place an Order");
System.out.println("2. Track an Order");
System.out.println("3. Admin Login");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
placeOrder(customer);
break;
case 2:
System.out.print("Enter the Order ID to track: ");
int orderId = scanner.nextInt();
trackOrder(orderId);
break;
case 3:
adminLogin();
break;
case 4:
System.out.println("Thank you for using the E-Commerce
System!");
return;
default:
System.out.println("Invalid choice. Please try
again.");
}
}
}

public static void main(String[] args) {


new Main().run();
}
}

You might also like