2017-05-11 5 views
2

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!

+0

Lesen Sie mehr über Java Namenskonventionen. Variablen sollten mit Kleinbuchstaben beginnen – Jens

+0

Sie sollten nach der Zeile zurückkehren: 'result = this.storage [i] .toString();' oder ersetzen Sie diese Zeile mit return 'this.storage [i] .toString();' – Jens

+0

Warum ein neues Buch erstellen? Sie suchen in einer Reihe von Büchern - geben Sie die übereinstimmende zurück. – duffymo

Antwort

3

Sie müssen das Buch zurück, wenn es gefunden wird, und null nur dann, wenn keine gefunden wird:

public Book search(String title) { 
    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!"); 
      return this.storage[i]; 
     } 
    } 
    System.out.println("The book with the title " + title + " does not exist in the library!"); 
    return null; 
} 
+0

Ich habe es ausprobiert und es hat ganz gut funktioniert. Vielen Dank! – djaszak

4

Ihr Problem ist hier:

for (int i = 0; i < this.Volume; i++) { 
     ... 
     } else { 
      System.out.println("The book with the title " + title + " does not exist in the library!"); 
      // *** THIS LINE IS WRONG *** 
      return null; 
     } 

Hier können Sie eine Schleife durch alle Ihre Bücher versuchen, das zu finden, das passt (das ist die Schleife, was ist, nicht wahr?). Leider, was das tatsächlich tut, ist es null auf dem ersten Buch zurückgegeben, das nicht übereinstimmt.

Sie brauchen etwas wie folgt aus:

public Book search(String title) { 
    Book found = null; 
    for (int i = 0; i < this.Volume && book == null; i++) { 
     if (title.equals(this.storage[i].toString())) { 
      System.out.println("The book with the title " + title + " exists in the library!"); 
      found = this.storage[i]; 
     } 
    } 
    // Check if we found it AFTER the loop completes. 
    if (found == null) { 
     System.out.println("The book with the title " + title + " does not exist in the library!"); 
    } 
    return found; 
} 

Hinweis hier, wie wir überprüfen, ob wir das Buch nach gefunden haben wir auf all die Bücher geschaut haben (oder wir es gefunden).

+0

Ich würde besser eine Pause hinzufügen; nachdem gefunden = this.storage [i]; wie es ist performanter (keine Notwendigkeit, einen vollständigen Scan durchzuführen, sobald das Buch gefunden wird) – Nemesis

+0

Oh okay, danke ich habe das Problem verstanden. Ich habe gerade die else-Anweisung gelöscht. Jetzt habe ich die Schleife, es funktioniert und wenn es kein passendes Buch findet, beendet es nur die Schleife, druckt eine Nachricht aus und gibt null zurück. Danke für die Hilfe! – djaszak

+0

Leistung ist kein Ziel in dem Fall, glaube ich. – kharandziuk

Verwandte Themen