homework problem

Status
Not open for further replies.

bogie81

Solid State Member
Messages
14
I'm totally lost with arrays in java. I have to change my class from using ArrayList to Array.

Code:
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Manage the stock in a business.
 * The stock is described by zero or more Products.
 * 
 * @author (Bogdan Cabaj) 
 * @version (Assignment 4 part 1)
 */
public class StockManager
{
    // A list of the products.
    private ArrayList<Product> stock;

    /**
     * Initialise the stock manager.
     */
    public StockManager()
    {
        stock = new ArrayList<Product>();
    }

    /**
     * Add a product to the list.
     * @param item The item to be added.
     */
    public void addProduct(Product item)
    {
        stock.add(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 product = findProduct(id);
        if(product != null) {
            product.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)
      {
        for(Product product : stock) {
            if (product.getID() == id) {
                return product;
            }            
        }
        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 product = findProduct(id);
        if(product != null) {
            return product.getQuantity();
        }
        else {
            return 0;
        }

    }

    /**
     * Print details of all the products.
     */

    
      public void printProductDetails()
    {
        Iterator it = stock.iterator();
        while(it.hasNext())  {
            System.out.println(it.next());
            
        }
    }
}

Here are the directions:
After completing the Product project Part One. Use a fixed size collection, an array of objects instead of the ArrayList collection. Assume a maximum of 10 products in stock (itÂ’s a very exclusive inventory).

• Change the addProduct method to use an array.

• Redo #1 of the Staged Implementation using an array instead of an ArrayList and a for loop instead of a for each loop in the printProductDetails method.

• Redo #2 of the Staged Implementation using an array instead of an ArrayList and a for loop instead of the while loop in the findProduct method. Null references should not be printed. Make sure that an invalid ID does not cause an abnormal interruption (null pointer exception).

• Test the numberInStock and delivery methods to ensure that they still function properly.

• Don’t use iterators.


I only have couple hours to submit my homework and I can't figure it out after looking through bunch of books. I guess it's not my good day today.

Thanks,
 
I'm having problem with running the findProduct() method. I'm not even sure where to start. It is suppose to be using arrays not arrayList.
 
Status
Not open for further replies.
Back
Top Bottom