如果我的收藏中还没有一本书,我如何将它添加到我的收藏中呢? (Java)的

时间:2016-02-22 06:30:57

标签: java loops

我创建了一个BookCollection类,它拥有Books(我制作的另一个类)。我试图将两个BookCollections合并在一起,以制作collection3。如果他们有重复的书籍,我会添加他们的股票并采取最低价格。如果它们不重复,我将该书添加到collection3。我理解如何处理重复的书籍,但我的循环不适用于独特的书籍。这是我的代码,我认为你可以理解我的目标,而不会看到所有其他代码:

 public static BookCollection merge(BookCollection collection1, 
                                  BookCollection collection2){
   //Make sure the collection is big enough
  int collection1Size = collection1.getSize();
  int collection2Size = collection2.getSize();
  int collection3Size = collection1Size + collection2Size;
  BookCollection collection3 = new BookCollection(collection3Size);

  //loop through collection one and add it to collection 3
  for (int i = 0; i < collection1Size; i++){
     Book collection1Book = new Book(collection1.objectAt(i));
     collection3.addBook(collection1Book);
  }

  //loop through collection two and add certain books to collection 3

  for (int q = 0; q < collection3Size; q++){
     if (collection3.objectAt(q) != null){
       Book collection3Book = new Book(collection3.objectAt(q));
      for (int j = 0; j < collection2Size; j++){
         if (collection2.objectAt(j) != null){
          Book collection2Book = new Book(collection2.objectAt(j));

        // if the book at collection 2 is the same as a book already in collection 3
           if (collection2Book.getIsbn().equals(collection3Book.getIsbn())){
               String BookISBN = collection2Book.getIsbn();

               //Handles the price of the book
               double price1 = collection2Book.getPrice();
               double price2 = collection3Book.getPrice();
               if (price1 > price2){
                 collection3.changePrice(BookISBN, price2);
                }
               if (price1 < price2){
                 collection3.changePrice(BookISBN, price1);
               } 
               // Handles the stock of the book
               int stock1 = collection2Book.getStock();
               collection3.changeStock(BookISBN, stock1);                  
           }

           // adds the rest of the new books from collection 2 to collection
           if (!(collection2Book.getIsbn().equals(collection3Book.getIsbn()))){
              if (collection3.getSize() < collection3Size){
                 collection3.addBook(collection2Book);
              }

           }
         }
       } 
      }
    }   
  return collection3;
}

首先,我将所有书籍从collection1添加到collection3。然后我创建循环来检查collection2和collection3中的重复书籍。 伪代码很好,我只是不知道如何让我的程序看到来自collection2的书是独一无二的。谢谢!

4 个答案:

答案 0 :(得分:1)

以下是您的代码采用的方法:

for each B3 in collection 3
    for each B2 in collection 2
        if B2 and B3 are the same book, then make some changes to the book
        if B2 and B3 are not the same book, then add it
    end
end

这不能做你想做的事。为了判断集合2中的图书是否唯一,您必须首先将图书与所有现有图书进行比较。只有在您阅读了第3集中的所有书籍之后,您才知道是否应该将其添加为新书。你的代码没有这样做 - 因为&#34;不是同一本书&#34;逻辑发生得太早了。

正确的方法就像是

for each B2 in collection 2
    for each B3 in collection 3
        if B2 and B3 are the same book, then make some changes to the book--and make a note that B2 has been found
    end
    if B2 has not been found then add it
end 

请注意,最后一次检查必须在内部循环之外。

答案 1 :(得分:0)

您可以使用此类代码段...

public class BookCollection {

    class Book {
        @Override
        public boolean equals(Object that) {
            // Determine criteria for equality e.g: Comparing only unique id or price
            return this.equals(that);
        }
    }

    private List<Book> collection = new ArrayList<BookCollection.Book>();;

    public List<Book> getCollection() {
        return collection;
    }

    public void setCollection(List<Book> collection) {
        this.collection = collection;
    }

    public BookCollection merge(BookCollection b1, BookCollection b2) {
        BookCollection b3 = new BookCollection();
        b3.getCollection().addAll(b1.getCollection());
        for (Book book : b2.getCollection()) {
            if (!b3.getCollection().contains(book))
                b3.getCollection().add(book);
        }
        return b3;
    }
}

我希望这会有所帮助。 :)

答案 2 :(得分:0)

您尚未添加collection3.addBook(collection2Book);在第二个条件中,从bookcollection2复制有重复条目。

      if (collection2Book.getIsbn().equals(collection3Book.getIsbn())) {
           String BookISBN = collection2Book.getIsbn();
           collection3.addBook(collection2Book);
           //Handles the price of the book
           double price1 = collection2Book.getPrice();
           double price2 = collection3Book.getPrice();
           if (price1 > price2){
             collection3.changePrice(BookISBN, price2);
            }
           if (price1 < price2){
             collection3.changePrice(BookISBN, price1);
           } 
           // Handles the stock of the book
           int stock1 = collection2Book.getStock();
           collection3.changeStock(BookISBN, stock1);                  
       }

答案 3 :(得分:0)

这样做:

  1. 废弃您的BookCollection - JDK已经拥有其中一个:Collection<Book>
  2. Book实施Comparable<Book>,以便对书籍进行排序
  3. 将两个列表中的所有图书添加到新列表中(使用addAll()
  4. 致电Collections.sort(myNewList);对其进行排序
  5. 使用迭代器迭代书籍,保留对前一本书的引用(如果它为null则初始化为第一本书)。如果当前图书与上一本图书“相同”,请将其合并到上一本图书并调用iterator.remove()