0% found this document useful (0 votes)
2 views3 pages

Prog 2 Learning Assign

The document contains Java classes for an e-commerce system, including Order, Customer, and Product classes. The Order class manages order details and generates summaries, while the Customer class handles shopping cart operations. The Product class defines product attributes and methods for accessing product information.

Uploaded by

robert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Prog 2 Learning Assign

The document contains Java classes for an e-commerce system, including Order, Customer, and Product classes. The Order class manages order details and generates summaries, while the Customer class handles shopping cart operations. The Product class defines product attributes and methods for accessing product information.

Uploaded by

robert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Robert Banda

Programming Assignment 2

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;
public Order(int orderID, Customer customer, List<Product> products)
{
this.orderID = orderID;
this.customer = customer;
this.products = products;
}
public int getOrderID() {
return orderID;
}
public Customer getCustomer() {
return customer;
}
public List<Product> getProducts() {
return products;
}
public double getOrderTotal() {
double total = 0;
for (Product product : products) {
total += product.getPrice();

return total;
}
public String generateOrderSummary() {
StringBuilder summary = new StringBuilder();
summary.append("Order ID: ").append(orderID).append("\n");
summary.append("Customer: ").append(customer.getName()).append("\
n");
summary.append("Products in the order:\n");
for (Product product : products) {
summary.append("- ").append(product.getName()).append("
($").append(product.getPrice()).append(")\n");
}
summary.append("Total cost: $").append(getOrderTotal());
return summary.toString();
}
}
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 void addToCart(Product product) {
shoppingCart.add(product);
}
public void removeFromCart(Product product) {
shoppingCart.remove(product);
}
public double calculateTotalCost() {
double totalCost = 0;
for (Product product : shoppingCart) {
totalCost += product.getPrice();
}
return totalCost;
}
public List<Product> getShoppingCart() {
return shoppingCart;
}
public String getName() {
return name;
}
}
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 void setProductID(int productID) {
this.productID = productID;
}
public String getName() {
return name;
}
}

You might also like