0

我正在尝试为我的 BookCollection 类编写一个 findBook 方法。它查找具有指定 ISBN 的书。我很难找到一种方法来比较不同类型的元素。我们得到了这个方法:

public void changePrice(String isbn, double price){
    int index = findBook(isbn);                                                //CREATE FINDBOOK METHOD TO HELP THIS METHOD
    if( index == -1){
        throw new UnsupportedOperationException("BookNotFound");
    }
    collection[index].setPrice(price); 
}

但是我对为什么将整数索引与带有字符串参数的 findbook 方法进行比较感到困惑。基本上,我们有一个 Book[] 类型的集合,并且使用给定的参数 isbn,必须在该图书集合中搜索那个 isbn。

这是对我到目前为止的粗略估计:

   //this method is a helper function
   private String findBook(int is){
      this.isbn = is;   
      for(int i = 0; i <= collection.length; i++){
         add(isbn);
      }  
   } 

我知道这种方法是错误的,但是我在思考如何编写这个方法时遇到了很多麻烦。如何使用字符串参数 isbn 搜索 Book[] 类型的集合?如果你们想要我的整个代码,请告诉我,我会发布它!

感谢任何帮助的人!

@drorbs 这是我的数据字段和构造函数:

  private int limit = 200;
   //Array of type book
   private int Book[];

   //actual size of collection, initialized to zero. Must never exceed limit
   private Book[] collection;    //collection is of book type    

   private int lastElement;

   //Constructor
   public BookCollection(int l){
      limit = l;
      int lastElement = 0;
         if(limit <= 200){
            Book[] collection = new Book[limit];
         } else{
            throw new UnsupportedOperationException("CannotExceedLimit");
           }   
      }
4

3 回答 3

1

我是否理解正确,您想在 Book[] 中搜索 ISBN,并返回找到的书的索引,或者如果找不到该书,则返回 -1?

我不知道 Book 是什么样子,但假设它有一个 getISBN() 方法,并且假设 collection 是你要搜索的 Book[],我会创建一个类似的方法:

private int findBook( String isbn ) {
    int returnIndex = -1;
    for ( int index = 0; index < collection.length; index++ ) {
        if ( collection[ index ].equals( isbn ) {
            returnIndex = index;
            break;
    }
    return returnIndex;
}
于 2013-11-13T22:50:21.170 回答
1

这种方法的一个简单的实现是:

private int findBook(String isbn){
    // iterate all the Book elements in the collection array
    for(int i = 0; i <= collection.length; i++){
        // check if the current book isbn matches the one provided argument
        if (collection[i].getIsbn().equals(isbn))
            return i;
    }
    return -1;
}

请注意,如果您正在查找书籍索引,则方法返回类型应该是 int,并且isbn参数应该是 String 类型。
此外,我认为books该数组的名称比collection.

于 2013-11-13T22:55:27.910 回答
1
import java.util.Arrays;
import java.util.List;

public class Library {

    public static void main(String[] args) {
        new Library();
    }

    private List<Book> collection;

    public Library() {
        collection = Arrays.asList(
            new Book("Foo", "000-0-00-000000-1", 0.0d),
            new Book("Bar", "000-0-00-000000-2", 0.0d),
            new Book("Baz", "000-0-00-000000-3", 0.0d)
        );

        Book b = collection.get(1);

        changePrice(b.getIsbn(), 3.50);

        System.out.println(b);
    }

    public int findBook(String isbn) {
        for (Book book : collection) {
            if (book.getIsbn().equals(isbn)) {
                return collection.indexOf(book);
            }
        }

        return -1;
    }

    public void changePrice(String isbn, double price) {
        int index = findBook(isbn);

        if (index < 0) {
            throw new UnsupportedOperationException("BookNotFound");
        }

        collection.get(index).setPrice(price);
    }

    public class Book implements Comparable<Book> {
        private String author;
        private String isbn;
        private double price;

        public Book() {
            this.author = "NOT FOUND";
            this.isbn = "000-0-00-000000-0";
            this.price = 0.0f;
        }

        public Book(String author, String isbn, double price) {
            this.author = author;
            this.isbn = isbn;
            this.price = price;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        public String getIsbn() {
            return isbn;
        }

        public void setIsbn(String isbn) {
            this.isbn = isbn;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        // You can use this to sort books.
        @Override
        public int compareTo(Book other) {
            return this.getIsbn().compareTo(other.getIsbn());
        }

        @Override
        public String toString() {
            return String.format("Book [author=%s, isbn=%s, price=$%.2f]",
                    author, isbn, price);
        }
    }
}
于 2013-11-13T23:13:31.530 回答