shoppingcart.java

来自「完整的网上商城程序」· Java 代码 · 共 90 行

JAVA
90
字号
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 + =
减小字号Ctrl + -
显示快捷键?