5 Replies - 356 Views - Last Post: 16 January 2013 - 04:27 PM Rate Topic: -----

#1 portillo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-January 13

Adding Objects to Arraylist

Posted 15 January 2013 - 09:11 PM

	public void addBook (Book newBook)
	{
		if (books.contains(newBook))
		{
			int b = books.indexOf(newBook);
			int a = books.get(B)/>.getQuantity();
			newBook.setQuantity(a++);
		}

		else
		{
			books.add(newBook);
		}
	}

This method is supposed to take in a newBook. If the newBook is already in the ArrayList, then it adds one to its quantity - an integer in the Book class. If the book is not in the ArrayList, then it adds it.

My driver contains two books that have the same title, thus the same book, but my code is passing over my if statement and jumping straight to the else.

		store.addBook(new Book ("Slaughterhouse Five", "Kurt Vonnegut", 8.99));
		store.addBook(new Book ("Slaughterhouse Five", "Kurt Vonnegut", 8.99));


The problem is that this code adds Slaughterhouse to my ArrayList twice, when it should just add one to the quantity. I think the problem is that these two book objects are different, so my contains() method does assume they are the same. I have some variables in the Book class - title, author, price, and quantity - with get methods for each that I can use to help establish if two objects are really the same book. One idea I had was to check each title in the ArrayList and compare it to the title of newBook. Every time the title was different, I added one to a counter. If the counter equals the size of the Arraylist, then the book is not in the ArrayList and I would add it. However, when I tried this, none of the books were added to the ArrayList, which was just empty when a checked what it contained.

Thanks!

Sorry, line six should look like
 int a = books.get(B)/>.getQuantity(); 


int a = books.get(B).getQuantity();

Is This A Good Question/Topic? 0
  • +

Replies To: Adding Objects to Arraylist

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7806
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: Adding Objects to Arraylist

Posted 15 January 2013 - 09:13 PM

I'm willing to be that your Book class does not implement an equals method. That being the case, thisBook.equals(thatBook) is going to fall back to the default equals of the Object class, which compares fopr identity. Since you've created these two instances by separate calls to the new Book() constructor, they're different objects in memory, hence comparison for identity fails, hence your library arraylist doesn't contain that particular copy Slaughterhouse 5, so it gets added.

If this is correct, implementing a suitable equals method in your book class will fix the problem.
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Adding Objects to Arraylist

Posted 15 January 2013 - 10:20 PM

My thoughts mimic that of jon.kiparsky. You have to tell Java how it can tell if your object is in the arraylist. It has to be able to compare them. It doesn't know how to compare this object called "book" without you telling it how to do so by defining an equals method. There you will tell Java that two books are equal if their titles are the same string.

:)

This post has been edited by Martyr2: 15 January 2013 - 10:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Adding Objects to Arraylist

Posted 16 January 2013 - 05:55 AM

This won't work

newBook.setQuantity(a++);

you increment a (the quantity) after having setting the value with your setter
You'll need

newBook.setQuantity(++a);
Was This Post Helpful? 0
  • +
  • -

#5 FallenG  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 12-January 13

Re: Adding Objects to Arraylist

Posted 16 January 2013 - 06:29 AM

On a somewhat separate note, why is quantity a property of Book?

I would argue that it is not the responsibility of a Book object to know how many books you have in your collection. If you had a library, you wouldn't be able to know how many of a certain book you have by looking at one of them.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Adding Objects to Arraylist

Posted 16 January 2013 - 04:27 PM

You can have 10,000 published books distributed into 1000 book store
May be the Book class responsability to know how many of themselve were published :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1