Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 10-14-2006, 10:04 PM   #1 (permalink)
 
Newb Techie

Join Date: Aug 2006

Posts: 14

bogie81

Send a message via AIM to bogie81 Send a message via Yahoo to bogie81
Default homework problem

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,
bogie81 is offline  
Old 10-14-2006, 11:11 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

So, what's your question?
jaeusm is offline  
Old 10-14-2006, 11:12 PM   #3 (permalink)
 
Newb Techie

Join Date: Aug 2006

Posts: 14

bogie81

Send a message via AIM to bogie81 Send a message via Yahoo to bogie81
Default

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.
bogie81 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On