简单购物车和登录界面的java程序
更新时间:2023-09-29 05:34:01 阅读量: 综合文库 文档下载
- java登录界面代码简单推荐度:
- 相关推荐
CartBean.java
GoodsBean.java package bean;
package bean; import java.util.ArrayList;
public class GoodsBean { import java.util.Iterator;
String id; public class CartBean {
String name; ArrayList items;
int price; public CartBean(){
int count; this.items=new ArrayList();
public GoodsBean(){} }
public GoodsBean(String id,String name,int public void addItem(GoodsBean item){
price,int count){ items.add(item);
this.id=id; }
...... public ArrayList getItems(){
} return this.items;
public String getId() { }
return id;} public int getTotalPrice(){
public void setId(String id) { int total=0;
this.id = id;} Iterator it=items.iterator();
...... while(it.hasNext()){
} GoodsBean item=(GoodsBean)it.next();
total=total+item.getPrice()*item.count;
} return total; } }
CartServlet.java package servlet;
import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*;
public class CartServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter(\ String name=request.getParameter(\ int price=Integer.parseInt(request.getParameter(\ int count=Integer.parseInt(request.getParameter(\ bean.GoodsBean item=new bean.GoodsBean(id,name,price,count); bean.CartBean cb=(bean.CartBean)request.getSession().getAttribute(\ if(cb==null) cb=new bean.CartBean(); cb.addItem(item); request.getSession().setAttribute(\ response.sendRedirect(\ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
index.jsp
<%@ page contentType=\ %>
......商品名称、商品单价、购买数量、购买操作
CartBean cb=(CartBean)session.getAttribute(\ ......002.003.004
if(cb!=null){
ArrayList items=cb.getItems();
for(Iterator it=items.iterator();it.hasNext();){
GoodsBean item=(GoodsBean)it.next();
out.println(\商品序号\
out.println(\商品名称\
out.println(\商品价格\
out.println(\商品数量\
out.println(\
}
out.println(\商品总价\
}else{
out.println(\购物车没有商品\
}
%>