Shopping Cart Simulation

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Code Explanation: Shopping Cart Simulation

This code simulates a simple e-commerce shopping cart. Here's a breakdown:

1. Imports: It starts by importing necessary classes from created packages


(Product, Customer, Order) and standard Java libraries (ArrayList, List).
2. Products: Three Product objects are created with unique IDs, names,
and prices. Each object represents an item available for purchase.
3. Customer: A Customer object is created with an ID and name.
4. Adding to Cart: The addProductToCart method of the customer object is
used to add each product to the customer's shopping cart.
5. Displaying Cart: The customer's name and the contents of their
shopping cart are printed.
6. Calculating Cost: The calculateTotalCost method calculates the total
cost of all items in the cart and displays it.
7. Creating Order: A new List is created to hold the products in the cart,
and an Order object is created with the customer, order ID, and product
list.
8. Order Summary: Finally, the generateOrderSummary method of the
order object displays a summary of the order.

Conclusion:
This code demonstrates the basic functionalities of an e-commerce shopping
cart system. It allows creating products, adding them to a cart, calculating total
cost, and creating an order.

The code utilizes concepts from the following resources:


• Flanagan, D. (2020). Java: The complete reference (12th ed.) (Flanagan,
2020). Pearson Education. This comprehensive reference book provides
a detailed introduction to Java programming concepts and syntax.
• Sun Microsystems, Inc. (2004). The Java™ Tutorials: Packages.
https://docs.oracle.com/javase%2Ftutorial%2F/java/package/packages.h
tml (Sun Microsystems, Inc., 2004). This online tutorial from the
creators of Java itself offers a clear explanation of packages and their
role in code organization.

References
1. Flanagan, D. (2020). The complete reference (12 ed.). Flanagan, D.
2. Sun Microsystems, Inc. (2004). The Java™ Tutorials: Packages.
Retrieved from
https://docs.oracle.com/javase%2Ftutorial%2F/java/package/packages.h
tml

Code:

import com.ecommerce.Product;
import com.ecommerce.Customer;
import com.ecommerce.orders.Order;
import java.util.ArrayList;
import java.util.List;

public class Main {


public static void main(String[] args) {
// Create products
// Product 1: Car. Used car, but in good condition. Terios 2015. Toyota
Dealer, Cost USD 12,000.00
Product product1 = new Product(1, "Car", 12000.00);
// Product 2: Motorcycle. Brand new. Honda 2024, Cost USD 2500.00
Product product2 = new Product(2, "Motorcycle", 2500.00);

// Product 3: Electronics. Cellphone, Laptop, USB driver, and Smart TV.


Cost USD 5000.00
Product product3 = new Product(3, "Electronics", 5000.00);

// Create customer
Customer customer = new Customer(1, "Jose Francisco Pena Diaz");

// Add products to shopping cart


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

// Display shopping cart


System.out.println("Customer: " + customer.getName());
System.out.println("Shopping Cart: " + customer.getShoppingCart());

// Calculate total cost


double totalCost = customer.calculateTotalCost();
System.out.println("Total Cost: $" + totalCost);

// Create order
List<Product> products = new
ArrayList<>(customer.getShoppingCart());
Order order = new Order(1, customer, products);

// Display order summary


System.out.println(order.generateOrderSummary());
}
}

You might also like