1

I am trying to merge two object arrays of a type "BookCollection" into one BookCollection array. I start by making a new blank BookCollection with a size equivalent to the sum of the other two BookCollections.

The code works with the first "collection1":

public static BookCollection merge(BookCollection collection1, BookCollection collection2) {
BookCollection mergedCollection = new BookCollection(collection1.getSize() + collection2.getSize());
int collectionSize = 0;
// Add books from collection1 to mergedCollection.
// This loop works, 2/21/15, 3:30
for (int c1 = 0; c1 < collection1.getSize(); c1++) {
  Book bookCopy = new Book(collection1.objectAt(c1));
  mergedCollection.addBook(bookCopy);
  collectionSize += 1;
}               

Now I run into trouble. My intention is to loop through the next BookCollection (collection2) and if the book exists, just update price and stock. If it doesn't exist, I want to add the book to the BookCollection.

// Loop through collection2
for (int c2 = 0; c2 < collection2.getSize(); c2++) {
  // Create a copy of the current book.
  Book bookCopy = new Book(collection2.objectAt(c2));
  String copyIsbn = bookCopy.getIsbn();
  // Loop through mergedCollection
  for (int m = 0; m < mergedCollection.getSize(); m++) {

    Book mergedBook = new Book(mergedCollection.objectAt(m));
    String mergedIsbn = mergedBook.getIsbn();

    // If the current book is already in mergedCollection.
    // NOT GETTING THROUGH THE LOGIC HERE.
    if (copyIsbn.equals(mergedIsbn)) {
      // Variables to update values in mergedCollections
      double price = bookCopy.getPrice();
      int stock = bookCopy.getStock();
      // If the mergedBook's price is larger than the collection's, change it.
      if (mergedBook.getPrice() > price) {
        mergedBook.setPrice(price);
      }
      // Update stock in mergedCollection
      mergedCollection.changeStock(mergedBook.getIsbn(), bookCopy.getStock() + mergedBook.getStock());    

    // If the current book is not in mergedCollection.      
    } else {
      mergedCollection.addBook(bookCopy);          
      collectionSize += 1;
    }          

  }
}
return mergedCollection;

}

I have tried to comment this well, and there is a comment in all caps where I believe the problem is. Now I also think it's pertinent to know that addBook() as used here:

mergedCollection.addBook(bookCopy);

basically says, if I parse through the array, and get a non-null return, then I can add the book.

Now, when I run this code, the compiler tells me the book I'm trying to add exists and stops. So I know that a) addBook is find a non-null value, and b)this little bit of logic:

if (copyIsbn.equals(mergedIsbn))

is not working as I want it to.

Why doesn't my program catch Book objects that occur twice and update the information?

4
  • 1
    Because you want a Set, specifically the TreeSet's addAll method. Commented Feb 22, 2015 at 0:41
  • I don't want a Set as I would like to learn why this method is not working. Thank you for introducing me to that idea and I will try to implement it with this once I figure out why my approach is not working. Commented Feb 22, 2015 at 0:44
  • "not working as I want it to" This isn't a useful description of the problem. What happens? Commented Feb 22, 2015 at 0:54
  • merge is supposed to merge the two BookCollections into a new BookCollection. If there is a duplicate occurrence of a Book object in the new BookCollection, then I just want to update the stock, and if the price in the second collection is lower than in the first, I want to update that to the lower price Commented Feb 22, 2015 at 0:55

3 Answers 3

1

I would suggest to use Set instead. Let me show you an example :

Lets assume your Book class is something like :

class Book
{
    private String bookNo;
    private int price;
    private int stock;

    public Book(String bookNo, int price, int stock) {
        this.bookNo = bookNo;
        this.price = price;
        this.stock = stock;
    }

    public String getBookNo() {
        return bookNo;
    }
    public int getPrice() {
        return price;
    }
    public int getStock() {
        return stock;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof Book) {
            if(this.bookNo.equals(((Book) obj).bookNo)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.bookNo.hashCode();
    }
}

Now to achive what you want, you can do something like :

    Set<Book> hs1 = new HashSet<Book>();
    Set<Book> hs2 = new HashSet<Book>();

    hs1.add(new Book("book1", 10, 10));
    hs1.add(new Book("book2", 20, 20));

    hs2.add(new Book("book3", 30, 30));
    hs2.add(new Book("book1", 40, 40));

    hs2.addAll(hs1);

hs2 have the desired data that you want.

Sign up to request clarification or add additional context in comments.

3 Comments

Please see my comment +1d
Yes, I completely agree with you.
This is a great answer, thank you. As I posted earlier I figured out a way to make my method work, but this is much more elegant and an awesome thing to know. Will be using this in the future for sure.
0

If your goal is to update the object, instead of creating a new instance:

   Book mergedBook = new Book(mergedCollection.objectAt(m));

use the existing instance already in the collection:

 Book mergedBook = mergedCollection.objectAt(m);

Then any updates made to mergedBook will be persistent to the object that is in the mergedCollection.

Comments

0

Ok, I got this figured out. I was always running:

mergedCollection.addBook(bookCopy);

All I had to do was create a:

found = false;

variable and imbed my .addBook function into that.

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.