Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Converting a method from ArrayList so it is capable with an Array

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting a method from ArrayList so it is capable with an Array

    I am trying to convert my addProcut method so that it is compatible with an array. The original addProduct method was compatible with my ArrayList.

    Here is the full code of the StockManager class with the Arraylist and methods:

    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());
        }
        }
    }

    The product class:

    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);
            }
        }
     
     
    }

    I have been able to convert the ArrayList in the StockManager class to an Array:

    public class StockManager
    {
        // A list of the products.
        private Product [] stock;
     
        /**
         * Initialise the stock manager.
         */
        public StockManager()
        {
            stock = new Product [10];
        }

    I am not able to compile the addProduct method so it will work with my Array.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Converting a method from ArrayList so it is capable with an Array

    The issue you have here is that you've already allocated an array with a maximum of 10 slots and since you are using an array rather than a list you cannot simply add items to it. You can however set each individual index to be what you want, for instance.

    public class StockManager
    {
        // A list of the products.
        private Product [] stock;
     
        /**
         * Initialise the stock manager.
         */
        public StockManager()
        {
            stock = new Product [10];
        }
     
        /**
         * Add a product to the list.
         * @param item The item to be added.
         */
        public void addProduct(Product product)
        {
            if(stock.length < 10)
            {
                stock[stock.length] = product;
            }
        }
    }

    That would ensure you never get an ArrayIndexOutOfBoundsException since you only have 10 slots in your array.

    I would personally go with a List when building a stock system though. What was the initial reason you wanted to use an array instead of a list for?

    // Json

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting a method from ArrayList so it is capable with an Array

    My assignment is to have the same program that uses an Array instead of an ArrayList.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM
  3. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. Replies: 4
    Last Post: May 1st, 2009, 03:32 PM