Results 1 to 4 of 4
  1. #1
    dandapice is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Beginner here and drowning fast

    Hello All,
    This is my first post here in the forum, so forgive me if I posted in the wrong spot. Anyway, I am taking a Java class through an online university and I am working on my week 6 project which requires us to do the following:

    • Modify the Inventory Program so the application can handle multiple items. Use an ArrayList to store the items. DO NOT PROMPT USER FOR INPUT; initialize the array with hard coded values.
    • The output should display the information the first product in your ArrayList, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.
    • Create a method to calculate the value of the entire inventory. Call this method and display the results. (Hint: Use a loop to make your calculation.)
    • Create another method to sort the array items by the name of the product.

    I am currently working on the first part of this assignment and my problem is when I try to "add" to my ArrayList and display the data, I get "inventory.Book@3301f287" instead of the data I am trying to add. I am posting code below and I hope I am not violating any rules by doing so...

    Inventory Class:

    package inventory;
    import java.util.ArrayList;
    /**
    * Inventory Class
    * @author Dan
    * Created 9/21/2012
    */
    public class Inventory {
    /**
    * @param args the command line arguments
    */
    double total = 0.0;

    public static void main(String[] args) {
    /* Create new ArrayList */
    ArrayList<Book> listBook = new ArrayList<>();
    /* Add objects to ArrayList */
    listBook.add(new Book("001a", "Java 101", 50, 19.95));
    listBook.add(new Book("001b", "Python 101", 50, 19.99));
    listBook.add(new Book("001c","C++ 101",50, 18.99));

    /* Print out objects from ArrayList */
    System.out.println("Item #\t Item Name\t In Stock\t Price Each");
    System.out.println(listBook.get(0));
    System.out.println(listBook.get(1)); /* this can be removed */
    System.out.println(listBook.get(2)); /* this can be removed */


    } /* End main */
    } /* End class */

    Book Class:

    package inventory;

    /**
    * Book Class
    * @author Dan
    * Created 9/21/2012
    */
    public class Book {
    /* Define variables */

    protected String itemNumber;
    protected String itemName;
    protected Integer inStock;
    protected Double priceEach;

    /* Parameterized Constructor */
    public Book(String itemNumber, String itemName, Integer inStock, Double priceEach)
    {
    this.itemNumber = itemNumber;
    this.itemName = itemName;
    this.inStock = inStock;
    this.priceEach = priceEach;
    }

    /* Calculate value of stock */
    public Double gettotal()
    {
    return this.priceEach * this.inStock;
    }
    /* Define Set and Get functions */
    public void setItemNumber(String java)
    {
    itemNumber = itemNumber;
    }
    public String getItemNumber()
    {
    return itemNumber;
    }
    public void setItemName(String java)
    {
    itemName = itemName;
    }
    public String getItemName()
    {
    return itemName;
    }
    public void setInStock(Integer java)
    {
    inStock = inStock;
    }
    public Integer getInStock()
    {
    return inStock;
    }
    public void setPriceEach(Double java)
    {
    priceEach = priceEach;
    }
    public Double getPriceEach()
    {
    return priceEach;
    }
    } /* End class */

    I'm sure it's something simple, but it's eluding me at the moment. Any ideas? No, I am not looking for someone to do my homework for me, just point out the obvious I am missing...

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default Re: Beginner here and drowning fast

    Please use codetags around your listing and indent the code: it makes it easier to read for us.
    That being said, there are a few small mistakes. You declare a listbook as ArrayList<Book>, but then you have to initialize it also as such (not with an empty <> ).
    The only problem left is that you didn't define a toString() for the Book-class. Every Object (=Class) has a toString() which is used for printing, but the default gives the rubbish you're looking at now. You have to override it (implement your own version).
    It is a good start. Good Luck!
    No bug ever had to calculate its fitnessfunction.

  3. #3
    dandapice is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Beginner here and drowning fast

    Hello Jodokus,
    Sorry about forgetting the tags. thank you for the reply. I am going to take what you advised and see what I can come up with. Upon success I will post the new code. Here goes nothing (I sure hope not anyways...)...

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Beginner here and drowning fast

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JOptionPane: HELP FAST!!!
    By Quizzle23 in forum New To Java
    Replies: 5
    Last Post: 02-03-2011, 11:16 PM
  2. Fast Help Plz ..
    By Hamodi18 in forum New To Java
    Replies: 6
    Last Post: 07-16-2010, 05:24 AM
  3. how fast?
    By abdullahansari in forum New To Java
    Replies: 4
    Last Post: 06-30-2010, 03:17 AM
  4. Help me fast!
    By Manikyr in forum New To Java
    Replies: 4
    Last Post: 03-16-2009, 07:43 AM
  5. pls help me fast!
    By rexson98 in forum New To Java
    Replies: 10
    Last Post: 10-15-2008, 12:20 PM

Posting Permissions

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