【JSP简单实现购物车(书本案例代码)】

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
	<body>
		<h2>欢迎来到购物网站</h2>
			请输入你的信息:<br/>
		<form action="buy.jsp" method="post">
			<input type="text" name="username" value="Guest"/><br/>
			<input type="radio" name="vip" value="y" checked>vip会员<br/>
			<input type="radio" name="vip" value="n"/>普通会员<br/>
			<input type="submit"  value="开始购物"/><br/>
		</form>
	</body>
</html>

buy.jsp`

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="mybean.Buy" %>
<html>
	<body>
	<h2>购物--商品选择页面</h2>
	<jsp:useBean id="buy" class="mybean.Buy" scope="session"/>
		<jsp:setProperty name="buy" property="*" />
		欢迎<%=buy.getUsername() %>:<%=buy.PrintVip() %>来到百货商店!<br/>
		选择要购买的商品:<br/>
	<form action="pay.jsp" method="post" >
	<%-- 实用三个文本框代替checkbox --%>
	<input type="text" name="Clothes" style="width:40px;"/>衣服 120元
	<input type="text" name="Pants" style="width:40px;"/>裤子  100元
	<input type="text" name="Shoes" style="width:40px;"/>鞋  80元
	<input type="submit" value="提交">
	<br/>
	</form>
	</body>

</html>

pay.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <body>
  	<h2>购物--结账页面</h2>
  	<jsp:useBean id="buy" class="mybean.Buy" scope="session"/>
  		<jsp:setProperty name="buy" property="*" />
  		<%
  		String Clothes;
  		Clothes=request.getParameter("Clothes");
  		int C =Integer.parseInt(Clothes);
  		buy.setClothes(C);       //衣服数量
  		Clothes=request.getParameter("Pants");
  		int P = Integer.parseInt(Clothes);
  		buy.setClothes(P);   //裤子数量
  		Clothes=request.getParameter("Shoes");
  		int S = Integer.parseInt(Clothes);
  		buy.setShoes(S);  //鞋数量
  		 %>
  		 <%=buy.PrintVip() %>的姓名是:<%=buy.getUsername() %>
  		 需要购买的商品是:<%=buy.Cart() %><br/>
  		 应付款是:<%=buy.Sum() %>元,您<%=buy.Notice() %>,实付款是:<%=buy.Discourn() %>元<br/>
  		 欢迎您下次光临!

  </body>
</html>

mybean 包 Buy.java

package mybean;

public class Buy {
			private String username;
			private String vip;
			private int Clothes=0;
			private int Pants=0;
			private int Shoes=0;
			private String notice;
			private int sum;
			public Buy() {
			}
			public String getUsername() {
				return username;
			}
			public void setUsername(String username) {
				this.username = username;
			}
			public String getVip() {
				return vip;
			}
			public void setVip(String vip) {
				this.vip = vip;
			}
			public int getClothes() {
				return Clothes;
			}
			public void setClothes(int clothes) {
				Clothes = clothes;
			}
			public int getPants() {
				return Pants;
			}
			public void setPants(int pants) {
				Pants = pants;
			}
			public int getShoes() {
				return Shoes;
			}
			public void setShoes(int shoes) {
				Shoes = shoes;
			}
			public String getNotice() {
				return notice;
			}
			public void setNotice(String notice) {
				this.notice = notice;
			}
			public int getSum() {
				return sum;
			}
			public void setSum(int sum) {
				this.sum = sum;
			}
		
			public String PrintVip(){
				if(vip.equals("y"))return "VIP";
				else return "普通会员";
			}
			public String Cart(){
				notice="";
				notice+=getClothes()+"件衣服";
				notice+=getPants()+"件裤子";
				notice+=getShoes()+"双鞋";
				
				return notice;
			}
			public int Sum(){
				sum=0;
				sum+=getClothes()*120;
				sum+=getPants()*100;
				sum+=getShoes()*80;
				return sum;
			}
			public String Notice(){
				if(vip.equals("y")) return "享有八折优惠";
				else return "不享有优惠";
			}
			public double Discourn(){
				if(vip.equals("y")) return sum*(0.8);
				else return sum;
			}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值