天天看點

Servlet+JSP實作簡易購物車功能項目介紹遇到的問題主要頁面展示主要頁面代碼展示臨别贈言

目錄

  • 項目介紹
  • 遇到的問題
  • 主要頁面展示
  • 主要頁面代碼展示
    • 1.Show.jsp
    • 2.Cart.jsp
  • 臨别贈言

項目介紹

同上一篇參考着JSP程式設計教程(第二版)教材完成了簡易購物車的功能實作,在此對期間遇到的問題及部分頁面展示如下

遇到的問題

期間在購物車展示頁面計算顯示單個商品的金額時候遇到了浮點數相乘結果保留小數位數的問題,理論上3.0*2.8結果應該為8.4,但未進行小數位數保留時候在頁面計算顯示出來的結果卻為8.39999999999,猜測其原因可能是因為計算機對資料的處理與我們數學計算的方式不太一樣導緻。處理前代碼如下:

Double money=num*1.0*price;    // 2.8*3.0結果得到8.399999999999
sum+=money;//計算應付金額
           

特對此進行了保留兩位小數的處理,采用的保留兩位小數方式如下:

String money=new DecimalFormat("#.00").format(num*1.0*price);//保留兩位小數
sum+=Double.parseDouble(money);//計算應付金額
           

這樣,程式按照預計的結果輸出了2.8*3.0的結果為8.40。

主要頁面展示

Servlet+JSP實作簡易購物車功能項目介紹遇到的問題主要頁面展示主要頁面代碼展示臨别贈言
Servlet+JSP實作簡易購物車功能項目介紹遇到的問題主要頁面展示主要頁面代碼展示臨别贈言

主要頁面代碼展示

1.Show.jsp

<%@page import="Class.Good"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>商城</title>
</head>
<body>
<%  ArrayList goods=(ArrayList)application.getAttribute("goods");
%>
<table border="1" width="450px" rules="none" cellspacing="0" cellpadding="0">
	<tr height="50px"><td colspan="3" align="center">提供商品如下</td></tr>
	<tr align="center" height="20px" bgcolor="lightgrey">
		<td>名稱</td>
		<td>價格(元/斤)</td>
		<td>購買</td>
	</tr>
	<% if(goods==null||goods.size()==0){ %>
	<tr height="100px"><td colspan="3" align="center">沒有商品可顯示!</td></tr>
	<% } 
	else{
		for(int i=0;i<goods.size();i++){
			Good good=(Good)goods.get(i);
	%>
	<tr height="50px" align="center"> 
		<td><%=good.getName() %></td>
		<td><%=good.getPrice() %></td>
		<td><a href="DoCart?action=buy&id=<%=i%>">購買</a></td>
	</tr>
	<%
		}
	}
	%>
	<tr height="50px">
		<td align="center" colspan="3"><a href="Cart.jsp">檢視購物車</a></td>
	</tr>
</table>
</body>
</html>
           

2.Cart.jsp

<%@page import="java.text.DecimalFormat"%>
<%@page import="Class.Good"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>我的購物車</title>
</head>
<body>
<%
	ArrayList buylist=(ArrayList)session.getAttribute("buylist");//擷取存儲在session中的使用者已買商品集合對象
	float sum=0;//存儲應付金額
%>
<table border="1" width="450px" rules="none" cellspacing="0" cellpadding="0">
	<tr height="50px"><td align="center" colspan="5">購買的商品如下</td></tr>
	<tr height="30px" bgcolor="lightgrey">
		<td width="25%" align="center">名稱</td>
		<td>價格(元/斤)</td>
		<td>數量</td>
		<td>總價(元)</td>
		<td>移除(-1/次)</td>
	</tr>
	<% if(buylist==null||buylist.size()==0){ %>
		<tr height="100px"><td align="center" colspan="5">購物車為空!</td></tr>
	<% }
	else{
		for(int i=0;i<buylist.size();i++){
			Good good=(Good)buylist.get(i);
			String name=good.getName();//擷取商品名稱
			Double price=good.getPrice();//擷取商品價格
			int num=good.getNum();//擷取購買數量
			String money=new DecimalFormat("#.00").format(num*1.0*price);//保留兩位小數
			sum+=Double.parseDouble(money);//計算應付金額
			//Double money=num*1.0*price;    // 2.8*3.0結果得到8.399999999999
			//sum+=money;//計算應付金額
	%>
	<tr align="center" height="50px">
		<td><%=name %></td>
		<td><%=price %></td>
		<td><%=num %></td>
		<td><%=money %></td>
		<td><a href="DoCart?action=remove&name=<%=good.getName() %>">移除</a></td>
	</tr>
	<% 
		}
	}
	%>
	<tr align="center" height="50px"><td colspan="5">應付金額:<%=sum %></td></tr>
	<tr align="center" height="50px">
		<td colspan="2"><a href="Show.jsp">繼續購買</a></td>
		<td colspan="3"><a href="DoCart?action=clear">清空購物車</a></td>
	</tr>
</table>
</body>
</html>
           

臨别贈言

人生天地間,忽如遠行客;

且行且珍惜!!!