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
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 に答える