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.

5
  • 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? Commented Nov 12, 2013 at 5: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. Commented Nov 12, 2013 at 5:21
  • 1
    You will want to go through this tutorial Commented Nov 12, 2013 at 5:22
  • ArrayList<InventoryItem> yourList = new ArrayList<InventoryItem>(); yourList.add(new InventoryItem("Pencil", 123456789, 1, 1.25)); Maybe you should review the basics first as was just suggested. Commented Nov 12, 2013 at 5:22
  • Code-style point: Java coders use lowercase first letters for local variables and non-static members, e.g. inventoryItem, itemNumber, etc. Commented Nov 12, 2013 at 5:56

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

2 Comments

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.
try get like this InventoryItem item = inventory.get(0); then you should able to get item.itemNumber,...
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

Comments

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!

1 Comment

Now the problem that I have is I need to sort my output. I have read these instructions as well as these but I can't seem to get it to work. Any ideas on how to sort this list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.