Results 1 to 10 of 10
- 07-08-2009, 03:45 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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
Product ClassJava 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()); } } }
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); } } }
-
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.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.
- 07-08-2009, 03:55 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
I think I have it so far, what I have done is changed the initial ArrayList to an Array:
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)Java Code:public class StockManager { // A list of the products. private Product [] stock; /** * Initialise the stock manager. */ public StockManager() { stock = new Product [10]; }
- 07-08-2009, 04:08 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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); }
-
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.
- 07-08-2009, 04:50 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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; } }
-
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.
- 07-25-2010, 07:28 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 1
- Rep Power
- 0
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 =[
- 07-25-2010, 08:07 PM #9
You should start a new thread for your problem.
If your have errors, please post the full text of the error message and the code that caused it.Nothing I try will compile!
-
Similar Threads
-
Converting ArrayList to Array
By vasavi.singh in forum New To JavaReplies: 1Last Post: 02-23-2009, 02:34 PM -
[SOLVED] Should I use an arraylist or an array?
By matzahboy in forum New To JavaReplies: 5Last Post: 11-17-2008, 04:34 AM -
ArrayList versus Array
By junpogi in forum New To JavaReplies: 13Last Post: 11-08-2008, 02:04 AM -
Converting ArrayList to Array
By Java Tip in forum Java TipReplies: 0Last Post: 11-13-2007, 10:41 AM -
Array to ArrayList
By javaplus in forum New To JavaReplies: 2Last Post: 11-12-2007, 12:46 AM


LinkBack URL
About LinkBacks


Bookmarks