Closed Thread
Results 1 to 7 of 7

Thread: bluej java help

  1. #1
    flowergirl is offline Newbie
    Join Date
    May 2009
    Posts
    4
    Rep Power
    0

    bluej java help

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

    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)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: bluej java help

    How about now?

    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.
    */
    Well, you can't use the instance, getID????? never heard of it. I would rather use .equals(id)
    Hope it works !

  4. #3
    flowergirl is offline Newbie
    Join Date
    May 2009
    Posts
    4
    Rep Power
    0

    Re: bluej java help

    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

  5. #4
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: bluej java help

    Quote Originally Posted by flowergirl View Post
    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
    Could I get the both codes ?
    Also post them with these wraps before !
    So example !
    Code:
     <--- Text --->
    Like that, so first ["CODE"] Your text here...
    When your done close it like this... ["/CODE"]
    Cheers !

  6. #5
    flowergirl is offline Newbie
    Join Date
    May 2009
    Posts
    4
    Rep Power
    0

    Red face Re: bluej java help

    Sorry!!
    heres the 'product' class.

    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);
            }
        }
    }
    ps thanks for everything!!
    Last edited by WingedPanther; 05-12-2009 at 08:49 AM. Reason: fix code tags

  7. #6
    flowergirl is offline Newbie
    Join Date
    May 2009
    Posts
    4
    Rep Power
    0

    Re: bluej java help

    Sorry!!
    heres the 'product' class.

    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);
            }
        }
    }
    ps thanks for everything!!
    Last edited by WingedPanther; 05-12-2009 at 08:49 AM. Reason: fix code tags

  8. #7
    brightmatter is offline Learning Programmer
    Join Date
    Sep 2008
    Location
    Boise
    Posts
    36
    Rep Power
    0

    Re: bluej java help

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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. BlueJ Project help
    By tayrob04 in forum Java Help
    Replies: 4
    Last Post: 04-30-2010, 05:06 AM
  2. Java BlueJ
    By BlueJ in forum Java Help
    Replies: 1
    Last Post: 12-16-2009, 09:38 AM
  3. Replies: 4
    Last Post: 12-08-2008, 01:05 PM
  4. BlueJ Information
    By grungefreak1 in forum Java Help
    Replies: 2
    Last Post: 12-21-2007, 11:53 AM
  5. Starting with BlueJ
    By TcM in forum Java Help
    Replies: 6
    Last Post: 11-30-2007, 11:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

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