简单的购物车
用到的相干知识
(1)Java基础集合 ArrayList HashMap LinkedHashmap(有序)
(2)session技术
(3)servlet
(4)单态
(5)如何选择不同的集合
List集合都是有序的,map是无序的
list和map集合都可以存放null
list可以存放相同的对象,map也可以存放相同的对象,但是key不能重复
定义一个Book类:
package com.hsp; public class Book implements java.io.Serializable{ private String id; private String name; private String price; private int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } 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 String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
Db相当于是个数据库
package com.hsp; import java.util.ArrayList; public class Db { private static ArrayList al=null; private Db(){ } static{ al=new ArrayList<Book>(); Book book1=new Book(); book1.setId("1"); book1.setName("JAVA"); book1.setPrice("50"); Book book2=new Book(); book2.setId("2"); book2.setName("oracle"); book2.setPrice("60"); Book book3=new Book(); book3.setId("3"); book3.setName("linux"); book1.setPrice("66"); al.add(book1); al.add(book2); al.add(book3); } public static ArrayList getDb(){ return al; } public static Book getBookById(String id){ Book book=null; boolean b=false; for(int i=0;i<al.size();i++){ book=(Book)al.get(i); if(book.getId().equals(id)){ b=true; break; } } if(b){ return book; }else{ return null; } } }
页面显示ShowBook
//先死后活 out.println("<h1>欢迎购买</h1>"); out.println("oracle书<a href='/myCart/BuyBookCl?id=3&name=oracle' >点击购买</a></br>"); //取出Db ArrayList<Book> mydb=Db.getDb(); for(Book book:mydb){ out.println(book.getName()+"----"+book.getPrice()+"<a href='/myCart/BuyBookCl?id="+book.getId()+"' >点击购买</a></br>"); }
提交处理servlet(BuyBookCl)
response.setContentType("text/html;charset=utf-8"); @SuppressWarnings("unused") PrintWriter out = response.getWriter(); //request.getAttribute(name)接受的servlet与本身(request里面放置的属性) //浏览器发过来的(http协议相关的) String id=request.getParameter("id"); Book bookinfo=Db.getBookById(id); HttpSession session=request.getSession(); HashMap<String,Book> hm=(HashMap<String,Book>)session.getAttribute("myBooks"); //如果第一次购物al=null if(hm==null){ hm=new LinkedHashMap<String,Book>(); Book book=new Book(); book.setId(id); book.setName(bookinfo.getName()); book.setNum(1); hm.put(id,book); //al放到session中 session.setAttribute("myBooks", hm); }else{ //判断hm中是否有该书 if(hm.containsKey(id)){ //表示购买过一次 //取出 Book book=hm.get(id); int bookNum=book.getNum(); book.setNum(bookNum+1); }else{ Book book=new Book(); book.setId(id); book.setName(bookinfo.getName()); book.setNum(1); hm.put(id,book); } //更新 session.setAttribute("myBooks", hm); } //跳转到购物车 request.getRequestDispatcher("/ShowMyCart").forward(request, response);
购物车结果页面servlet(ShowMyCart)
response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); HashMap<String,Book> myBooks=(HashMap<String,Book>)request.getSession().getAttribute("myBooks"); out.println("你的购物车有以下书籍:</br>"); //out.println(myBooks); //遍历hashMap Iterator iterator=myBooks.keySet().iterator(); while(iterator.hasNext()){ String key=(String)iterator.next(); Book book=myBooks.get(key); out.println(book.getName()+"----"+book.getNum()+"</br>"); } out.println("购物车的总价是"); out.println("<a href='/myCart/ShowBook'>返回购物大厅</a>");