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...:(shake):
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!
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...)...
Re: Beginner here and drowning fast
Please go through the Forum Rules -- particularly the third paragraph.
db