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

📄 50f7b2c91c46001c194efe7b2844a31e

📁 是一个网上手机超市
💻
字号:
package hall;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import javax.servlet.http.*;

public class opBasket {

	private HttpSession session; //页面的session;

	private Vector purchaselist; //显示商品列表向量数组

	private float all_price = 0; //购物总价钱

	private String orderId = ""; //用户订单号

	private DBWrapper myConnection = null;

	private String sqlStr = "";
	
	public opBasket() throws Exception{
		try{
			myConnection = DBWrapper.Instance();
			
		}catch(Exception e){
			System.out.println(e);
		}
		
	}

    public void setSession(HttpSession see){
    	session = see;
    	
    }
	public Vector getPurchaselist() {
		return purchaselist;
	}

	public float getAll_price(){
		return all_price;
	}
	public void setOrderId(String newId) {
		orderId = newId;
	}

	public String getOrderId() {
		return orderId;
	}

	
	/**
	 * 往购物车中添加选购的商品
	 */
	public boolean addnew(int inProItem, int inQuantitr) throws Exception {

		int prodid = inProItem;
		int quantity = inQuantitr;

		if (quantity < 0)
			return false;
		
		purchaselist = (Vector) session.getAttribute("basket");
		if(quantity==0)return true;
		if(prodid==-2)return true;
		sqlStr = "select quantity from products where producItem = " + prodid;
		try {
			ResultSet rs = myConnection.runQuery(sqlStr);
			if (rs.next()) {
				if (quantity > rs.getInt(1)) {
					return false;
				}
			}
			rs.close();
		} catch (SQLException e) {
			System.out.println(e);
			return false;
		}

		orderItem iList = new orderItem();
		iList.setProducItem(inProItem);
		iList.setQuantity(quantity);
		boolean match = false; //是否购买过该商品
		if (purchaselist == null) { //第一次购买
			purchaselist = new Vector();
			purchaselist.addElement(iList);
		}

		else { // 不是第一次购买
			for (int i = 0; i < purchaselist.size(); i++) {
				orderItem itList = (orderItem) purchaselist.elementAt(i);
				if (iList.getProItem() == itList.getProItem()) {
					itList.setQuantity(itList.getQuantity() + iList.getQuantity());
					purchaselist.setElementAt(itList, i);
					match = true;
					break;
				} 
			} 
			if (!match)
				purchaselist.addElement(iList);
		}
		session.setAttribute("basket", purchaselist);
		for(int i = 0; i < purchaselist.size(); i++){
			orderItem temp = new orderItem();
			temp = (orderItem)purchaselist.elementAt(i);
			System.out.println("***************");
			System.out.println(temp.getQuantity());
		}
		return true;
	}

	/**
	 * 修改已经放进购物车的数据
	 * @param newrequest
	 * @return
	 */
	public boolean modifBasket(int inProItem, int inQuantity) throws Exception {
		int prodid = inProItem;
		int quantity = inQuantity;

		if (quantity < 1)
			return false;
		
		purchaselist = (Vector) session.getAttribute("basket");
		if (purchaselist == null) {
			return false;
		}
		sqlStr = "select quantity from products where producItem = " + prodid;
		try {
			ResultSet rs = myConnection.runQuery(sqlStr);
			if (rs.next()) {
				if (quantity > rs.getInt(1)) {
					return false;
				}
			}
			rs.close();
		} catch (SQLException e) {
			return false;
		}

		for (int i = 0; i < purchaselist.size(); i++) {
			orderItem itList = (orderItem) purchaselist.elementAt(i);
			if (prodid == itList.getProItem()) {
				itList.setQuantity(quantity);
				purchaselist.setElementAt(itList, i);
				break;
			} 
		} 
		return true;
	}

	/**
	 *删除购物车中数据
	 * @param newrequest
	 * @return
	 */
	public boolean delShoper(int inProItem) {
		int prodid = inProItem;
		purchaselist = (Vector) session.getAttribute("basket");
		if (purchaselist == null) {
			return false;
		}

		for (int i = 0; i < purchaselist.size(); i++) {
			orderItem itList = (orderItem) purchaselist.elementAt(i);
			if (prodid == itList.getProItem()) {
				purchaselist.removeElementAt(i);
				break;
			} 
		} 
		return true;
	}

