Reintroduced default values the proper way

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
Jim Martens 2017-05-03 16:25:07 +02:00
parent 9a6146002c
commit 596c9677f2
4 changed files with 259 additions and 219 deletions

View File

@ -119,37 +119,23 @@ public class ImmobilienEditor {
System.out.println("Haus "+h.getStrasse()+" "+h.getHausnummer()+", "+h.getPlz()+" "+h.getOrt()+" wird bearbeitet. Leere Felder bzw. Eingabe von 0 lässt Feld unverändert."); System.out.println("Haus "+h.getStrasse()+" "+h.getHausnummer()+", "+h.getPlz()+" "+h.getOrt()+" wird bearbeitet. Leere Felder bzw. Eingabe von 0 lässt Feld unverändert.");
//Neue Daten abfragen //Neue Daten abfragen
String newOrt = FormUtil.readString("Ort ("+h.getOrt()+")"); String newOrt = FormUtil.readString("Ort", h.getOrt());
int newPlz = FormUtil.readInt("PLZ ("+h.getPlz()+")"); int newPlz = FormUtil.readInt("PLZ", h.getPlz());
String newStrasse = FormUtil.readString("Straße ("+h.getStrasse()+")"); String newStrasse = FormUtil.readString("Straße", h.getStrasse());
String newHausNummer = FormUtil.readString("Hausnummer ("+h.getHausnummer()+")"); String newHausNummer = FormUtil.readString("Hausnummer", h.getHausnummer());
int newFlaeche = FormUtil.readInt("Fläche ("+h.getFlaeche()+")"); int newFlaeche = FormUtil.readInt("Fläche", h.getFlaeche());
int newStockwerke = FormUtil.readInt("Stockwerke ("+h.getStockwerke()+")"); int newStockwerke = FormUtil.readInt("Stockwerke", h.getStockwerke());
int newKaufpreis = FormUtil.readInt("Kaufpreis ("+h.getKaufpreis()+")"); int newKaufpreis = FormUtil.readInt("Kaufpreis", h.getKaufpreis());
boolean newGarten = FormUtil.readBoolean("Garten ("+(h.isGarten() ? "j" : "n")+")"); boolean newGarten = FormUtil.readBoolean("Garten", h.isGarten());
//Neue Daten setzen //Neue Daten setzen
if(!newOrt.equals("")) h.setOrt(newOrt);
h.setOrt(newOrt); h.setStrasse(newStrasse);
h.setHausnummer(newHausNummer);
if(!newStrasse.equals("")) h.setPlz(newPlz);
h.setStrasse(newStrasse); h.setFlaeche(newFlaeche);
h.setStockwerke(newStockwerke);
if(!newHausNummer.equals("")) h.setKaufpreis(newKaufpreis);
h.setHausnummer(newHausNummer);
if(newPlz != 0)
h.setPlz(newPlz);
if(newFlaeche != 0)
h.setFlaeche(newFlaeche);
if(newStockwerke != 0)
h.setStockwerke(newStockwerke);
if(newKaufpreis != 0)
h.setKaufpreis(newKaufpreis);
h.setGarten(newGarten); h.setGarten(newGarten);
service.editHaus(h); service.editHaus(h);
@ -215,38 +201,24 @@ public class ImmobilienEditor {
System.out.println("Haus "+w.getStrasse()+" "+w.getHausnummer()+", "+w.getPlz()+" "+w.getOrt()+" wird bearbeitet. Leere Felder bzw. Eingabe von 0 lässt Feld unverändert."); System.out.println("Haus "+w.getStrasse()+" "+w.getHausnummer()+", "+w.getPlz()+" "+w.getOrt()+" wird bearbeitet. Leere Felder bzw. Eingabe von 0 lässt Feld unverändert.");
//Neue Daten abfragen //Neue Daten abfragen
String newOrt = FormUtil.readString("Ort ("+w.getOrt()+")"); String newOrt = FormUtil.readString("Ort", w.getOrt());
int newPlz = FormUtil.readInt("PLZ ("+w.getPlz()+")"); int newPlz = FormUtil.readInt("PLZ", w.getPlz());
String newStrasse = FormUtil.readString("Straße ("+w.getStrasse()+")"); String newStrasse = FormUtil.readString("Straße", w.getStrasse());
String newHausNummer = FormUtil.readString("Hausnummer ("+w.getHausnummer()+")"); String newHausNummer = FormUtil.readString("Hausnummer", w.getHausnummer());
int newFlaeche = FormUtil.readInt("Fläche ("+w.getFlaeche()+")"); int newFlaeche = FormUtil.readInt("Fläche", w.getFlaeche());
int newStockwerk = FormUtil.readInt("Stockwerk ("+w.getStockwerk()+")"); int newStockwerk = FormUtil.readInt("Stockwerk", w.getStockwerk());
int newMietpreis = FormUtil.readInt("Mietpreis ("+w.getMietpreis()+")"); int newMietpreis = FormUtil.readInt("Mietpreis", w.getMietpreis());
boolean newEbk = FormUtil.readBoolean("EBK ("+(w.isEbk() ? "j" : "n")+")"); boolean newEbk = FormUtil.readBoolean("EBK", w.isEbk());
boolean newBalkon = FormUtil.readBoolean("Balkon ("+(w.isBalkon() ? "j" : "n")+")"); boolean newBalkon = FormUtil.readBoolean("Balkon", w.isBalkon());
//Neue Daten setzen //Neue Daten setzen
if(!newOrt.equals("")) w.setOrt(newOrt);
w.setOrt(newOrt); w.setStrasse(newStrasse);
w.setHausnummer(newHausNummer);
if(!newStrasse.equals("")) w.setPlz(newPlz);
w.setStrasse(newStrasse); w.setFlaeche(newFlaeche);
w.setStockwerk(newStockwerk);
if(!newHausNummer.equals("")) w.setMietpreis(newMietpreis);
w.setHausnummer(newHausNummer);
if(newPlz != 0)
w.setPlz(newPlz);
if(newFlaeche != 0)
w.setFlaeche(newFlaeche);
if(newStockwerk != 0)
w.setStockwerk(newStockwerk);
if(newMietpreis != 0)
w.setMietpreis(newMietpreis);
w.setEbk(newEbk); w.setEbk(newEbk);
w.setBalkon(newBalkon); w.setBalkon(newBalkon);
service.editWohnung(w); service.editWohnung(w);

View File

@ -85,20 +85,16 @@ public class MaklerEditor {
System.out.println("Makler "+m.getName()+" wird bearbeitet. Leere Felder bleiben unverändert."); System.out.println("Makler "+m.getName()+" wird bearbeitet. Leere Felder bleiben unverändert.");
//Neue Daten abfragen //Neue Daten abfragen
String new_name = FormUtil.readString("Name ("+m.getName()+")"); String new_name = FormUtil.readString("Name", m.getName());
String new_address = FormUtil.readString("Adresse ("+m.getAdresse()+")"); String new_address = FormUtil.readString("Adresse", m.getAdresse());
String new_login = FormUtil.readString("Login ("+m.getLogin()+")"); String new_login = FormUtil.readString("Login", m.getLogin());
String new_password = FormUtil.readString("Passwort ("+m.getPasswort()+")"); String new_password = FormUtil.readString("Passwort", m.getPasswort());
//Neue Daten setzen //Neue Daten setzen
if(!new_name.equals("")) m.setName(new_name);
m.setName(new_name); m.setAdresse(new_address);
if(!new_address.equals("")) m.setLogin(new_login);
m.setAdresse(new_address); m.setPasswort(new_password);
if(!new_login.equals(""))
m.setLogin(new_login);
if(!new_password.equals(""))
m.setPasswort(new_password);
service.editEstateAgent(m); service.editEstateAgent(m);
} }

View File

@ -84,17 +84,14 @@ public class PersonEditor {
System.out.println("Person "+p.getVorname()+" "+p.getNachname()+" wird bearbeitet. Leere Felder bleiben unverändert."); System.out.println("Person "+p.getVorname()+" "+p.getNachname()+" wird bearbeitet. Leere Felder bleiben unverändert.");
//Neue Daten einlesen //Neue Daten einlesen
String newVorname = FormUtil.readString("Vorname ("+p.getVorname()+")"); String newVorname = FormUtil.readString("Vorname", p.getVorname());
String newNachname = FormUtil.readString("Nachname ("+p.getNachname()+")"); String newNachname = FormUtil.readString("Nachname", p.getNachname());
String newAddress = FormUtil.readString("Adresse ("+p.getAdresse()+")"); String newAddress = FormUtil.readString("Adresse", p.getAdresse());
//Neue Daten setzen //Neue Daten setzen
if(!newVorname.equals("")) p.setVorname(newVorname);
p.setVorname(newVorname); p.setNachname(newNachname);
if(!newNachname.equals("")) p.setAdresse(newAddress);
p.setNachname(newNachname);
if(!newAddress.equals(""))
p.setAdresse(newAddress);
service.editPerson(p); service.editPerson(p);
} }
} }

