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

📄 basket.java

📁 J2EE网络书店系统 采用JSP , servlet ,javabean开发 初学者可以参考
💻 JAVA
字号:
/*
 * 创建日期 2005-3-7
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package store;

/**
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
import java.util.*;

public class Basket {
    int MAXBOOKS = 100;

    /**
     * The Book hashtable stores all of the books in the
     * library basket.  The key is the book isbn, the value
     * stored is the quantity of that book. Initially, the
     * Book hashtable has a size of 15 and a load factor of .75.
    */

    Hashtable Books = new Hashtable( 15, (float) .75 );
    int numBooks=0;

    /**
     * Empty constructor.  All JavaBeans must have an empty constructor.
     * The student is not repsonsible for adding any code to this function.
     */
    public Basket()
    {}

    /**
     * Remove an item from the library basket.
     * @param ISBN String input isbn to remove
     * @return void
     */
    public void removeItem( String ISBN )
    {
        Integer Quantity;

        //get the quantity of the book from the hash table.

        Quantity = (Integer) Books.get( ISBN );

        //a null quantity means that no book with this ISBN is in the hashtable

        if (Quantity==null)
            {
                return;
            }

        /**
         * If there is only one copy of this particular book in the hash table, remove
         * the entry totally from the hash table.
         */
        if (Quantity.intValue()==1)
            {
                Books.remove( ISBN );
                numBooks--;
            }
        /**
         * If the quantity of this ISBN is greater than 1,
         * decrement the quantity and place the
         * new quanity back into the hashtable.
         */
        else
            {
                int tempQuant = Quantity.intValue();
                tempQuant--;
                Books.remove( ISBN );
                Books.put( ISBN, new Integer(tempQuant) );
            }
    }

    /**
     * Add an item to the library basket.
     * @param ISBN String isbn of item to add
     * @return void
     */
    public void addItem( String ISBN )
    {
        Integer Quantity;
        // Get the quantity for this book in the basket.
        Quantity = (Integer) Books.get( ISBN );
        /**
         * If the quantity is null, then this book is not in the basket, so
         * add it to the hashtable.
         */
        if (Quantity==null)
            {
                Quantity = new Integer(1);
                Books.put( ISBN, Quantity );
                numBooks++;
            }
        /**
         * If the book is in the basket, remove the entry
         * from the hash table and place the newly incremented
         * quantity in back into the basket.
         */
        else
            {
                int tempQuant = Quantity.intValue();
                tempQuant++;
                Books.remove( ISBN );
                Books.put( ISBN, new Integer(tempQuant) );
            }
    }

    /**
     * Return the number of distinct books in the basket.
     * @return int
     */
    public int getnumBooks()
    {
        return numBooks;
    }

    /**
     * Get the total number of books in the basket for a given ISBN.
     * @param ISBN String isbn of book
     * @return int
     */
    public int getQuantity( String ISBN )
    {
        Integer Quantity;
        Quantity = (Integer) Books.get( ISBN );
        if (Quantity==null)
            return 0;
        else
            return Quantity.intValue();
    }

    /**
     * Return an array of strings which are all of the distinct isbns in the basket.
     * @return String[]
     */
    public String [] getBooks()
    {
        String [] returnISBNs = new String[MAXBOOKS];
        int currPos = 0;
        for( Enumeration bookEnumeration = Books.keys(); bookEnumeration.hasMoreElements(); )
            {
                returnISBNs[currPos] = (String) bookEnumeration.nextElement();
                currPos++;
            }
        return returnISBNs;
    }

    public void clearBasket()
    {
      Books.clear();
      numBooks = 0;
    }
}

⌨️ 快捷键说明

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