Als Teil meiner Odysee auf Java zu lernen, haben wir eine Aufgabe, in der wir das Klassenbuch und die Klassenbibliothek implementieren müssen. Sie sollten bis zu 10 Bücher in einer Bibliothek speichern, außerdem sollten Sie die Möglichkeit haben, in der Bibliothek nach Büchern zu suchen. Jetzt ist mein Problem, dass meine Suchmethode nicht funktioniert. Vielleicht hat jemand hier eine Idee hat, das ist mein Code:Grundlegende Java-Bibliothek, Suchmethode funktioniert nicht
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;
};
}
Vielen Dank für jede Hilfe!
Lesen Sie mehr über Java Namenskonventionen. Variablen sollten mit Kleinbuchstaben beginnen – Jens
Sie sollten nach der Zeile zurückkehren: 'result = this.storage [i] .toString();' oder ersetzen Sie diese Zeile mit return 'this.storage [i] .toString();' – Jens
Warum ein neues Buch erstellen? Sie suchen in einer Reihe von Büchern - geben Sie die übereinstimmende zurück. – duffymo