Hi, also ich soll eine Klasse erstellen mit diesen Vorgaben (entschuldigt das Bild, will aber vermeiden dass durch Zusammenfassen Informationen verloren gehen):
Soweit so gut. Hier mal mein Ansatz:
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?
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?