Java eigen Klasse zum verwalten von Büchern

frager2345

Aktives Mitglied
Hi, also ich soll eine Klasse erstellen mit diesen Vorgaben (entschuldigt das Bild, will aber vermeiden dass durch Zusammenfassen Informationen verloren gehen):
1654557845535.png

Soweit so gut. Hier mal mein Ansatz:
Java:
public class Book {
    private String author;
    private String title;
    private LocalDate returnDate = null;
    private String status;
    public Book(String author, String title, String Status) throws IllegalArgumentException{
        setAuthor(author);
        setTitle(title);
        setReturnDate(returnDate);
        setStatus(status);
    }
    private static boolean checkAuthor(String author){
        return (author.matches("[a-zA-ZäÄöÖüÜß ]") && author.length() >= 2);
    }
    private static boolean checkTitle(String title){
        return (title.length() >= 1 && title.length() < 10);
    }
    public static boolean checkReturnDate(LocalDate returnDate){
        return (  );
    }
    public static boolean checkStatus(String status){
        return (status.equals("rent") || status.equals("not rent"));
    }
    public final void setAuthor(String author) throws IllegalArgumentException{
        if(!checkAuthor(author)){
            throw new IllegalArgumentException("author not valid: "+ author);
        }
        this.author = author;
    }
    public final void setTitle(String title) throws IllegalArgumentException{
        if(!checkTitle(title)){
            throw new IllegalArgumentException("title not valid: "+ title);
        }
        this.title = title;
    }
    private void setReturnDate(LocalDate returnDate) throws IllegalArgumentException{
        if(!checkReturnDate(returnDate)){
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy");
            throw new IllegalArgumentException("Return Date not valid: " + returnDate.format(dtf));
        }
        this.returnDate = returnDate;
    }
    private void setStatus(String status){
        if(!checkStatus(status)){
            throw new IllegalArgumentException("Status not valid:" + status);
        }
        this.status = status;
    }
    public String getAuthor(){
        return this.author;
    }
    public String getTitle(){
        return this.title;
    }
    public LocalDate getReturnDate(){
        return this.returnDate;
    }
    public String getStatus(){
        return this.status;
    }
    @Override
    public boolean equals(Object o) {
        if (o == null) return false;
        if (!(o.getClass().equals(this.getClass())))
            return false;
        Book a = (Book) o;
        return (a.getTitle() == this.getTitle());
    }
    @Override
    public String toString () {
        return getAuthor() + " : " + getTitle() + ".";
    }
    

}

Meine Probleme:
1. Wie erstelle ich die Check Methode für returnDate ? Kann mir irgendwie nicht ganz vorstellen, was hier das gewünschte Format ist (nur Zahlen?)
2. Wie genau operiere ich mit dem Ausleihsystem? Muss ich eine Methode schreiben, die beim Aufruf überprüft ob der Status "not rent" ist und dann das Objekt "ausleiht" bzw Status auf "rent" setzten und returnDate auf "14 Tage später" setzten ?
3.Für die Rückgabe dann eine Methode die das gleiche macht bloß auf "not rent" überprüft und dann returnDate auf null setzt wieder?
 
1. Wie erstelle ich die Check Methode für returnDate ? Kann mir irgendwie nicht ganz vorstellen, was hier das gewünschte Format ist (nur Zahlen?)
Du prüfst ob die Variable „returnDate“ „null“ ist wenn ja wirfst du deine Exception wenn nein gibst du das Datum zurück .
Ist ja im Gunde ein getter der eine Exception werfen kann. Eine Übergabe ist da eigentlich nicht erforderlich.

In der Klasse kannst du auf die privaten Variablen auch direkt zugreifen.

Tipp: für den Status würde sich ein Enum besser eignen.
 
Java:
...
    private LocalDate returnDate = null;
    private String status = "not rent";
    public Book(String author, String title) throws IllegalArgumentException{
        setAuthor(author);
        setTitle(title);
    }

den Status würe ich auch geleich auf "not rent" setzen.
Beim Objekt erzeugen reicht eigentlich Author und Title.
Es wird doch nicht gleich entliehen sein.
Dazu musst du ja Methoden Schreiben.
 
Dein Aufbau entspricht nicht der Aufgabe. Es soll kein Setter für Autor und Title existieren, zumindest nicht public. final-Setter verhindern nicht das ein Feld geändert wird. Die check-Methoden entsprechen auch nicht der Aufgabe, es soll lediglich auf nicht-leere-Zeichenketten geprüft werden. Das Brimborium in deinen check-Methoden ist nicht verlangt - daher falsch.

Ein LocalDate ist ein LocalDate, formatiert ist nur ein String. Es macht keinen Sinn ein Datum zu formatieren und dann zu prüfen ob das Format stimmt. returnDate kann entweder null sein oder in der Zukunft - wenn das Buch ausgeliehen ist. Ist das nicht so, dann stimmt was nicht.

Nach der Validierung hast du schon mal gefragt:
Java:
private static String validateText(String text, String errorMsg) {
    if (text == null || text.isBlank()) {
        throw new IllegalArgumentException(errorMsg);
    }
    return text;
}
Java:
public class Book {

    private final String author;
    private final String title;
    ...
  
    public Book(String author, String title, String status) {
        this.author = validateText(author, "author is not valid");
        this.title = validateText(title, "title is not valid");
        ...
    }

Das Feld status ist eigentlich sinnfrei, ist das Buch ausgeliehen dann wird das returnDate gesetzt. Jetzt ist doch klar das das Buch ausgeliehen ist: returnDate != null ist das Buch verfügbar returnDate == null. Doppelte Datenhaltung mit einem zusätzlichen Status vermeidet man immer, das führt nur zu inkonsistenten Zuständen.
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben