?? librarybasket.java
字號:
package library;
import java.util.*;
import java.io.*;
import java.text.*;
/**
* Class <b>LibraryBasket</b> contains
* the specification of a JaveBean modeling
* a basket at a library.
*
* @author CTE
* @version 1.0
*/
public class LibraryBasket
{
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 LibraryBasket()
{}
/**
* 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;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -