In class we decides on a group as to the ShoppingCart API, presented below. Note that the shopping cart has a cursor to keep track of the current item. In this way the items in the cart can be enumerated such as:
ShoppingCart sh = new ShoppingCart() ; // ... etc... Item item ; int howMany ; if ( sh.gotoNth(0) ) do { item = sh.getItem() ; howMany = sh.getQuantity() ; } while ( sh.gotoNext() ) ;
class ShoppingCart { void addItem( Item itemToAdd, int quantity) throws ShoppingCartFullException { // adds itemToAdd with quantity to shopping cart. } int getTotalCost() { // returns the total cost, an integer in cents (or real) } Item getItem() { // return current item } int getQuantity( ) { // return quantity of current item } // these are for listing the items in the cart boolean gotoNth(int nth) { // sets the cursor to the n-th item, counting // from 0 } boolean gotoNext() { // returns flase if there is no next, you are at the end } void changeQuantity(int newQuantity) { // if you change to 0 quantity, item is removed // from cart ... } }