⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcart.java

📁 beginJsp2.0外文书籍源代码
💻 JAVA
字号:
package com.wrox.shop;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Models a shopping cart holding a number of Books.
 */
public class ShoppingCart 
{
  
  private final List items = new ArrayList();

  public ShoppingCartItem getItem(int index) 
  {
    return (ShoppingCartItem)items.get(index);
  }
  
  public void setItem(int index, ShoppingCartItem item) 
  {
    items.set(index, item);
  }

  public List getItems() 
  {
    return items;
  }

  public double getTotal() 
  {
    double total = 0.0;
    for (Iterator i = items.iterator(); i.hasNext(); ) 
    {
      ShoppingCartItem item = (ShoppingCartItem)i.next();
      total += item.getBook().getPrice() * item.getQuantity();
    }
    return total;
  }

  public void addBook(Book book) 
  {
    ShoppingCartItem newItem = new ShoppingCartItem(book);
    if (items.contains(newItem)) 
    {
      ShoppingCartItem item = getItem(items.indexOf(newItem));
      item.setQuantity(item.getQuantity() + 1);
    }
    else 
    {
      items.add(newItem);
    }
  }

  public void validate() 
  {
    for (Iterator i = items.iterator(); i.hasNext(); ) 
    {
      ShoppingCartItem item = (ShoppingCartItem)i.next();
      if (item.getQuantity() <= 0) i.remove();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -