• 这里的购物车暂时存放书,后期把参数改成Object,把方法抽取成接口,只要实现了接口的Object类都可以放进购物项,这样就实现了购物任何物品
  • 使用购物项因为一个购物项可以包含某种商品的数量,总价等,反之则需要把商品重复存放到购物车,没有用户体验
  • 购物车用HashMap,键存放书id,值存放购物项

1. 设计bean

public class Book implements Serializable{

    //因为对象传输需要实现序列化接口
//后面代码中id作为Map的键,而键只能为String
String id;
String name;
double price; public Book(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
} public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
} @Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}

购物项

public class CartItem implements Serializable{

	private Book book;
private int quantity;
private double price; public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return book.getPrice() * quantity;
}
public void setPrice(double price) {
this.price = price;
} @Override
public String toString() {
return "CartItem [book=" + book + ", quantity=" + quantity + ", price=" + price + "]";
}
}

购物车

public class Cart<K, V> implements Serializable{

    //键为书名id,储存实物
private double totalPrice;
private HashMap<String,CartItem> bookMap = new HashMap<String, CartItem>(); public void addBook(Book book){
//从购物车找对应书籍的购物项
CartItem cartItem = bookMap.get(book.getId());
//若没有该书的购物项,新建一个
if(cartItem == null){
cartItem = new CartItem();
cartItem.setBook(book);
cartItem.setQuantity(1);
bookMap.put(book.getId(), cartItem);
}else{
cartItem.setQuantity(cartItem.getQuantity() + 1);
}
}
public void deleteBook(Book book){
CartItem cartItem = bookMap.get(book.getId());
if(cartItem == null){
//do nothing
}else if(cartItem.getQuantity() == 1){
bookMap.remove(book.getId());
}else{
cartItem.setQuantity(cartItem.getQuantity() - 1);
}
}
public double getPrice(){
//遍历购物车里的购物项
for(Map.Entry set : bookMap.entrySet()){
//String bookId = (String) set.getKey();
CartItem cartItem = (CartItem) set.getValue();
totalPrice += cartItem.getPrice();
}
return totalPrice;
} public HashMap<String, CartItem> getBookMap() {
return bookMap;
}
public void setBookMap(HashMap<String, CartItem> bookMap) {
this.bookMap = bookMap;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
}

2. 购物车序列化存放到Cookie

2.1 模仿购物车添加商品

//往购物车添加书本
Cart cart = new Cart();
cart.addBook(new Book("1","且听风吟",10.5f));
cart.addBook(new Book("1","且听风吟",10.5f));
cart.addBook(new Book("1","且听风吟",10.5f));
cart.addBook(new Book("2","我们仨",5.5f));
cart.deleteBook(new Book("1","且听风吟",10.5f));
cart.deleteBook(new Book("2","我们仨",5.5f));
cart.deleteBook(new Book("3","解忧杂货店",20.5f));

#### 2.2 购车从序列化存入Cookie

  • 其中Cookie不能有[ ] ( ) = , " / ? @ : ;特殊字符,需要URL编码
  • ByteArrayOutputStream.toString()把字节数组内容转化成字符串
//	-----------------------------购物车对象序列化------------------------[开始]
ByteArrayOutputStream bos= new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(cart);
String objectString = URLEncoder.encode(bos.toString("ISO-8859-1"),"UTF-8");
// -----------------------------购物车对象序列化------------------------[完] // -----------------------------给客户端添加cookie------------------------[开始]
response.setContentType("text/html;charset=UTF-8");
Cookie cookie = new Cookie("name", objectString);
cookie.setMaxAge(1000);
response.addCookie(cookie);
// -----------------------------给客户端添加cookie------------------------[完]

3. 服务器读取Cookie

  • 遍历所有Cookie,找到Cart
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookieLoop : cookies){
String name = cookieLoop.getName();
String value = URLDecoder.decode(cookieLoop.getValue(), "UTF-8");
if(name == "Cart"){
ByteArrayInputStream bis = new ByteArrayInputStream(value.getBytes("ISO-8859-1"));
ObjectInputStream ois = new ObjectInputStream(bis);
try {
Cart cart1 = (Cart) ois.readObject();
HashMap cartMap = cart1.getBookMap();
for(Object cartItem : cartMap.values()){
//遍历购物项并打印
System.out.println(cartItem.toString());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}

4. 测试结果

CartItem [book=Book [id=1, name=且听风吟, price=10.5], quantity=2, price=0.0]
<!-- 剩下且听风吟 * 2 -->

最新文章

  1. PHP从mysqli中获取的资源$result是不是不能while($row = $result-&gt;fetch_assoc())这样两次?【坑】
  2. C语言常量与指针
  3. Javascript基础系列之(五)条件语句(比较操作符)
  4. android实现 服务器功能
  5. mysql 数据库还原出错ERROR:Unknown command &#39;\&#39; mysql中断
  6. Mutex — Windows API
  7. 条款21: 必须返回对象时,不要强行返回对象的reference
  8. 初识JAVA,对servlet的理解
  9. echarts_部分图表配置_堆叠折线图
  10. Vue生命周期-手动挂载理解
  11. php(curl请求)测试接口案例
  12. windows配置tomcat https访问
  13. java 值传递 数组传递
  14. Java中A instanceof B是什么意思?
  15. docker简易命令
  16. 【Ansible】 各种模块
  17. 利用grep命令查找字符串分析log文件的一次实践
  18. Ping程序的实现
  19. 【Dagger2】简介 配置 使用 MVP案例
  20. SVN无法读取cruuent修复方法

热门文章

  1. jmeter(二十七)分布式压测注意事项
  2. C#完成 使用异步线程定时更新窗体标签内容,并对标签内容进行求和显示
  3. [個人紀錄] git 設定
  4. selenium自学笔记---ecshop购买脚本 xpath定位元素(下拉框,单选框)
  5. python基础2--if,while,for,逻辑运算
  6. kafka安装和简单测试
  7. CDH5.14.4中的Hue集成HBase
  8. 很带劲,Android9.0可以在i.MX8开发板上这样跑
  9. JS面向对象设计-理解对象
  10. Spring Boot加载application.properties配置文件顺序规则