<% 引入JavaBean %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<% 创建商品类 %>
<%!
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
%>
<% 创建商品列表 %>
<%
List
products.add(new Product("手机", 999.99));
products.add(new Product("电脑", 1999.99));
products.add(new Product("平板", 599.99));
%>
<% 显示商品列表 %>
<% for (Product product : products) { %>
<% } %>
<% 创建购物车 %>
<%
List
double total = 0;
// 添加商品到购物车
String[] selectedProducts = request.getParameterValues("product");
if (selectedProducts != null) {
for (String productName : selectedProducts) {
for (Product product : products) {
if (product.getName().equals(productName)) {
cart.add(product);
total = product.getPrice();
break;
}
}
}
}
%>
<% 显示购物车 %>
<% for (Product product : cart) { %>
<% } %>
总计:¥<%= total %>
<% 提交订单 %>