標籤:getc com sub out 初學者 判斷 outlook string 系統
購物項(Item)
(屬性)
商品對象(商品的pid也是購物項的主鍵) product
購買數量(使用者輸入) count
金額小計(購買數量*商品單價(product.getShop_price)) subtotal = count*product.getPrice()
購物車(cart)
(屬性)
map集合(商品的pid,購物項) map(String, Item)
金額總計(所有購物項的金額小計的總和,初始值為零) total=0.0
(動作)
添加購物項到購物車
從購物車刪除某個購物項
清空購物車
添加購物車過程
1.產生購物項item
2.判斷購物車中是否有此購物項
取出購物項item中的商品pid:pid = item.getProduct().getPid();
使用map.containsKey(pid)判斷購物車的map集合中是否已經包含該商品
2.1 購物車包含該商品:修改item中的購買數量,修改購物車中的金額總計
修改後的購買數量:count = item.setCount(map.get(pid).getCount()+item.getCount());
修改後的金額總計:total = total (原來的金額總計)+ item.getSubtotal()(新增的數量的金額總計);
2.2 購物車不包含該商品:直接將(商品pid,購物項)添加到map集合,並計算金額總計
直接添加map:map.put(item.getProduct().getPid(), item);
金額總計:total += total(原來的金額總計) + item.getSubtotal
添加購物車過結束
從購物車中刪除某個購物項
1.根據pid刪除購物項 CartItem item = map.remove(pid);
2.修改購物車中的金額總計
購物車當前金額總計 = 購物車金額總計 - 刪除的item的金額小計
購物車金額總計total -= item.getSubtotal();
購物車中刪除某商品過程結束
清空購物車
1.使用clear()方法清空購物車中的購物項 map.clear();
2.修改購物車金額總計為0.0 total = 0.0
商城系統中的購物車虛擬碼(java)(javaEE初學者,歡迎溝通交流 [email protected])