Programming Assignment Unit 2
Programming Assignment Unit 2
com.ecommerce.Product.java
package com.ecommerce;
// Constructor
public Product(String productID, String name, double price) {
this.productID = productID;
this.name = name;
this.price = price;
}
com.ecommerce.Customer.java
package com.ecommerce;
import java.util.ArrayList;
import java.util.List;
public class Customer {
private String customerID;
private String name;
private List<Product> shoppingCart;
// Constructor
public Customer(String customerID, String name) {
this.customerID = customerID;
this.name = name;
this.shoppingCart = new ArrayList<>();
}
com.ecommerce.orders.Order.java
package com.ecommerce.orders;
import com.ecommerce.Customer;
import com.ecommerce.Product;
import java.util.List;
// Constructor
public Order(String orderID, Customer customer, List<Product> products) {
this.orderID = orderID;
this.customer = customer;
this.products = products;
this.orderTotal = calculateOrderTotal();
}
Main.java
import com.ecommerce.Product;
import com.ecommerce.Customer;
import com.ecommerce.orders.Order;
import java.util.ArrayList;
import java.util.List;
// Place order
customer.placeOrder();
// Create order
List<Product> productsInOrder = new ArrayList<>(customer.getShoppingCart());
Order order = new Order("O001", customer, productsInOrder);
Explanation:
In this implementation, the ‘com.ecommerce’ package contains the Product and Customer
classes, while the ‘com.ecommerce.orders’ package contains the Order class. The ‘main’
program creates instances of these classes, demonstrates adding products to the shopping cart,
placing an order, and displaying order details. The use of packages and the import statement
ensures proper organization and encapsulation.
Screenshot:
References:
Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition