В рамках моего курса по изучению Java мы получили задание, в котором нам нужно реализовать книгу классов и библиотеку классов. В одной библиотеке должно храниться до 10 книг, а также должна быть возможность поиска книг в библиотеке. Теперь моя проблема в том, что мой метод поиска не работает. Может быть, у кого-то есть идеи, это мой код:
public class Library {
int Capacity = 10;
int Volume = 0;
Book[] storage = new Book[10];
public Library() {
System.out.println("Hello, I am a library, which can store up to 10 books!");
this.storage = new Book[10];
}
public void add(Book book) {
if (Volume < Capacity) {
this.storage[Volume] = book;
System.out.println("I added the book " + book + ".");
Volume++;
} else if (Volume >= Capacity) System.out.println("The library is full!");
}
public Book search(String title) {
String result = new String();
for (int i = 0; i < this.Volume; i++) {
if (title.equals(this.storage[i].toString())) {
System.out.println("The book with the title " + title + " exists in the library!");
result = this.storage[i].toString();
} else {
System.out.println("The book with the title " + title + " does not exist in the library!");
return null;
}
}
Book retBook = new Book(result);
return retBook;
}
}
public class Book {
String title;
public Book(String title){
this.title = title;
System.out.println("Book " + title + " created.");}
public String toString(){
return this.title;
};
}
В рамках моего курса по изучению Java мы получили задание, в котором нам нужно реализовать книгу классов и библиотеку классов. В одной библиотеке должно храниться до 10 книг, а также должна быть возможность поиска книг в библиотеке. Теперь моя проблема в том, что мой метод поиска не работает. Может быть, у кого-то есть идеи, это мой код:
[code]public class Library { int Capacity = 10; int Volume = 0; Book[] storage = new Book[10];
public Library() { System.out.println("Hello, I am a library, which can store up to 10 books!"); this.storage = new Book[10]; }
public void add(Book book) { if (Volume < Capacity) { this.storage[Volume] = book; System.out.println("I added the book " + book + "."); Volume++; } else if (Volume >= Capacity) System.out.println("The library is full!");
}
public Book search(String title) { String result = new String(); for (int i = 0; i < this.Volume; i++) { if (title.equals(this.storage[i].toString())) { System.out.println("The book with the title " + title + " exists in the library!"); result = this.storage[i].toString(); } else { System.out.println("The book with the title " + title + " does not exist in the library!"); return null; } } Book retBook = new Book(result); return retBook; } }
public class Book { String title;
public Book(String title){ this.title = title; System.out.println("Book " + title + " created.");}
public String toString(){ return this.title; }; } [/code]