[go: up one dir, main page]

0% found this document useful (0 votes)
26 views4 pages

E-Commerce Assignment

The document contains a Java implementation of an e-commerce system with classes for Product, Customer, and Order. It demonstrates creating products, adding them to a customer's shopping cart, calculating the total cost, and generating an order summary. The main method showcases the functionality of these classes in a simple e-commerce scenario.

Uploaded by

belovedvince
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)
26 views4 pages

E-Commerce Assignment

The document contains a Java implementation of an e-commerce system with classes for Product, Customer, and Order. It demonstrates creating products, adding them to a customer's shopping cart, calculating the total cost, and generating an order summary. The main method showcases the functionality of these classes in a simple e-commerce scenario.

Uploaded by

belovedvince
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/ 4

import com.ecommerce.

Product;
import com.ecommerce.Customer;
import com.ecommerce.orders.Order;

public class Main {


public static void main(String[] args) {
// Create products
Product product1 = new Product(1, "Laptop", 1200.00);
Product product2 = new Product(2, "Headphones", 150.00);
Product product3 = new Product(3, "Mouse", 25.00);

// Display products
System.out.println("Available Products:");
System.out.println(product1);
System.out.println(product2);
System.out.println(product3);

// Create a customer
Customer customer = new Customer(101, "John Doe");

// Customer adds products to cart


customer.addProductToCart(product1);
customer.addProductToCart(product2);

// Display cart total


System.out.println("Cart Total: $" + customer.calculateTotalCost());

// Place an order
Order order = new Order(5001, customer, customer.getShoppingCart());
order.generateOrderSummary();
}
}

package com.ecommerce;

import java.util.ArrayList;
import java.util.List;

public class Customer {


private int customerID;
private String name;
private List<Product> shoppingCart;

public Customer(int customerID, String name) {


this.customerID = customerID;
this.name = name;
this.shoppingCart = new ArrayList<>();
}

public int getCustomerID() {


return customerID;
}

public String getName() {


return name;
}

public void addProductToCart(Product product) {


shoppingCart.add(product);
System.out.println(product.getName() + " added to cart.");
}

public void removeProductFromCart(Product product) {


if (shoppingCart.remove(product)) {
System.out.println(product.getName() + " removed from cart.");
} else {
System.out.println(product.getName() + " not found in cart.");
}
}

public double calculateTotalCost() {


return shoppingCart.stream().mapToDouble(Product::getPrice).sum();
}

public List<Product> getShoppingCart() {


return shoppingCart;
}
}

package com.ecommerce;

public class Product {


private int productID;
private String name;
private double price;

public Product(int productID, String name, double price) {


this.productID = productID;
this.name = name;
this.price = price;
}
public int getProductID() {
return productID;
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}

@Override
public String toString() {
return "Product{" +
"productID=" + productID +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}

package com.ecommerce.orders;

import com.ecommerce.Customer;
import com.ecommerce.Product;

import java.util.List;

public class Order {


private int orderID;
private Customer customer;
private List<Product> products;
private double total;

public Order(int orderID, Customer customer, List<Product> products) {


this.orderID = orderID;
this.customer = customer;
this.products = products;
this.total = products.stream().mapToDouble(Product::getPrice).sum();
}

public int getOrderID() {


return orderID;
}
public Customer getCustomer() {
return customer;
}

public double getTotal() {


return total;
}

public void generateOrderSummary() {


System.out.println("Order ID: " + orderID);
System.out.println("Customer: " + customer.getName());
System.out.println("Products:");
for (Product product : products) {
System.out.println(" - " + product.getName() + ": $" + product.getPrice());
}
System.out.println("Total: $" + total);
}
}

You might also like