12 Replies - 1207 Views - Last Post: 08 February 2012 - 04:21 PM Rate Topic: -----

#1 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

How do I add information into an ArrayList below? Am I thinking right?

Posted 06 February 2012 - 08:32 PM

I don't even know if I'm going right. :/

public class Shopping {
    public static void main(String args[]){
        System.out.println("How many carts do you want? ");
        
        System.out.println("Please estimate number of item you want in this Cart! ");
        
    }
}



import java.util.ArrayList;
public class Cart {
    ArrayList<Item> cart1= new ArrayList<Item>();
   }



I am trying to add three variables in "Item" class in the "cart1" (in Cart class) arraylist in one index without changing the data type (if this is possible). How can i do that?
public class Item {
    String name;
    Integer price;
    Integer quantity;
    
    public Item(String n, int p, int q){
        name = n;
        price = p;
        quantity = q;                
    }
    
    public String getName(){
        return name;
    }
    
    public int getPrice(){
        return price;
    }
    
    public int getQuantity(){
        return quantity;
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: How do I add information into an ArrayList below? Am I thinking right?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 06 February 2012 - 08:39 PM

You add the object as the whole, create an object of Item(which means will have all three instance variables), then add the object to the list. Something like:
cart1.add(new Item("name",3,4));


Also dont use wrapper classes data type for your variables, no need for that. Just use int instead of Integer.
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,754
  • Joined: 19-March 11

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 06 February 2012 - 08:42 PM

Your Cart class needs a method to add an item to the cart.

It should have public visibility, return a void, and accept an Item as a parameter. It'll be quite simple to write, one line will do it.

Once you've done that, you'll be able to put Items into the cart, but you won't be able to do anything with them. That's another issue, though.
Was This Post Helpful? 3
  • +
  • -

#4 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 06:03 AM

View Postjon.kiparsky, on 06 February 2012 - 08:42 PM, said:

Your Cart class needs a method to add an item to the cart.

It should have public visibility, return a void, and accept an Item as a parameter. It'll be quite simple to write, one line will do it.

Once you've done that, you'll be able to put Items into the cart, but you won't be able to do anything with them. That's another issue, though.

Yes, i just want to print them. What kind of method am i looking for? Can i get an example? Isn't smohd right?

View Postsmohd, on 06 February 2012 - 08:39 PM, said:

You add the object as the whole, create an object of Item(which means will have all three instance variables), then add the object to the list. Something like:
cart1.add(new Item("name",3,4));


Also dont use wrapper classes data type for your variables, no need for that. Just use int instead of Integer.


Yes I tried that and it doesn't seem to work. Jon says to create a method with public modifier returning nothing.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 06:08 AM

You will need to create a public method in Cart class which will take in parameter of what you need to add. Then use the list add method to add the passed parameter.
The above was just an idea showing how to add as whole object
Was This Post Helpful? 3
  • +
  • -

#6 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 10:05 AM

View Postsmohd, on 07 February 2012 - 08:08 AM, said:

You will need to create a public method in Cart class which will take in parameter of what you need to add. Then use the list add method to add the passed parameter.
The above was just an idea showing how to add as whole object


Okay, I was thinking to add the whole object in one index. For example, one item in index [0], then trying to print the three information (name , price , and quantity) about the item. Can i do that?
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 10:17 AM

Sure you can since your arraylist is of type Item then you can add an item, which will have all info that you need as what we have said before.
So create a method to add info that will pass parameter(here you have a decision, either to pass Item as the whole, or pass your three data ), then use the method add() of an arrayList to add an object of Item.
Also we access data using get() method in arrayList not [0].
Was This Post Helpful? 1
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,754
  • Joined: 19-March 11

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 10:25 AM

To add, you'd provide a method which accepts an Item and adds it to the ArrayList. A naive implementation would look like this:

public void addItem(Item item)
{
  itemList.add(item);
}


In the real world, there might be some validation and whatnot, but this is more or less what you need.


To print the information, you need to provide a method or methods which access the data. Simplest would be to just provide indexed access:
public Item getItem(int index)
{
  return itemList.get(index);
}


Again, bounds checking has been skipped. This would fail if you tried to access something off the edge of the arraylist, or if you provided a negative index.

Printing an Item's data is an issue for the Item class and the class that does the printing - they cooperate on this. Item should be able to return each of the fields that you would like to print, so you can format them for printing. You could return a formatted String with the information, but this is a mistake. You don't want to have to modify the Item class every time you want to print it. This is why you don't want to write a custom toString() for this purpose.
Display of complex information should be left to the class that wants to display it. As a ferinstance, suppose you write this program to display information correctly at the command line. You finish it, you've got it working, you're happy as a clam at high tide. Now your client comes back and wants it to also run in a GUI. Now, you don't want to screw up your working CLI version, so you whip up a set of methods to do what you should have done from the start, and return the data. You get your GUI calling these, and now you have a GUI that works, but you have two different ways of returning the information. Now you have unwanted complexity. If Item changes, you have two things to change, or else some refactoring to do. Bad design. Don't do that. Let the class that does the printing handle all the formatting, that's what it's paid for.
Was This Post Helpful? 1
  • +
  • -

#9 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 04:54 PM

So this is my attempt to print the instance variables in the arraylist. The output seems to be the address. What do you think?

public class Item {
    String name;
    int price;
    int quantity;
    
    
    
    public Item(String n, int p, int q){
        name = n;
        price = p;
        quantity = q;                
    }
    
    public String getName(){
        return name;
    }
    
    public int getPrice(){
        return price;
    }
    
    public int getQuantity(){
        return quantity;
    }
}



import java.util.ArrayList;
public class Cart {
    ArrayList<Item> cart1 = new ArrayList<Item>();
    
       
    
 public void addItem(Item item){
     
     cart1.add(item); 
     System.out.println(cart1.get(0));
 }
 
 
}



public class Shopping {
    public static void main(String args[]){
        
        Cart c = new Cart();
        Item i = new Item("Chicken", 3, 5);
        c.addItem(i);
        

        
    }
}


OUTPUT....

Item@42e816

Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,754
  • Joined: 19-March 11

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 04:59 PM

Quote

The output seems to be the address.


I prefer to say that it's a unique identifier assigned to that object, but yes, you may assume that it's the address on the heap. It's printed because there is no toString for your Item class, so it's using the one it inherits from Object.

I've discussed output in my previous post. You may use toString for the purpose, but I consider it bad design. Instead, I'd get the fields you want and format them for printing in the class that's doing the output.
Was This Post Helpful? 1
  • +
  • -

#11 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 07:09 PM

View Postjon.kiparsky, on 07 February 2012 - 06:59 PM, said:

Quote

The output seems to be the address.


I prefer to say that it's a unique identifier assigned to that object, but yes, you may assume that it's the address on the heap. It's printed because there is no toString for your Item class, so it's using the one it inherits from Object.

I've discussed output in my previous post. You may use toString for the purpose, but I consider it bad design. Instead, I'd get the fields you want and format them for printing in the class that's doing the output.


Yes, you're right! but im trying to learn toString method. Also now i am able to print the data but its not coming right out of cart1 arraylist. I want something like this to happen:
System.out.println(cart1.get(0));
with output
Chicken, 3, 5
. I want to use the arrayList
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 07 February 2012 - 08:13 PM

You will need a toString() method in your Item class, which will specify how your data should be displayed. That what jon was telling you. Put it there and you will see what happened.
Was This Post Helpful? 0
  • +
  • -

#13 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: How do I add information into an ArrayList below? Am I thinking right?

Posted 08 February 2012 - 04:21 PM

View Postsmohd, on 07 February 2012 - 10:13 PM, said:

You will need a toString() method in your Item class, which will specify how your data should be displayed. That what jon was telling you. Put it there and you will see what happened.


Ah i see! thanks. I'm still working on it. And I will post my final code to see how i did. Im so close. right now!!!!!! :1eye:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1