	/*
	 * 得到购物车中货物总价
	 */
	public boolean getTotalPrice(Vector inVector) throws Exception{
		try{
			float tempAmount = 0;
			for (int i = 0; i < inVector.size(); i++) {
				orderItem iList = (orderItem) purchaselist.elementAt(i);
				sqlStr = "select price from products where producItem = ";
				sqlStr = sqlStr + iList.getProItem() ;				
				ResultSet rs = myConnection.runQuery(sqlStr);
				rs.next();
				tempAmount = rs.getFloat(1);
				all_price += tempAmount * iList.getQuantity();
				
			}
			return true;
		}catch(Exception e){
			System.out.println(e);
			return false;
		}
	}
	/**
	 * 提交购物车
	 */
	public boolean payout(String inUser,String inAddress,String inCode,double total) 
	throws Exception {

		String userName = inUser; 
		purchaselist = (Vector) session.getAttribute("basket");
		if (purchaselist == null || purchaselist.size() < 0) {
			return false;
		}

		try{
			for (int i = 0; i < purchaselist.size(); i++) {
				orderItem iList = (orderItem) purchaselist.elementAt(i);
				sqlStr = "select quantity from products where producItem = "
						+ iList.getProItem();
				ResultSet rs = myConnection.runQuery(sqlStr);
				System.out.println(sqlStr);
				rs.next();
				if(rs.getInt(1) < iList.getQuantity()){
					return false;
				}
			}
		}catch(Exception e){
			System.out.println(e);
		}
		sqlStr = "update customers set account = account - "
			+ total + " where name = '" + userName +"'";
		try{	
			myConnection.runUpdate(sqlStr);

		}catch(Exception e){
			System.out.println(e);
			return false;
		}
		String address = inAddress;
		String code = inCode;
		Date tempDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); 
		while(true){
			long timeInMillis = System.currentTimeMillis();
			orderId = "" + timeInMillis;//以系统时间产生位移的订单编号
			sqlStr = "select * from orders where orderId = '" + orderId + "'";
			try{
				System.out.println(sqlStr);
				ResultSet rs1 = myConnection.runQuery(sqlStr);
				if(!rs1.next()){
					break;
				}
			}catch(Exception e){
				System.out.println(e);
				return false;
			}
			
		}
		
		sqlStr = "insert into [orders]([orderId],[customerName]," +
				"[address],[code],[orderTime],[amount],[state]) values ('";
		sqlStr = sqlStr + orderId + "','";
		sqlStr = sqlStr + userName + "','";
		sqlStr = sqlStr + address + "','";
		sqlStr = sqlStr + code + "','";
		sqlStr = sqlStr + sdf.format(tempDate) + "'," ;
		sqlStr = sqlStr + total	+ "," ;
		sqlStr = sqlStr + 1 + " )";

		try {
			System.out.println(sqlStr);
			myConnection.runUpdate(sqlStr);

			for (int i = 0; i < purchaselist.size(); i++) {
				orderItem iList = (orderItem) purchaselist.elementAt(i);
				sqlStr = "insert into orderItem (orderId,producItem,quantity) values ('";
				sqlStr = sqlStr + orderId + "',";
				sqlStr = sqlStr + iList.getProItem() + ",";
				sqlStr = sqlStr + iList.getQuantity() + ")";
				System.out.println(sqlStr);
				myConnection.runUpdate(sqlStr);
				sqlStr = "update products set quantity = quantity - "
						+ iList.getQuantity() + " where producItem = "
						+ iList.getProItem();
				System.out.println(sqlStr);
				myConnection.runUpdate(sqlStr);
			}
			return true;
		} catch (SQLException e) {
			System.out.print(e.getMessage());
			return false;
		}

	}

}

⌨️ 快捷键说明

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