📄 cart.java
字号:
package com.beans;
import java.util.*;
import java.sql.*;
import com.utils.DBConnection;
public class Cart implements java.io.Serializable
{
private String login=null; //用户的标识
private String pass=null; //用户密码
private String name=null; //用户姓名
private HashMap items=null; //购物车中的物品
private float totalPrice = 0; //总价(新加入)
private int orderId = 0; //订单号(新加入)
public float getTotalPrice()//(新加入)
{
return this.totalPrice;
}
public int getOrderId()//(新加入)
{
return this.orderId;
}
public Cart()
{
items=new HashMap();
}
public void saveOrder(Products products) throws Exception //生成并保存订单
{
Connection con = null;
try
{
con = DBConnection.getConnection();
}
catch(Exception e)
{
//e.printStackTrace();
throw e;
}
Set book = items.keySet(); //获取HashMap表的名即书号
Iterator it = book.iterator();
try
{
if(it.hasNext())
{ //如果有书调用存储过程生成订单获取订单号
CallableStatement cstmt=con.prepareCall("{call saveOrder(?,?)}");
cstmt.setString(1,login);
cstmt.registerOutParameter(2,java.sql.Types.INTEGER);
try
{
cstmt.execute();
orderId = cstmt.getInt(2);
}
catch(Exception e)
{
//e.printStackTrace();
throw e;
}
cstmt.close();
}
else
{
throw new Exception();
}
float price = 0;
while(it.hasNext()) //将书号和订单号等信息
{
String itemId = (String)it.next();
price = products.getItem(itemId).getPrice();
int number = ((Integer)items.get(itemId)).intValue();
this.writeDatabase(con,orderId,itemId,number,price); //保存订单明细
totalPrice += price*number; //计算总价格
}
//将总价格和时间等的信息存入订单表中
PreparedStatement stm=con.prepareStatement("update Orders set totalPrice = ?,time = ? where orderId = ?");
stm.setFloat(1,totalPrice);
long date = new java.util.Date().getTime();
stm.setTimestamp(2,new Timestamp(date));
stm.setInt(3,orderId);
try
{
stm.execute();
}
catch(Exception e)
{
//e.printStackTrace();
throw e;
}
stm.close();
con.close();
}
catch(Exception e)
{
//e.printStackTrace();
throw e;
}
}
public void writeDatabase(Connection con,int orderId,String itemId,int number,float price) throws Exception
{ //保存订单明细,由saveOrder()调用
try
{
PreparedStatement stm=con.prepareStatement("insert into OrderDetails values(?,?,?,?)");
stm.setInt(1,orderId);
stm.setString(2,itemId);
stm.setInt(3,number);
stm.setFloat(4,price);
try
{
stm.execute();
}
catch(Exception e)
{
//e.printStackTrace();
throw e;
}
stm.close();
}
catch(Exception e)
{
//e.printStackTrace();
throw e;
}
}
public void addItem(String itemId,int quantity) //(修改过的方法)
{
if(items.containsKey(itemId))
{
int number = ((Integer)items.get(itemId)).intValue(); //获取原来的数量
items.put(itemId,new Integer(number+quantity));
}
else
{
items.put(itemId,new Integer(quantity));
}
}
public void removeItem(String itemId)
{
items.remove(itemId);
}
public void updateItem(String itemId,int quantity)
{
if(items.containsKey(itemId))items.remove(itemId);
items.put(itemId,new Integer(quantity));
}
public HashMap getItems()
{
return this.items;
}
public void setLogin(String login)
{
this.login=login;
}
public String getLogin()
{
return this.login;
}
public void setPass(String pass)
{
this.pass=pass;
}
public String getPass()
{
return this.pass;
}
public String getName()
{
return this.name;
}
public void clear() //(修改过)
{
items.clear();
this.totalPrice = 0;
this.orderId = 0;
}
public void iniLogin() throws Exception //(新方法)用户验证,如果不成功抛出异常
{
Connection con = null;
Statement stm = null;
ResultSet result = null;
try
{
con = DBConnection.getConnection();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
stm=con.createStatement();
result=stm.executeQuery("select * from regInfo where login='"+login+"' and pass='"+pass+"'");
if(result.next())
{
this.name=result.getString("name");
}
else
{
throw new Exception(); //连接不成功抛出异常
}
}
catch(Exception e)
{
//e.printStackTrace();
throw e;
}
finally
{
try
{
result.close();
stm.close();
con.close();
}
catch(Exception e)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -