1

i'm new to java and I am having problems adding my array objects to my list. I have already checked the topics in this forum but can't find something similar to my problem.

I have already made a class called Item that will save information about an item to be sold, and it has 4 instance variables id, desc, cost, quantity. And then I have a Sales class which has my main method. Basically what I want to do is build an array of objects from my Item class and hard code my data(which I have already created, i'm not sure if I did it right though).

I have created an ArrayList and what I want to do now is add the 5 objects from the array that I created into the list.

This is my Item class

public class Item {


    private String itemID;
    private String desc;
    private double cost;
    private int quantity;

    public Item() {
        itemID="";
        desc="";
        cost=0;
        quantity=0;
    }

    public Item(String id, String d, double c, int q) {
        itemID = id;
        desc = d;
        cost = c;
        quantity = q;

    }

    /**
     * @return the itemID
     */
    public String getItemID() {
        return itemID;
    }

    /**
     * @param itemID the itemID to set
     */
    public void setItemID(String itemID) {
        this.itemID = itemID;
    }

    /**
     * @return the desc
     */
    public String getDesc() {
        return desc;
    }

    /**
     * @param desc the desc to set
     */
    public void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * @return the cost
     */
    public double getCost() {
        return cost;
    }

    /**
     * @param cost the cost to set
     */
    public void setCost(double cost) {
        this.cost = cost;
    }

    /**
     * @return the quantity
     */
    public int getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Item [itemID=" + itemID + ", desc=" + desc + ", cost=" + cost
                + ", quantity=" + quantity + "]";
    }



}

And my Sales class:

import java.util.*;

public class Sales {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int i;

        Item[] items = new Item[5];


        for(i = 0; i < items.length; i++)
        {
            items[i]= new Item(); // create array 
        }

        //hard-coded values of id, desc, cost, qty
        items[0].setItemID("PN250");
        items[1].setItemID("ER100");
        items[2].setItemID("PP150");
        items[3].setItemID("MK200");
        items[4].setItemID("PN300");

        items[0].setDesc("Pen");
        items[1].setDesc("Eraser");
        items[2].setDesc("Paper");
        items[3].setDesc("Marker");
        items[4].setDesc("Pen");

        items[0].setCost(1.50);
        items[1].setCost(1.25);
        items[2].setCost(3.75);
        items[3].setCost(2.50);
        items[4].setCost(2.25);

        items[0].setQuantity(0);
        items[1].setQuantity(175);
        items[2].setQuantity(310);
        items[3].setQuantity(75);
        items[4].setQuantity(450);

        double total = 0;
        for(Item d : items)
        {

            System.out.print(d.getItemID());
            System.out.print("\t" + d.getDesc());
            System.out.print("\t" + d.getCost());
            System.out.println("\t" + d.getQuantity());
        }

        List<Item> obj;
        obj = new ArrayList<Item>();

    }
}
1
  • Any suggestions would be very much appreciated. Commented Aug 5, 2012 at 22:22

3 Answers 3

4

You could add the array into your list with the following:

obj.addAll(Arrays.asList(items));
Sign up to request clarification or add additional context in comments.

4 Comments

This may be a silly question. But i'll ask it anyway, will this code and the other one that was posted, add all my hard coded data in my array to my list?? And how will I know if it's been added, can I test it, or what I mean is print out my list?
System.out.println(obj) will print out your list BUT you must Overide the method toString in class Item to print a printable format for each object of the list
Ok! Thank you again, i've just tried and it works. And thanks also for the link.
@Mariell:If this works for you, you can accept the answer.This way you encourage people to help you
1

You can use:

for (i = 0; i < items.length; i++)
{
    obj.add(items[i]);
}

but why not add the items as soon as they are created & populated?

2 Comments

It's my class project, that's what my instructor wants us to do. We're also suppose to askthe user if they want to add more items. If no, then we have to print out the total of the cost. Thank you so much for the information, I will try to add it in my code.
Do you mean "why not" instead of "why now"?
1

instead of creating an array at the beginning of your function, create an arrayList and add objects to it using the put method.

Also, you should think of making your objects immutables and pass their attributes to their constructor instead of calling the setter for each attribute

Comments

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.