public Product productName if productName null throw

Public product productname if productname null throw

This preview shows page 2 - 4 out of 4 pages.

public Product removeFromWishList(String productName) { if (productName == null) throw new IllegalArgumentException(); for (int i = 0; i < wishList.size(); i++) { if (wishList.get(i).getName().equals(productName)) { System.out.println("Removed from wishlist"); return wishList.remove(i); } } return null; } /** * Print each product in the user's wishlist in its own line using the * PrintStream object passed in the argument * @param printStream The printstream object on which to print out the * wishlist */ public void printWishList(PrintStream printStream){ for (int i = 0; i < wishList.size(); i++) { printStream.append(wishList.get(i).toString() + "\n"); }
Image of page 2
} /** * Buys the specified product in the user's wishlist. * Charge the user according to the price of the product by updating the credit * Remove the product from the wishlist as well * Throws an InsufficientCreditException if the price of the product is greater than the credit available. * * @param productName name of the product * @return true if successfully bought, false if product not found * @throws InsufficientCreditException if price > credit */ public boolean buy(String productName) throws InsufficientCreditException { for (int i = 0; i < wishList.size(); i++) { if (wishList.get(i).getName().equals(productName)) { if (wishList.get(i).getPrice() <= credit) { credit -= wishList.get(i).getPrice(); wishList.remove(i); return true; } else { throw new InsufficientCreditException(); } } } return false; } /** * Returns the credit of the user * @return the credit */ public int getCredit(){ return credit; } /** * This method is already implemented for you. Do not change. * Declare the first N items in the currentUser's wishlist to be in stock * N is generated randomly between 0 and size of the wishlist * * @returns list of products in stock */ public ListADT<Product> generateStock() { ListADT<Product> inStock= new DLinkedList<Product>(); int size=wishList.size(); if(size==0) return inStock; int n=randGen.nextInt(size+1);//N items in stock where n>=0 and n<size //pick first n items from wishList for(int ndx=0; ndx<n; ndx++) inStock.add(wishList.get(ndx)); return inStock;
Image of page 3
} }
Image of page 4

You've reached the end of your free preview.

Want to read all 4 pages?

  • Spring '08
  • MarvinSolomon
  • Data Structures, Login, Wishlist, InsufficientCreditException

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture