怎么计算小计总价,
/*购物车类*/
public class MyCart extends HashMap{
private String myCartName;
public MyCart(){
this("myCart");
}
public MyCart(String name){
this.myCartName = name;
}
public String getName(){
return this.myCartName;
}
}
对购物车的操作:
//这是购物车的操作类,为了保证一个客户端只能new一个购物车,MyCartBO只能new一次
public class MyCartBO {
private MyCart myCart = new MyCart("我的购物车");
private Connection ct;
private Statement sm;
private ResultSet rs;
//根据商品id号删除记录
public void delGoodsById(int goodsId){
this.myCart.remove(goodsId);
}
//跟据商品id返回数量
public int getGoodsNumById(int goodsId){
int numbers = (int)this.myCart.get(goodsId);
return numbers;
}
//添加商品
public void addGoods(int goodsId){
if(this.myCart.get(goodsId) == null){
myCart.put(goodsId, 1);
}else{
this.upGoodsNum(goodsId, (int)myCart.get(goodsId) + 1);
}
}
//修改商品数量
public void upGoodsNum(int goodsId, int newNum){
myCart.put(goodsId, newNum);
}
//删除所有的商品
public void delAllGoods(){
myCart.clear();
}
//返回购物车的商品信息
public ArrayList getGoodsOrder(){
ArrayList al = new ArrayList();
try{
if(this.myCart.size() == 0){
return al;
}
String sql = "select * from goods where goodsId in(";
//从购物车中获得所有的GoodsId号
Iterator it = myCart.keySet().iterator();
while(it.hasNext()){
int key = (int)it.next();
if(it.hasNext()){
sql += key;
sql += ",";
}else{
sql += key;
sql += ")";
}
}
ct = new ConnDB().getConn();
sm = ct.createStatement();
rs = sm.executeQuery(sql);
while(rs.next()){
GoodsBean gb = new GoodsBean();
gb.setGoodsId(rs.getInt(1));
gb.setGoodsName(rs.getString(2));
gb.setGoodsIntro(rs.getString(3));
gb.setGoodsPrice(rs.getFloat(4));
gb.setGoodsNum(rs.getInt(5));
gb.setPublicsher(rs.getString(6));
gb.setPhoto(rs.getString(7));
gb.setType(rs.getString(8));
al.add(gb);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
this.close();
}
return al;
}
/*
//返回购物车的商品信息,这个算法废弃,原因是查询数据库次数太多,需要优化
//缺点之处在于:每得到个goodsId就取数据库查询一次,然后封装到GoodsBean
public ArrayList getGoodsOrder(){
ArrayList al = new ArrayList();
try{
//遍历HashMap中的所有key,获取购物车中的所有key
Iterator it = this.myCart.keySet().iterator();
while(it.hasNext()){
int key = (int)it.next();
//获得一个key查询一次
String sql = "select * from goods where goodsId=" + key + "";
ct = new ConnDB().getConn();
sm = ct.createStatement();
rs = sm.executeQuery(sql);
if(rs.next()){
GoodsBean gb = new GoodsBean();
gb.setGoodsId(rs.getInt(1));
gb.setGoodsName(rs.getString(2));
gb.setGoodsIntro(rs.getString(3));
gb.setGoodsPrice(rs.getFloat(4));
gb.setGoodsNum(rs.getInt(5));
gb.setPublicsher(rs.getString(6));
gb.setPhoto(rs.getString(7));
gb.setType(rs.getString(8));
al.add(gb);
}
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
this.close();
}
return al;
}
*/
//关闭数据库资源
private void close(){
try{
if(rs != null){
rs.close();
}
if(sm != null){
sm.close();
sm = null;
}
if(ct != null){
ct.close();
ct = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public float getTotalPrice(){
float totalPrice = 0.0f;
//从购物车中获取所有的GoodsBean
ArrayList al = this.getGoodsOrder();
for(int i = 0; i < al.size(); i++){
GoodsBean gb = (GoodsBean)al.get(i);
int goodsId = gb.getGoodsId();
//totalPrice = 商品个数 x 商品价格
int num =(int)this.myCart.get(goodsId);
float goodsPrice = gb.getGoodsPrice();
totalPrice += num*goodsPrice;
}
//对总价四舍五入,留小数点两位
totalPrice = ((int)(totalPrice*100)) / 100.0f;
return totalPrice;
}
}
计算购物车总价格是在前台,可以通过js进行计算的。
参考
http://www.cnblogs.com/come-on/archive/2016/02/04/5182409.html