Results 1 to 10 of 10
  1. #1
    trojansc82 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default Need to change an ArrayList to an Array

    I need to change my StockManager project from an ArrayList to an Array that holds a maximum of 10 products in its stock. I also need to change the addProduct Method so it uses an Array.

    Here is the code right now:
    StockManager Class

    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    
    
    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 product)
        {
            stock.add(product);
        }
        
        /**
         * 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)
        {
            if(amount < 1){
                System.out.println("Invalid amount.");
             return;
            }
            Product p = findProduct(id);
            if(p != null){
                p.increaseQuantity(amount);
            }
            else{
                System.out.println("Can't find product.");
            }
            return;
            
        }
        
        /**
         * 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<Product> it = stock.iterator();
            while(it.hasNext()){
                Product tempProduct = it.next();
                if(tempProduct.getID() == id){
                    
                return tempProduct;
            }
            
           
        }
         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)
        {
            return 0;
        }
    
        /**
         * Print details of all the products.
         */
        public void printProductDetails()
        {
            
            for(Product product: stock){
            System.out.println(product.toString());
        }
        }
    }
    Product Class

    Java Code:
    
    public class Product
    {
        // An identifying number for this product.
        private int id;
        // The name of this product.
        private String name;
        // The quantity of this product in stock.
        private int quantity;
    
        /**
         * Constructor for objects of class Product.
         * The initial stock quantity is zero.
         * @param id The product's identifying number.
         * @param name The product's name.
         */
        public Product(int id, String name)
        {
            this.id = id;
            this.name = name;
            quantity = 0;
        }
    
        /**
         * @return The product's id.
         */
        public int getID()
        {
            return id;
        }
    
        /**
         * @return The product's name.
         */
        public String getName()
        {
            return name;
        }
    
        /**
         * @return The quantity in stock.
         */
        public int getQuantity()
        {
            return quantity;
        }
    
        /**
         * @return The id, name and quantity in stock.
         */
        public String toString()
        {
            return id + ": " +
                   name +
                   " stock level: " + quantity;
        }
    
        /**
         * Restock with the given amount of this product.
         * The current quantity is incremented by the given amount.
         * @param amount The number of new items added to the stock.
         *               This must be greater than zero.
         */
        public void increaseQuantity(int amount)
        {
            if(amount > 0) {
                quantity += amount;
            }
            else {
                System.out.println("Attempt to restock " +
                                   name +
                                   " with a non-positive amount: " +
                                   amount);
            }
        }
    
        /**
         * Sell one of these products.
         * An error is reported if there appears to be no stock.
         */
        public void sellOne()
        {
            if(quantity > 0) {
                quantity--;
            }
            else {
                System.out.println(
                    "Attempt to sell an out of stock item: " + name);
            }
        }
        
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I need to change my StockManager project from an ArrayList to an Array that holds a maximum of 10 products in its stock. I also need to change the addProduct Method so it uses an Array.
    So go ahead and do it. Asking us to do it before attempting it yourself will only harm you. If you have already attempted it and have run into a wall, then please post your attempt and a specific question regarding your attempt. Best of luck.

  3. #3
    trojansc82 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    I think I have it so far, what I have done is changed the initial ArrayList to an Array:

    Java Code:
    public class StockManager
    {
        // A list of the products.
        private Product [] stock;
    
        /**
         * Initialise the stock manager.
         */
        public StockManager()
        {
            stock = new Product [10];
        }
    I don't know what to do for the addProduct method. I left the method as it was and it returned with an error stating it can not find the method add(Product)

  4. #4
    trojansc82 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    This is the specific method I can not figure out how to convert so it will work with my Array:

    Java Code:
        public void addProduct(Product product)
        {
            stock.add(product);
        }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way is to add an int variable to StockManager, say count, that keeps track of how many Products have been added. It can be used as the arrays index when adding Products to the array, and the add method can throw an exception if you try to add more than Max (= 10) Products.

  6. #6
    trojansc82 is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    I think this one worked, but not 100 percent yet:

    Java Code:
        public void addProduct(Product item)
        {
            if (stock[item.getID()] == null){
                stock[item.getID()] = item;
            }
        }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry to say, but I'm not a big fan of your current attempt as it ties your Product's ID with the stock array index. The Product object shouldn't have to have any restrictions on its fields due to the array that holds it, and trying to do this will bite you in the end.

  8. #8
    kojha is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default

    Did you figure out how to do it? I'm stuck on the same problem. I got through the constructor but I can't figure out my methods. Nothing I try will compile! Let me know! I need help!!! I mostly learn from example but I can't find anything similar enough =[

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    You should start a new thread for your problem.
    Nothing I try will compile!
    If your have errors, please post the full text of the error message and the code that caused it.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Converting ArrayList to Array
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 03:34 PM
  2. [SOLVED] Should I use an arraylist or an array?
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 05:34 AM
  3. ArrayList versus Array
    By junpogi in forum New To Java
    Replies: 13
    Last Post: 11-08-2008, 03:04 AM
  4. Converting ArrayList to Array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 11:41 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •