0

I am just starting out with arraylists so please bear with me. I am trying to create an entry at index 0 with an item name, item number, amount in stock, and the price of the item. I think that I have built my array list to use my InventoryItem class, but I am not quite sure that I have done so. Here is my InventoryItem class.

class InventoryItem { //Delcare variables below

String ItemName;
int ItemNumber;
int InStock;
double UnitPrice;
double Value;

public InventoryItem(String ItemName, int ItemNumber, int InStock, double UnitPrice) { //declare variables for this class
    this.ItemName = ItemName;
    this.ItemNumber = ItemNumber;
    this.InStock = InStock;
    this.UnitPrice = UnitPrice;
    this.Value = UnitPrice * InStock; //calculate value of inventory

}

public void output() {
    System.out.println("Item Name = " + ItemName); //print out the item name
    System.out.println("Item Number = " + ItemNumber); //print out the item number
    System.out.println("In Stock = " + InStock); //print out how many of the item are in stock
    System.out.println("Item Price = $" + UnitPrice); //print out the price per item
    System.out.println("Value of inventory = $" + Value); //calculate how much the inventory is worth for this one item

}

As you can see there are strings, integers, and doubles listed in this class. Now I have created my arraylist as shown here. (I have tried to insert them all to the 0 spot)

package inventory;

import java.util.ArrayList;

public class Inventory {

public static void main(String[] args) {
    ArrayList InventoryItem = new ArrayList();
    InventoryItem.add(0, "Pencil ");
    InventoryItem.add(0, "123456789");
    InventoryItem.add(0, "1");
    InventoryItem.add(0, "1.25");

    for (int i = 0; i< InventoryItem.size(); i++)
        System.out.printf("%s", InventoryItem.get(i));
    System.out.println();

My problem is that I wanted this list to take an input on a single line. Can I do something like

ArrayList<String int int double> Stock = new ArrayList<String int int double>();
InventoryItem.add(0, "Pencil", 123456789, 1, 1.25);

Will this make my 0 entry as I have intended? I have tried to do this but it doesn't like it. I have also tried to put a comma in between each type, but that doesn't seem to work either. Maybe my display is not right for this situation? Any help would be great. This is really for a multidimensional type array where each line has these characteristics.

kerinr
  • 85
  • 1
  • 2
  • 11
  • 1
    You are misunderstanding encapsulation. You already have a class called `InventoryItem`, why don't you create instances of it and add those to your `ArrayList`? – Sotirios Delimanolis Nov 12 '13 at 05:18
  • That sounds great, however I haven't the foggiest how to do such a thing. I think that was originally what I was thinking that I could do, but I am a newbie here. – kerinr Nov 12 '13 at 05:21
  • 1
    [You will want to go through this tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/) – Sotirios Delimanolis Nov 12 '13 at 05:22
  • `ArrayList yourList = new ArrayList(); yourList.add(new InventoryItem("Pencil", 123456789, 1, 1.25));` Maybe you should review the basics first as was just suggested. – Radiodef Nov 12 '13 at 05:22
  • Code-style point: Java coders use lowercase first letters for local variables and non-static members, e.g. ``inventoryItem``, ``itemNumber``, etc. – brettw Nov 12 '13 at 05:56

3 Answers3

1

try this

ArrayList<InventoryItem> inventory = new ArrayList<InventoryItem>();
InventoryItem in_1 = new InventoryItem(0, "Pencil", 123456789, 1, 1.25);
inventory.add(in_1);

or in single line

inventory.add(new InventoryItem(0, "Pencil", 123456789, 1, 1.25));
subash
  • 3,116
  • 3
  • 18
  • 22
  • This does not work because it sees the 0 as being an integer and not being an index number. I tried both of your above examples. – kerinr Nov 12 '13 at 05:36
  • try get like this **InventoryItem item = inventory.get(0);** then you should able to get item.itemNumber,... – subash Nov 12 '13 at 05:56
0

As you already have the InventoryItem calss, create the object of it and set the values to the object and add this object to ArrayList something like this

   InventoryItem item = new InventoryItem("Pencil", 123456789, 1, 1.25);
   ArrayList<InventoryItem> itemList = new ArrayList<InventoryItem>();
   itemList.add(item);

While Retriving You will get the item Object, which has all the data you have set earlier

Siva
  • 1,938
  • 1
  • 17
  • 36
0

Thank you guys for your assistance. Without it I wouldn't have gotten to where I am right now. I have modified my code to look like this:

public static void main(String[] args) {

    ArrayList<inventoryItem> inventory = new ArrayList<inventoryItem>();
    inventory.add(new inventoryItem("Pencil", 1111, 10, .25));
    inventory.add(new inventoryItem("Pen", 9876, 2222, .99));
    inventory.add(new inventoryItem("Canned Air", 3333, 5, 2.00));
    inventory.add(new inventoryItem("Notebook", 4444, 10, 2.50));
    inventory.add(new inventoryItem("Staples", 5555, 5, 1.00));
    inventory.add(new inventoryItem("Paper Clips", 6666, 10000, .05));



    double total = 0;

    for (int i = 0; i < inventory.size(); i++) {
        inventory.get(i).output();

    }
    inventoryTotal(inventory);


}

public static void inventoryTotal(ArrayList<inventoryItem> inventory) {
    double total = 0;

    for (int i = 0; i < inventory.size(); i++) {
        total = total + inventory.get(i).value;
    }
    System.out.printf("Total value of inventory $%.2f\n", + total);

This allows me to add to the arraylist using inventory.add and assigning it. The next thing I now have to do is sort the items alphabetically, but I think I can handle that.

Thanks again for all your help!

kerinr
  • 85
  • 1
  • 2
  • 11
  • Now the problem that I have is I need to sort my output. I have read [these](http://stackoverflow.com/questions/16184653/sorting-arraylist-of-arrayliststring-in-java) instructions as well as [these](http://stackoverflow.com/questions/13046532/java-best-way-to-sort-an-arraylist-of-strings) but I can't seem to get it to work. Any ideas on how to sort this list? – kerinr Nov 13 '13 at 14:34