View File

@ -8,145 +8,220 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
/** /**
* Kleine Helferklasse zum Einlesen von Formulardaten * Small helper class for forms.
*/ */
public class FormUtil { public class FormUtil {
/** /**
* Liest einen String vom standard input ein * Reads a string from the console.
* @param label Zeile, die vor der Eingabe gezeigt wird *
* @return eingelesene Zeile * @param label
*/ * Label that is shown before the input
public static String readString(String label) { * @return read string
String ret = null; */
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); public static String readString(String label) {
return readString(label, "");
try { }
System.out.print(label+": ");
ret = stdin.readLine(); /**
} catch (IOException e) { * Reads a string from the console.
e.printStackTrace(); *
} * @param label
* Label that is shown before the input
return ret; * @param defaultValue
} * the default value in case an empty input is provided
* @return read string
/** */
* Liest ein Passwort vom standard input ein public static String readString(String label, String defaultValue) {
* @param label Zeile, die vor der Eingabe gezeigt wird String ret = null;
* @return eingelesene Zeile BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
*/
public static String readPassword(String label) { try {
String ret = null; System.out.print(label + (!defaultValue.isEmpty() ? "[" + defaultValue + "]" : "") + ": ");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); ret = stdin.readLine();
ret = ret.isEmpty() ? defaultValue : ret;
try { } catch (IOException e) {
System.out.print(label+": "); e.printStackTrace();
if (System.console() != null) { }
ret = String.valueOf(System.console().readPassword());
} return ret;
else { }
ret = stdin.readLine();
} /**
} catch (IOException e) { * Zeigt eine Nachricht an und wartet auf Bestätigung des Benutzers
e.printStackTrace(); *
} * @param msg Nachricht
return ret; */
} public static void showMessage(String msg) {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
/**
* Zeigt eine Nachricht an und wartet auf Bestätigung des Benutzers try {
* @param msg Nachricht System.out.print(msg);
*/ stdin.readLine();
public static void showMessage(String msg) { } catch (IOException e) {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); e.printStackTrace();
}
try { }
System.out.print(msg);
stdin.readLine(); /**
} catch (IOException e) { * Reads a password from the console.
e.printStackTrace(); *
} * @param label the label shown before the input
} * @return the entered password
*/
/** public static String readPassword(String label) {
* Liest einen Integer vom standard input ein String password = "";
* @param label Zeile, die vor der Eingabe gezeigt wird System.out.print(label + ": ");
* @return eingelesener Integer if (System.console() != null) {
*/ password = String.valueOf(System.console().readPassword());
public static int readInt(String label) { } else {
int ret = 0; try {
boolean finished = false; BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
password = stdin.readLine();
while(!finished) { } catch (IOException e) {
String line = readString(label); e.printStackTrace();
}
try { }
ret = Integer.parseInt(line);
finished = true; return password;
} catch (NumberFormatException e) { }
System.err.println("Ungültige Eingabe: Bitte geben Sie eine Zahl an!");
} /**
} * Reads a password from the console.
*
return ret; * @param label the label shown before the input
} * @param oldPassword the old password
* @return the entered password
/** */
* Liest ein Datum vom standard input im Format dd.MM.yyyy ein public static String readPassword(String label, String oldPassword) {
* @param label Zeile, die vor der Eingabe gezeigt wird String password = readPassword(label);
* @return eingelesenes Datum return password.isEmpty() ? oldPassword : password;
*/ }
public static Date readDate(String label) {
SimpleDateFormat parser = new SimpleDateFormat("dd.MM.yyyy"); /**
Date ret = null; * Liest ein Datum vom standard input im Format dd.MM.yyyy ein
boolean finished = false; * @param label Zeile, die vor der Eingabe gezeigt wird
* @return eingelesenes Datum
while(!finished) { */
String line = readString(label); public static Date readDate(String label) {
SimpleDateFormat parser = new SimpleDateFormat("dd.MM.yyyy");
Date ret = null;
boolean finished = false;
try {
ret = parser.parse(line); while(!finished) {
finished = true; String line = readString(label);
} catch (ParseException e) {
System.err.println("Ungültige Eingabe: Bitte geben Sie ein Datum im Format dd.MM.yyyy an!"); try {
} ret = parser.parse(line);
} finished = true;
} catch (ParseException e) {
return ret; System.err.println("Ungültige Eingabe: Bitte geben Sie ein Datum im Format dd.MM.yyyy an!");
} }
}
/**
* Stellt eine Ja/Nein-Frage und gibt das Ergebnis zurück return ret;
* @param label Zeile, die vor der Eingabe gezeigt wird }
* @return true, falls ja, false, falls nein
*/ /**
public static boolean readBoolean(String label) { * Reads an integer from the console.
String line = null; *
boolean finished = false; * @param label
boolean ret = false; * Label that is shown before the input
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); * @return read integer
*/
try { public static int readInt(String label) {
while(!finished) { return readInt(label, -1);
System.out.print(label+" [j/n]: "); }
line = stdin.readLine().toLowerCase();
/**
if(line.equals("j") || line.equals("ja")) { * Reads an integer from the console.
ret = true; *
finished = true; * @param label
} else if(line.equals("n") || line.equals("nein")) { * Label that is shown before the input
ret = false; * @param defaultValue
finished = true; * the default value
} else { * @return read integer
System.err.println("Bitte geben Sie ja oder nein bzw. j oder n ein!"); */
} public static int readInt(String label, int defaultValue) {
} int ret = 0;
} catch (IOException e) { boolean finished = false;
e.printStackTrace(); String defaultValueStr = defaultValue == -1 ? "" : String.valueOf(defaultValue);
}
while (!finished) {
return ret; String line = readString(label, defaultValueStr);
}
try {
ret = Integer.parseInt(line);
finished = true;
} catch (NumberFormatException e) {
System.err.println("Invalid input: Please insert a valid number!");
}
}
return ret;
}
/**
* Stellt eine Ja/Nein-Frage und gibt das Ergebnis zurück
*
* @param label Zeile, die vor der Eingabe gezeigt wird
* @return true, falls ja, false, falls nein
*/
public static boolean readBoolean(String label) {
label = label + "(j/n)";
return readBooleanIntern(label, false, false);
}
/**
* Stellt eine Ja/Nein-Frage und gibt das Ergebnis zurück
*
* @param label Zeile, die vor der Eingabe gezeigt wird
* @param defaultValue der Standardwert
* @return true, falls ja, false, falls nein
*/
public static boolean readBoolean(String label, boolean defaultValue) {
label = label + "(j/n) [" + (defaultValue ? "j" : "n") + "]";
return readBooleanIntern(label, true, defaultValue);
}
private static boolean readBooleanIntern(String label, boolean withDefault, boolean defaultValue) {
boolean finished = false;
String line;
boolean ret = false;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
try {
while(!finished) {
System.out.print(label+": ");
line = stdin.readLine().toLowerCase();
switch (line) {
case "j":
case "ja":
ret = true;
finished = true;
break;
case "n":
case "nein":
ret = false;
finished = true;
break;
case "":
if (withDefault) {
ret = defaultValue;
finished = true;
}
else {
System.err.println("Bitte geben Sie ja oder nein bzw. j oder n ein!");
}
break;
default:
System.err.println("Bitte geben Sie ja oder nein bzw. j oder n ein!");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
} }