📄 shoppingcart.java
字号:
package mypack;import java.util.*;public class ShoppingCart{ HashMap items=null; int numberOfItem=0; public ShoppingCart() { items=new HashMap(); } public synchronized void add(String ticketId, TicketDetails ticket) { if(items.containsKey(ticketId)) { ShoppingCartItem scitem=(ShoppingCartItem)items.get(ticketId); scitem.incrementQty(); } else { ShoppingCartItem newItem=new ShoppingCartItem(ticket); items.put(ticketId, newItem); numberOfItem++; } } public synchronized void remove(String ticketId) { if(items.containsKey(ticketId)) { ShoppingCartItem scitem=(ShoppingCartItem)items.get(ticketId); scitem.decrementQty(); if(scitem.getQty()<=0) { items.remove(ticketId); numberOfItem--; } } } public synchronized Collection getItems() { return items.values(); } public void finalize() throws Throwable { items.clear(); } public synchronized int getNumberOfItem() { return numberOfItem; } public synchronized double getTotal() { double amount=0.0; for(Iterator i=getItems().iterator();i.hasNext();) { ShoppingCartItem item=(ShoppingCartItem)i.next(); TicketDetails ticketDetails=(TicketDetails)item.getItem(); amount+=item.getQty()*ticketDetails.getPrice(); } return roundOff(amount); } private double roundOff(double x) { long val=Math.round(x*100); return val/100.0; } public synchronized void clear() { items.clear(); numberOfItem=0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -