GoodsSingle类
package com.zn.valuebean;
public class GoodsSingle {
private String name;
private float price;
private int num;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
MyTools类
package com.zn.toolbean;
import java.io.UnsupportedEncodingException;
public class MyTools {
public static String toChinese(String str){
if(str==null)
str="";
try{
str=new String(str.getBytes("ISO-8859-1"),"gb2312");
}catch (UnsupportedEncodingException e){
str="";
e.printStackTrace();
}
return str;
}
public static int strToint(String str){
if(str==null||str.equals(""))
str="0";
int i = 0;
try{
i = Integer.parseInt(str);
}catch(NumberFormatException e){
i = 0;
e.printStackTrace();
}
return i;
}
}
ShopCar类
package com.zn.toolbean;
import java.util.ArrayList;
import com.zn.valuebean.*;
public class ShopCar {
private ArrayList buylist=new ArrayList();
public ArrayList getBuylist(){
return buylist;
}
/**
*
* @Description 添加商品
* @author zhunan
* @date 2021年6月17日下午5:08:47
* @param single
*/
public void addItem(GoodsSingle single){
if(single!=null){
if(buylist.size()==0){
GoodsSingle temp=new GoodsSingle();
temp.setName(single.getName());
temp.setPrice(single.getPrice());
temp.setNum(single.getNum());
buylist.add(temp);
}else{
int i = 0;
for(;i < buylist.size();i++)
{
GoodsSingle temp=(GoodsSingle)buylist.get(i);
if(temp.getName().equals(single.getName()))
{temp.setNum(temp.getNum() + 1);
break;}
}
if(i >= buylist.size()){
GoodsSingle temp=new GoodsSingle();
temp.setName(single.getName());
temp.setPrice(single.getPrice());
temp.setNum(single.getNum());
buylist.add(temp);
}
}
}
}
/**
*
* @Description 清除购物车
* @author zhunan
* @date 2021年6月17日下午5:08:15
*/
public void clearCar(){
buylist.clear();
}
/**
*
* @Description 移除
* @author zhunan
* @date 2021年6月17日下午5:08:01
* @param name
*/
public void removeItem(String name){
for(int i = 0;i < buylist.size();i++){
GoodsSingle temp=(GoodsSingle)buylist.get(i);
if(temp.getName().equals(MyTools.toChinese(name))){
if(temp.getNum()>1){
temp.setNum(temp.getNum()-1);
break;
}else if(temp.getNum()==1)
{
buylist.remove(i);
}
}
}
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.zn.valuebean.GoodsSingle" %>
<%!
static ArrayList goodslist=new ArrayList();
static{
String[] names={"苹果","香蕉","梨","orange"};
float[] prices={2.8f,3.1f,2.5f,2.3f};
for(int i = 0;i < 4;i++){
GoodsSingle single = new GoodsSingle();
single.setName(names[i]);
single.setPrice(prices[i]);
single.setNum(1);
goodslist.add(i,single);
}
} %>
<%
session.setAttribute("goodslist",goodslist);
response.sendRedirect("show.jsp");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
docar.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.zn.valuebean.GoodsSingle" %>
<%@ page import="com.zn.toolbean.MyTools" %>
<jsp:useBean id="myCar" class="com.zn.toolbean.ShopCar" scope="session"></jsp:useBean>
<% String action=request.getParameter("action");
if(action==null)
action="";
if(action.equals("buy")){
ArrayList goodslist=(ArrayList)session.getAttribute("goodslist");
int id=MyTools.strToint(request.getParameter("id"));
GoodsSingle single=(GoodsSingle)goodslist.get(id);
myCar.addItem(single);
response.sendRedirect("show.jsp");
}
else if(action.equals("remove")){
String name=request.getParameter("name");
myCar.removeItem(name);
response.sendRedirect("shopcar.jsp");
} else if(action.equals("clear")){
myCar.clearCar();
response.sendRedirect("shopcar.jsp");
}else{
response.sendRedirect("show.jsp");
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
shopcar.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.zn.valuebean.GoodsSingle" %>
<jsp:useBean id="myCar" class="com.zn.toolbean.ShopCar" scope="session"></jsp:useBean>
<%
ArrayList buylist=myCar.getBuylist();
float total = 0;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" width="450" rules="none" cellspacing="0" cellpadding="0">
<tr height="50"><td colspan="5" align="center">购买商品如下</td></tr>
<tr align="center" height="30" bgcolor="lightgrey">
<td width="25%">名称</td>
<td>价格(元/斤)</td>
<td>数量</td>
<td>总价(元)</td>
<td>移除(-1/次)</td>
</tr>
<% if(buylist==null||buylist.size()==0){ %>
<tr height="100"><td colspan="5" align="center">您的购物车为空!</td></tr>
<%
}
else{
for(int i = 0;i < buylist.size();i++){
GoodsSingle single=(GoodsSingle)buylist.get(i);
String name = single.getName();
float price = single.getPrice();
int num = single.getNum();
float money = ((int) ((price*num+0.05f)*10))/10f;
total+=money;
%>
<tr align="center" height="50">
<td><%=name%></td>
<td><%=price%></td>
<td><%=num%></td>
<td><%=money%></td>
<td>
<a href="docar.jsp?action=remove&name=<%=single.getName()%>">移除</a>
</td>
</tr>
<%
}
}%>
<tr height="50" align="center"><td colspan="5">应付金额:<%=total%></td></tr>
<tr height="50" align="center">
<td colspan="2"><a href="show.jsp">继续购物</a></td>
<td colspan="3"><a href="docar.jsp?action=clear">清空购物车</a></td>
</tr>
</table>
</body>
</html>
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.zn.valuebean.GoodsSingle" %>
<% ArrayList goodslist=(ArrayList)session.getAttribute("goodslist"); %>
<table border="1" width="450" rules="none" cellspacing="0" cellpadding="0">
<tr height="50"><td colspan="3" align="center">提供商品如下</td></tr>
<tr align="center" height="30" bgcolor="lightgrey">
<td>名称</td>
<td>价格(元/斤)</td>
<td>购买</td>
</tr>
<% if(goodslist==null||goodslist.size()==0){ %>
<tr height="100"><td colspan="3" align="center">没有商品可显示!</td></tr>
<%
}
else{
for(int i = 0;i < goodslist.size();i++){
GoodsSingle single=(GoodsSingle)goodslist.get(i);
%>
<tr height="50" align="center">
<td><%=single.getName() %></td>
<td><%=single.getPrice() %></td>
<td><a href="docar.jsp?action=buy&id=<%=i %>">购买</a></td>
</tr>
<%
}
}%>
<tr height="50">
<td align="center" colspan="3"><a href="shopcar.jsp">查看购物车</a></td>
</tr>
</table>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>