⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycartbo.java

📁 这是一个购物网站可以实现购物的功能还有后台管理
💻 JAVA
字号:
package com.my.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class MyCartBO 
{   private ResultSet rs=null;
private Connection ct=null;
private PreparedStatement ps=null;
HashMap<String, String> hm=new HashMap<String, String>();	
  private float allPrice=0.0f;

public float getAllPrice()
  {
	  return this.allPrice;
  }


public String getGoodsNumBygoodsId(String goodsId)
  {
	 return hm.get(goodsId);
  }
    //1.添加货物
	 public void addGoods(String goodsId,String goodsNum)
	 {
		 hm.put(goodsId, goodsNum);
		/* hm.put("11", "2");
		 Iterator it=hm.keySet().iterator();
		 while(it.hasNext())
		 {
			 String id=(String)it.next();
			 System.out.println(id);
		 }*/		 
	 }	 
	//2.删除货物
	 public void delGoods(String goodsId)
	 {
		 hm.remove(goodsId);
	 }
	//3.清空货物
	 public void clear()
	 {
		 hm.clear();
	 }
	//4.修改数量
	 public void upGoods(String goodsId,String newNum)
	 {
		 hm.put(goodsId, newNum);
	 }
	//5.显示购物车
	 public ArrayList showMyCart()
	 {  
		 ArrayList<GoodsBean> al=new ArrayList<GoodsBean>();
		 //Iterator it=new Iterator();
		 try
		 {  	
			 String sql="select * from goods where goodsId in ";
			 Iterator it=hm.keySet().iterator();
			 if(it.hasNext())
			 {
				 String sub="(";
			 while(it.hasNext())
			 { //System.out.println(it.hasNext());
			 String goodsId=(String)it.next();
				 if(it.hasNext())
			    {	
			    //System.out.println(goodsId);
				 sub+=goodsId+",";
			     } else
			     { 
					 sub+=goodsId+")";
				 }				
			} 
			 sql+=sub;
			 //System.out.println(sql);
			 ct=new ConnDB().getConn();
			 ps=ct.prepareStatement(sql);
			 rs=ps.executeQuery();
			 this.allPrice=0.0f;
			 while(rs.next())
			 {
				 GoodsBean gb=new GoodsBean();
				 int goodsId=rs.getInt(1);
				   gb.setGoodsId(goodsId);
				   gb.setGoodsName(rs.getString(2));
				   gb.setGoodsIntro(rs.getString(3));
				   float unit=rs.getFloat(4);
				   gb.setGoodsPrice(unit);
				   gb.setGoodsNum(rs.getInt(5));				   
				   gb.setPublisher(rs.getString(6));
				   gb.setPhoto(rs.getString(7));
				   gb.setType(rs.getString(8));
				   this.allPrice=this.allPrice+unit*Integer.parseInt(this.getGoodsNumBygoodsId(goodsId+""));
				   al.add(gb);
			 }
			}else
			{
				this.allPrice=0;
			}
		 }
		 catch(Exception e)
		 {
			 e.printStackTrace();
		 }finally{
			 this.close();
		 }
		 return al;
	 }
	 public void close() {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (ps != null) {
					ps.close();
					ps = null;
				}
				if (ct != null) {
					ct.close();
					ct = null;
				}
			} catch (SQLException e) {

				e.printStackTrace();
			}
		} 
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -