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

📄 504b5973e146001c1160e657ddb92502

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

/**
 * Project:    NWPU online shop
 * JDK version used: jdk1.5.0
 * Version: 	1.01
 * class opBasket 用来处理关于购物车内的货物进行添加,修改与删除和购物车的提交
 */

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 = "";

	private String errorMsg;//出错信息

	/**

	 * 默认构造函数

	 */
	public opBasket() throws Exception {
		try {
			myConnection = DBWrapper.Instance();

		} catch (Exception e) {
			System.out.println(e);
		}

	}

	/**
	 * String getErrorMsg()
	 * Description :得到出错信息
	 * @return Vector
	 */
	public String getErrorMsg() {
		return errorMsg;
	}

	/**
	 * void setSession(HttpSession see)
	 * Description :修改页面Session
	 * @param HttpSession
	 */
	public void setSession(HttpSession see) {
		session = see;

	}

	/**
	 * Vector getPurchaselist()
	 * Description :得到存储orderItem实例的Vector
	 * @return Vector
	 */
	public Vector getPurchaselist() {
		return purchaselist;
	}

	/**
	 * float getAll_price()
	 * Description :得到购物车内货物总价
	 * @return float
	 */
	public float getAll_price() {
		return all_price;
	}

	/**
	 * void setOrderId(String newId)
	 * Description :修改订单号
	 * @return String
	 */
	public void setOrderId(String newId) {
		orderId = newId;
	}

	/**
	 * String getOrderId()
	 * Description :得到订单号
	 * @return String
	 */
	public String getOrderId() {
		return orderId;
	}

	/**
	 * boolean addnew(String inUser, int inProItem, int inQuantity)
	 * Description :往购物车中添加选购的商品
	 * @param String 选购商品的用户名
	 * @param int 货物号
	 * @param int 货物量
	 * * @return boolean 返回操作是否成功信息
	 */
	public boolean addnew(String inUser, int inProItem, int inQuantity)
			throws Exception {

		int prodid = inProItem;
		int quantity = inQuantity;
		String userName = inUser;

		if (quantity < 0)
			return false;
		if (quantity == 0)
			return true;
		if (prodid == -2)
			return true;
		// 得到货物的价格
		sqlStr = "select price from products where producItem = " + prodid;
		double proPrice = 0;
		int proQuantity = 0;
		try {
			ResultSet rs = myConnection.runQuery(sqlStr);
			if (rs.next()) {
				proPrice = rs.getDouble(1);
			}
			rs.close();
		} catch (SQLException e) {
			System.out.println(e);
			errorMsg = "访问数据库失败!";
			return false;
		}
		// 得到顾客的余额
		sqlStr = "select account from customers where name = '" + userName + "'";
		double userAccount = 0;
		try {
			ResultSet rs = myConnection.runQuery(sqlStr);
			if (rs.next()) {
				userAccount = rs.getDouble(1);
			}
			rs.close();
		} catch (SQLException e) {
			System.out.println(e);
			errorMsg = "访问数据库失败!";
			return false;
		}

		purchaselist = (Vector) session.getAttribute("basket");

		// 得到商品的剩余量
		sqlStr = "select quantity from products where producItem = " + prodid;
		try {
			ResultSet rs = myConnection.runQuery(sqlStr);
			if (rs.next()) {
				proQuantity = rs.getInt(1);
				if (quantity > proQuantity) {
					errorMsg = "货物剩余量不足";
					return false;
				}
			}
			
			rs.close();

		} catch (SQLException e) {
			System.out.println(e);
			errorMsg = "访问数据库失败!";
			return false;
		}

		orderItem iList = new orderItem();
		iList.setProducItem(inProItem);
		iList.setQuantity(quantity);
		boolean match = false; // 是否购买过该商品
		if (purchaselist == null) { // 第一次购买
			if (userAccount < proPrice * quantity) {
				errorMsg = "你的余额不足";
				return false;
			} else {
				purchaselist = new Vector();
				purchaselist.addElement(iList);
				return true;
			}
		}

		else { // 不是第一次购买
			getTotalPrice(purchaselist);
			if (userAccount < (all_price + proPrice * quantity)) {
				errorMsg = "你的余额不足";
				return false;
			} 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);
				
				for(int i =0 ;i < purchaselist.size();i++)
					System.out.println(i);

				session.setAttribute("basket", purchaselist);
				return true;
			}
		}
	}

	/**
	 * boolean modifBasket(int inProItem, int inQuantity)
	 * 修改已经放进购物车的数据
	 * @param int 货物号
	 * @param int 货物量
	 * @return boolean 返回操作是否成功信息
	 */
	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;
	}

	/**
	 * boolean delShoper(int inProItem)
	 * 删除购物车中数据
	 * 
	 * @param int 货物号
	 *   
	 * @return boolean 返回操作是否成功信息
	 */
	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;
	}

	/**
	 * boolean getTotalPrice(Vector inVector)
	 * 得到购物车中货物总价
	 * 
	 * @param Vector 
	 *            
	 * @return boolean 返回操作是否成功信息
	 */
	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;
		}
	}

	/**
	 * boolean payout(String inUser, String inAddress, String inCode,
	 *		double total)
	 * 提交购物车,产生订单,并将订单和订单项写入数据库
	 * @param String 用户名
	 * @param String 客户地址
	 * @param String 客户邮政编码
	 * @param Double 总价
	 * @return boolean 返回操作是否成功信息
	 */
	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()) {
					errorMsg = "货物剩余量不足";
					return false;
				}
			}
		} catch (Exception e) {
			System.out.println(e);
			errorMsg = "访问数据库失败!";
			return false;
		}
		sqlStr = "update customers set account = account - " + total
				+ " where name = '" + userName + "'";
		try {
			myConnection.runUpdate(sqlStr);

		} catch (Exception e) {
			System.out.println(e);
			errorMsg = "访问数据库失败!";
			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 {
				ResultSet rs1 = myConnection.runQuery(sqlStr);
				if (!rs1.next()) {
					break;
				}
			} catch (Exception e) {
				System.out.println(e);
				errorMsg = "访问数据库失败!";
				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 {
			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();
				myConnection.runUpdate(sqlStr);
			}
			return true;
		} catch (SQLException e) {
			System.out.print(e.getMessage());
			errorMsg = "访问数据库失败!";
			return false;
		}

	}

}

⌨️ 快捷键说明

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