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.");
//Neue Daten abfragen
String newOrt = FormUtil.readString("Ort ("+h.getOrt()+")");
int newPlz = FormUtil.readInt("PLZ ("+h.getPlz()+")");
String newStrasse = FormUtil.readString("Straße ("+h.getStrasse()+")");
String newHausNummer = FormUtil.readString("Hausnummer ("+h.getHausnummer()+")");
int newFlaeche = FormUtil.readInt("Fläche ("+h.getFlaeche()+")");
int newStockwerke = FormUtil.readInt("Stockwerke ("+h.getStockwerke()+")");
int newKaufpreis = FormUtil.readInt("Kaufpreis ("+h.getKaufpreis()+")");
boolean newGarten = FormUtil.readBoolean("Garten ("+(h.isGarten() ? "j" : "n")+")");
String newOrt = FormUtil.readString("Ort", h.getOrt());
int newPlz = FormUtil.readInt("PLZ", h.getPlz());
String newStrasse = FormUtil.readString("Straße", h.getStrasse());
String newHausNummer = FormUtil.readString("Hausnummer", h.getHausnummer());
int newFlaeche = FormUtil.readInt("Fläche", h.getFlaeche());
int newStockwerke = FormUtil.readInt("Stockwerke", h.getStockwerke());
int newKaufpreis = FormUtil.readInt("Kaufpreis", h.getKaufpreis());
boolean newGarten = FormUtil.readBoolean("Garten", h.isGarten());
//Neue Daten setzen
if(!newOrt.equals(""))
h.setOrt(newOrt);
if(!newStrasse.equals(""))
h.setStrasse(newStrasse);
if(!newHausNummer.equals(""))
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.setOrt(newOrt);
h.setStrasse(newStrasse);
h.setHausnummer(newHausNummer);
h.setPlz(newPlz);
h.setFlaeche(newFlaeche);
h.setStockwerke(newStockwerke);
h.setKaufpreis(newKaufpreis);
h.setGarten(newGarten);
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.");
//Neue Daten abfragen
String newOrt = FormUtil.readString("Ort ("+w.getOrt()+")");
int newPlz = FormUtil.readInt("PLZ ("+w.getPlz()+")");
String newStrasse = FormUtil.readString("Straße ("+w.getStrasse()+")");
String newHausNummer = FormUtil.readString("Hausnummer ("+w.getHausnummer()+")");
int newFlaeche = FormUtil.readInt("Fläche ("+w.getFlaeche()+")");
int newStockwerk = FormUtil.readInt("Stockwerk ("+w.getStockwerk()+")");
int newMietpreis = FormUtil.readInt("Mietpreis ("+w.getMietpreis()+")");
boolean newEbk = FormUtil.readBoolean("EBK ("+(w.isEbk() ? "j" : "n")+")");
boolean newBalkon = FormUtil.readBoolean("Balkon ("+(w.isBalkon() ? "j" : "n")+")");
String newOrt = FormUtil.readString("Ort", w.getOrt());
int newPlz = FormUtil.readInt("PLZ", w.getPlz());
String newStrasse = FormUtil.readString("Straße", w.getStrasse());
String newHausNummer = FormUtil.readString("Hausnummer", w.getHausnummer());
int newFlaeche = FormUtil.readInt("Fläche", w.getFlaeche());
int newStockwerk = FormUtil.readInt("Stockwerk", w.getStockwerk());
int newMietpreis = FormUtil.readInt("Mietpreis", w.getMietpreis());
boolean newEbk = FormUtil.readBoolean("EBK", w.isEbk());
boolean newBalkon = FormUtil.readBoolean("Balkon", w.isBalkon());
//Neue Daten setzen
if(!newOrt.equals(""))
w.setOrt(newOrt);
if(!newStrasse.equals(""))
w.setStrasse(newStrasse);
if(!newHausNummer.equals(""))
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.setOrt(newOrt);
w.setStrasse(newStrasse);
w.setHausnummer(newHausNummer);
w.setPlz(newPlz);
w.setFlaeche(newFlaeche);
w.setStockwerk(newStockwerk);
w.setMietpreis(newMietpreis);
w.setEbk(newEbk);
w.setBalkon(newBalkon);
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.");
//Neue Daten abfragen
String new_name = FormUtil.readString("Name ("+m.getName()+")");
String new_address = FormUtil.readString("Adresse ("+m.getAdresse()+")");
String new_login = FormUtil.readString("Login ("+m.getLogin()+")");
String new_password = FormUtil.readString("Passwort ("+m.getPasswort()+")");
String new_name = FormUtil.readString("Name", m.getName());
String new_address = FormUtil.readString("Adresse", m.getAdresse());
String new_login = FormUtil.readString("Login", m.getLogin());
String new_password = FormUtil.readString("Passwort", m.getPasswort());
//Neue Daten setzen
if(!new_name.equals(""))
m.setName(new_name);
if(!new_address.equals(""))
m.setAdresse(new_address);
if(!new_login.equals(""))
m.setLogin(new_login);
if(!new_password.equals(""))
m.setPasswort(new_password);
m.setName(new_name);
m.setAdresse(new_address);
m.setLogin(new_login);
m.setPasswort(new_password);
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.");
//Neue Daten einlesen
String newVorname = FormUtil.readString("Vorname ("+p.getVorname()+")");
String newNachname = FormUtil.readString("Nachname ("+p.getNachname()+")");
String newAddress = FormUtil.readString("Adresse ("+p.getAdresse()+")");
String newVorname = FormUtil.readString("Vorname", p.getVorname());
String newNachname = FormUtil.readString("Nachname", p.getNachname());
String newAddress = FormUtil.readString("Adresse", p.getAdresse());
//Neue Daten setzen
if(!newVorname.equals(""))
p.setVorname(newVorname);
if(!newNachname.equals(""))
p.setNachname(newNachname);
if(!newAddress.equals(""))
p.setAdresse(newAddress);
p.setVorname(newVorname);
p.setNachname(newNachname);
p.setAdresse(newAddress);
service.editPerson(p);
}
}

View File

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