Hi everyone!
Not sure if ive put this in the right place, but am new here so wasnt quite sure!
Im trying to complete my java coursework for my deadline on thursday and am on the final/hardest exercise now!
The program is to manage the stock of the business. We have been given the start of the program with different methods which we then have to complete.
This semester has been my first time EVER using java, so sorry if im very very amateur! lol
Ive been working on this one part for hours though and cant get it right!
This is the part that isnt working... it just doesnt DO anything... ive tried so many different things, some not even looking like this, and this is my latest attempt. but i just cant seem to get it working!Code:/** * 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 p : stock) if (p.getID() == id) return p; return null; }
Would appreciate any help at all guys.
Thanks
a desperate flowergirl x x x
ps whole code below: (obviously a lot is missing as this was only the second part of the excercise.
Code:import java.util.ArrayList; /** * Manage the stock in a business. * The stock is described by zero or more Products. */ 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. */ /** * 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. */ /** * 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 p : stock) if (p.getID() == id) return p; 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. */ /** * Print details of all the products. */ public void printProductDetails() { for (Product p : stock) System.out.println(p); } /** * Locate a product with the maximum number in stock. If * there are more than one, we return the first one. * * @return The product with the maximum number in stock. */ /** * Returns a list of products which are low in stock (less than 5 * items in stock). * * @return Products with less than 5 items in stock. */ }
Last edited by WingedPanther; 05-12-2009 at 08:48 AM. Reason: add code tags (the # button)
How about now?
Well, you can't use the instance, getID????? never heard of it. I would rather use .equals(id)Code:import java.util.ArrayList; /** * Manage the stock in a business. * The stock is described by zero or more Products. * @param <Product> */ public class StockManager<Product>{ // 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. */ /** * 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. */ /** * 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 p : stock) if (p.equals(id)) //Changed # return p; 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. */ /** * Print details of all the products. */ public void printProductDetails(){ for (Product p : stock) System.out.println(p); } } /** * Locate a product with the maximum number in stock. If * there are more than one, we return the first one. * * @return The product with the maximum number in stock. */ /** * Returns a list of products which are low in stock (less than 5 * items in stock). * * @return Products with less than 5 items in stock. */
Hope it works !
Hiiii, thanks for your help!
But the getID is a method from another class.
There are two classes, Stock manager, and Product, and i need to be able to find a certain products ID from the product class, using the stock manager.
Sorry for the poor explanation! lol
Sorry!!
heres the 'product' class.
ps thanks for everything!!Code:/** * Model some details of a product sold by a company. * * @author David J. Barnes and Michael Kolling * @version 2006.03.30 */ 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); } } }
Last edited by WingedPanther; 05-12-2009 at 08:49 AM. Reason: fix code tags
Sorry!!
heres the 'product' class.
ps thanks for everything!!Code:/** * Model some details of a product sold by a company. * */ 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); } } }
Last edited by WingedPanther; 05-12-2009 at 08:49 AM. Reason: fix code tags
public Product findProduct(int id)
{
for (Product p : stock)
if (p.getID() == id)
return p;
return null;
}
//first of all Product has a constructor forcing you to enter the basic requirements
//you never instanciated your object "Product p". In that state, it is equal to null
//Product p = new Product("integer ID", "String name");
//now the object resembles something that can be checked for data via the p.getID()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks