模拟购物车表格

<div id="arae">
<table border="1" style="text-align: center">
<thead>
<th>商品编号</th>
<th>商品名称</th>
<th>商品数量</th>
<th>商品单价</th>
<th>商品总价</th>
<th>操作</th>
</thead>
<tbody id="tbody">
<tr>
<td>1</td>
<td>iphone4</td>
<td><input type="button" value=" + " class="ad"><span class="count">0</span><input type="button" value=" - " class="sb"></td>
<td class="danJia">2000</td>
<td><span class="price">0</span></td>
<td><input type="button" value="删除" class="delete"></td>
</tr>
<tr>
<td>2</td>
<td>iphone4s</td>
<td><input type="button" value=" + " class="ad"><span class="count">0</span><input type="button" value=" - " class="sb"></td>
<td>2200</td>
<td><span class="price">0</span></td>
<td><input type="button" value="删除" class="delete"></td>
</tr>
<tr>
<td>3</td>
<td>iphone5</td>
<td><input type="button" value=" + " class="ad"><span class="count">0</span><input type="button" value=" - " class="sb"></td>
<td>2400</td>
<td><span class="price">0</span></td>
<td><input type="button" value="删除" class="delete"></td>
</tr>
<tr>
<td>4</td>
<td>iphone6</td>
<td><input type="button" value=" + " class="ad"><span class="count">0</span><input type="button" value=" - " class="sb"></td>
<td>4000</td>
<td><span class="price">0</span></td>
<td><input type="button" value="删除" class="delete"></td>
</tr>
<tr>
<td>5</td>
<td>iphone7</td>
<td><input type="button" value=" + " class="ad"><span class="count">0</span><input type="button" value=" - " class="sb"></td>
<td>7000</td>
<td><span class="price">0</span></td>
<td><input type="button" value="删除" class="delete"></td>
</tr>
</tbody>
<tfoot>
<tr>
<td>商品总数</td>
<td><span class="allCount">0</span></td>
<td>商品总价</td>
<td><span class="allPrice">0</span></td>
<td><input type="button" value="清空购物车" class="allDelete"></td>
</tr>
</tfoot>
</table>
</div>


#arae{
width: 550px;
margin: 0 auto;
float: left;
}
#arae span{
color: black;
text-decoration: none;
}




$('#tbody').on('click','.delete',function(){
$(this).parent().parent().empty();
showAllCount();
showAllPrice();
});
$('.allDelete').on('click',function(){
$('#tbody').empty();
});
$('#tbody').on('click','.ad',function(){
var a = $(this).next('.count').text();
a++;
$(this).next('.count').text(a);
var b = +$(this).parent().next('td').text();
s = a*b;
$(this).parent().next('td').next('td').children().text(s);
showAllCount();
showAllPrice();
}).on('click','.sb',function(){
var a = $(this).prev('.count').text();
a--;
$(this).prev('.count').text(a);
var b = +$(this).parent().next('td').text();
s = a*b;
$(this).parent().next('td').next('td').children().text(s);
showAllCount();
showAllPrice();
});
function showAllCount(){
var allSum = 0;
$('.count').each(function(){
allSum = allSum + (+$(this).text());
$('.allCount').text(allSum);
});
}
function showAllPrice(){
var allSum = 0;
$('.price').each(function(){
allSum = allSum + (+$(this).text());
$('.allPrice').text(allSum);
});
}
 

转载于:https://www.cnblogs.com/xingxing88/p/6060170.html

import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 购物车类 */ public class Cart { //创建一个map对象,用来保存商品,key为商品,value为商品的数量 private Map<Goods, Integer> map = new HashMap<Goods, Integer>(); /** * 添加商品到购物车方法 * @param id 商品编号 * @param quantity 商品数量 */ public void addGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ Integer oQuantity = map.get(goods);//获取商品在购物车中原本的数量 if(oQuantity!=null){ oQuantity += quantity; }else{ oQuantity = quantity; } map.put(goods, oQuantity);//添加商品到map中 System.out.println("添加商品"+goods.getName()+"成功!"); }else{ System.out.println("添加失败!商品编号不存在!"); } } /** * 按指定的编号删除商品 * @param id 商品编号 */ public void delGoods(int id){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.remove(goods);//从map删除商品 System.out.println("删除商品"+goods.getName()+"成功!"); }else{ System.out.println("删除失败!商品编号不存在!"); } } /** * 修改商品数量方法 * @param id 商品编号 * @param quantity 要修改的商品数量 */ public void updateGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.put(goods, quantity);//从map删除商品 }else{ System.out.println("修改失败!商品编号不存在!"); } } /** * 打印购物车商品列表 */ public void show(){ Set<Entry<Goods, Integer>> entrySet = map.entrySet(); System.out.println("编号\t单价\t数量\t名称\t总价"); for(Entry<Goods, Integer> entry:entrySet){ Goods goods = entry.getKey(); Integer quantity = entry.getValue(); System.out.println(goods.getId()+"\t"+goods.getPrice()+"\t"+quantity+"\t"+goods.getName()+"\t"+goods.getPrice()*quantity); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值