运用java语言设计购物车的实现
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
首先,建3个实体类,如下:
package entity;
//订单类
import java.util.Date;
public class Order {
private String orderId;//订单编号
private OrderItem[] items;//订单项
private Date date;//订单时间
private Float total;//订单总额
public Float getTotal() {
return total;
}
public void setTotal(Float total) {
this.total = total;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) { this.orderId = orderId;
}
public OrderItem[] getItems() {
return items;
}
public void setItems(OrderItem[] items) { this.items = items;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
package entity;
//订单项,为了方便拓展
public class OrderItem {
private Product product;//商品
private Integer number;//购买数量
public OrderItem(Product product, Integer number) {
this.product = product;
this.number = number;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
package entity;
//产品信息
public class Product {
private String name;
private String image;
private Integer id;
private Float price;
public Product(String name, String image, Integer id, Float price) {
= name;
this.image = image;
this.id = id;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}
接下来就是处理了:
package util;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpSession;
import entity.Order;
import entity.OrderItem;
import entity.Product;
public class ShoppingCart {
public static Product[] init() {
Product[] product = new Product[3];
Product p1 = new Product("Struts Web", "struts.jpg", 1, 80.5F);
Product p2 = new Product("Hibernate开发与实践", "hibernate.jpg", 2, 60.5F);
Product p3 = new Product("Struts Spring Hibernate", "spring.jpg", 3, 90.5F);
product[0] = p1;
product[1] = p2;
product[2] = p3;
return product;
}
/**