📄 estorebasket.java
字号:
package estore;
import java.util.*;
/**
* Class <b>EStoreBasket</b> contains
* the specification of a JaveBean modeling
* a basket at a E-Store.
*
* @author A Jiayi
* @version 1.0
*/
public class EStoreBasket {
/**
* The Book hashtable stores all of the books in the
* E-Store 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 EStoreBasket() {}
/**
* Remove an item from the E-Store 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 E-Store 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[Books.size()];
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 + -