import java.util.LinkedList; import java.util.Iterator; /** * Second portion of Lab 6 * Manage the stock in a business. * The stock is described by zero or more Products. * @author John Sloan * @version 02-12-2008 */ public class StockManager { // A list of the products. private LinkedList stock; /** * Initialise the stock manager. */ public StockManager() { stock = new LinkedList(); } /** * */ public void printProductDetails () { for(Product product : stock) { System.out.println(product); } } /** * Add a product to the list. * @param item The item to be added. */ public void addProduct(Product item) { int ID = item.getID(); if( findProduct( ID ) != null ) { System.out.println( "Product with ID " + ID + " already exists" ); } else { stock.add(item); System.out.println( "Adding to Inventory: " + item ); } } /** * Receive a delivery of a particular product. * Increase the quantity of the product by the given amount. * @param id The ID of the product. * @param amount The amount to increase the quantity by. */ public void delivery(int id, int amount) { Product temp = findProduct( id ); if( temp != null ) { temp.increaseQuantity( amount ); } } /** * Try to find a product in the stock with the given id. * @return The identified product, or null if there is none * with a matching ID. */ public Product findProduct(int id) { Iterator it = stock.iterator(); boolean found = false; //invoices is a made up name; Product invoice = null; while( it.hasNext() && !found) { invoice = it.next(); //assign the next object returned to it (iterator) to invoice. if (id == invoice.getID ()) { found = true; } } if (found) { return invoice; } else { return null; } } /** * Locate a product with the given ID, and return how * many of this item are in stock. If the ID does not * match any product, return zero. * @param id The ID of the product. * @return The quantity of the given product in stock. */ public int numberInStock(int id) { Product temp = findProduct( id ); int number = 0; if( temp != null ) { number = temp.getQuantity(); } return number; } /** * Search the inventory for any product that has a quantity * less than the given level. The details of all low inventory * products are printed. * * @param level The quantity below which is considered low inventory */ public void lowInventory( int level ) { Iterator it = stock.iterator(); while(it.hasNext() ) { Product temp = it.next(); if(temp.getQuantity() < level) { System.out.println(temp); } } } /** * Locate the product with the given id and reduce the inventory * of that product by one. Print error message if product is not * in the inventory or if product's quantity is 0. * * @param id The ID of the product to sell */ public void sellProduct( int id ) { Product temp = findProduct(id); if(temp == null) { System.out.println("item with ID " + id + " not in inventory"); } else { temp.sellOne(); } } }