*/\r
package org.mxchange.addressbook.client;\r
\r
-import org.mxchange.addressbook.UnhandledUserChoiceException;\r
import org.mxchange.addressbook.FrameworkInterface;\r
+import org.mxchange.addressbook.UnhandledUserChoiceException;\r
import org.mxchange.addressbook.contact.Contact;\r
import org.mxchange.addressbook.menu.item.SelectableMenuItem;\r
\r
*\r
* @param message Message to show to the user\r
*/\r
- public void displayMessage (final String message);\r
+ public void outputMessage (final String message);\r
\r
/**\r
* Displays a "box" for the name\r
/**\r
* Let the user choose what to change on the address: [n]ame, [a]ddress,\r
* [o]ther\r
+ * \r
* @param contact Contact instance to let the user change data\r
* @throws UnhandledUserChoiceException If choice is not supported\r
*/\r
\r
/**\r
* Asks the user for a choice and proceeds accordingly\r
+ * \r
* @throws UnhandledUserChoiceException If choice is not supported\r
*/\r
public void doUserMenuChoice () throws UnhandledUserChoiceException ;\r
\r
/**\r
* Asks the the user to enter a single character which must match validChars\r
+ * \r
* @param validChars Valid chars that are accepted\r
* @param message Message to user\r
* @return Allowed character\r
\r
/**\r
* Reads a string of minimum and maximum length from the user\r
+ * \r
* @param minLength Minimum length of the string to read\r
* @param maxLength Maximum length of the string to read\r
* @param message Message to user\r
*/\r
public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty);\r
\r
+ /**\r
+ * Reads an integer (int) from the user\r
+ * \r
+ * @param minimum Minimum allowed number\r
+ * @param maximum Maximum allowed number\r
+ * @param message Message to user\r
+ * @return Entered string by user or null if empty string is allowed\r
+ */\r
+ public int enterInt (final int minimum, final int maximum, final String message);\r
+\r
/**\r
* Setter for current menu choice\r
+ * \r
* @param currentMenu Current menu choice\r
*/\r
public void setCurrentMenu (final String currentMenu);\r
\r
/**\r
* Some "Getter" for menu item\r
+ * \r
* @param accessKey Key to press to access this menu\r
* @param text Text to show to user\r
* @return \r
*/\r
package org.mxchange.addressbook.client.console;\r
\r
+import java.text.MessageFormat;\r
import java.util.Arrays;\r
import java.util.HashMap;\r
import java.util.Map;\r
public void displayAddressBox (final Contact contact) {\r
// Simple display ...\r
// @todo Use mask\r
- this.displayMessage("Strasse, PLZ Ort, Land: " + contact.getStreet() + "\n" + contact.getZipCode() + " " + contact.getCity() + "\n" + contact.getCountryCode());\r
- }\r
-\r
- /**\r
- * Displays textural message to the user\r
- * @param message \r
- */\r
- @Override\r
- public void displayMessage (final String message) {\r
- System.out.println(message);\r
+ this.outputMessage("Strasse, PLZ Ort, Land: " + contact.getStreet() + "\n" + contact.getZipCode() + " " + contact.getCity() + "\n" + contact.getCountryCode());\r
}\r
\r
/**\r
if ((companyName == null) || (companyName.isEmpty())) {\r
// Now put all together: gender, surname, family name\r
// @todo Use mask\r
- this.displayMessage("Anrede, Vorname, Name: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName());\r
+ this.outputMessage("Anrede, Vorname, Name: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName());\r
} else {\r
// Company contact\r
- this.displayMessage("Firma: " + companyName + "\nAnsprechpartner: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName());\r
+ this.outputMessage("Firma: " + companyName + "\nAnsprechpartner: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName());\r
}\r
}\r
\r
@Override\r
public void displayOtherDataBox (final Contact contact) {\r
// Cellphone and such ...\r
- this.displayMessage("Telefonnumer: " + contact.getPhoneNumber() + "\nFaxnummer: " + contact.getFaxNumber() + "\nHandy: " + contact.getCellphoneNumber() + "\nKommentar:\n" + contact.getComment());\r
+ this.outputMessage("Telefonnumer: " + contact.getPhoneNumber() + "\nFaxnummer: " + contact.getFaxNumber() + "\nHandy: " + contact.getCellphoneNumber() + "\nKommentar:\n" + contact.getComment());\r
}\r
\r
@Override\r
return input;\r
}\r
\r
+ /**\r
+ * Reads an integer (int) with a textural message from the user\r
+ * \r
+ * @param minimum Minimum allowed number\r
+ * @param maximum Maximum allowed number\r
+ * @param message Messager to display in console\r
+ * @return \r
+ */\r
+ @Override\r
+ public int enterInt (final int minimum, final int maximum, final String message) {\r
+ // Minimum should not be below zero\r
+ assert(minimum >= 0);\r
+ assert(maximum > minimum);\r
+\r
+ // Init input\r
+ int input = -1;\r
+\r
+ while ((input < minimum) || (input > maximum)) {\r
+ // Output message\r
+ System.out.print(message);\r
+\r
+ // Read integer from user\r
+ input = this.readInt();\r
+ }\r
+\r
+ // Return it\r
+ return input;\r
+ }\r
+\r
/**\r
* Reads a string of minimum and maximum length from the user\r
+ * \r
* @param minLength Minimum length of the string to read\r
* @param maxLength Maximum length of the string to read\r
* @param message Message to user\r
*/\r
@Override\r
public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty) {\r
+ // Check on length, e.g. country codes are excactly 2 chars long\r
+ assert(maxLength >= minLength);\r
+\r
// Init input\r
String input = null;\r
\r
return new ConsoleMenuItem(accessKey,text);\r
}\r
\r
+ /**\r
+ * Displays textural message to the user\r
+ * @param message\r
+ */\r
+ @Override\r
+ public void outputMessage (final String message) {\r
+ System.out.println(message);\r
+ }\r
+\r
/**\r
* Shows textural menu on console\r
*/\r
@Override\r
public void showEntry (final SelectableMenuItem item) {\r
// Access key then text\r
- this.displayMessage("[" + item.getAccessKey() + "] " + item.getText());\r
+ this.outputMessage("[" + item.getAccessKey() + "] " + item.getText());\r
}\r
\r
/**\r
*/\r
@Override\r
public void showWelcome () {\r
- this.displayMessage("Welcome to " + AddressbookApplication.APP_TITLE + " v" + AddressbookApplication.APP_VERSION);\r
- this.displayMessage("");\r
- this.displayMessage("Copyright(c) 2015 by Roland Haeder, this is free software");\r
+ this.outputMessage("Welcome to " + AddressbookApplication.APP_TITLE + " v" + AddressbookApplication.APP_VERSION);\r
+ this.outputMessage("");\r
+ this.outputMessage("Copyright(c) 2015 by Roland Haeder, this is free software");\r
\r
// Debug message\r
this.getLogger().debug("Intro shown to user");\r
\r
/**\r
* Reads one character\r
- * @return \r
+ * \r
+ * @return A single character\r
*/\r
private char readChar () {\r
// Read line\r
return input.charAt(0);\r
}\r
\r
+ /**\r
+ * Reads an integer (int) from user\r
+ * \r
+ * @return An integer number\r
+ */\r
+ private int readInt () {\r
+ // First read a string\r
+ String input = this.readString();\r
+\r
+ // Init number with invalid value\r
+ int num = -1;\r
+\r
+ // Parse number, this can be risky\r
+ try {\r
+ num = Integer.parseInt(input);\r
+ } catch (final NumberFormatException e) {\r
+ this.outputMessage("Bitte geben Sie nur Zahlen ein!");\r
+ this.getLogger().warn(MessageFormat.format("No numbers-only entered. input={0},message={1}", input, e.getMessage()));\r
+ }\r
+\r
+ // Return read number\r
+ return num;\r
+ }\r
+\r
/**\r
* Reads a string from a scanner until RETURN is pressed\r
* \r
client.displayOtherDataBox((Contact) this);\r
}\r
\r
+ /**\r
+ * Updates address data in this Contact instance\r
+ *\r
+ * @param street Street\r
+ * @param zipCode ZIP code\r
+ * @param city City\r
+ * @param countryCode Country code\r
+ */\r
+ public void updateAddressData (final String street, final int zipCode, final String city, final String countryCode) {\r
+ // Set all\r
+ this.setStreet(street);\r
+ this.setZipCode(zipCode);\r
+ this.setCity(city);\r
+ this.setCountryCode(countryCode);\r
+ }\r
+\r
/**\r
* Updates name data in this Contact instance\r
* @param gender Gender (M, F, C)\r
*/\r
public void show (final Client client);\r
\r
+ /**\r
+ * Updates address data in this Contact instance\r
+ * \r
+ * @param street Street\r
+ * @param zipCode ZIP code\r
+ * @param city City\r
+ * @param countryCode Country code\r
+ */\r
+ public void updateAddressData (final String street, final int zipCode, final String city, final String countryCode);\r
+\r
/**\r
* Updates name data in this Contact instance\r
+ * \r
* @param gender Gender (M, F, C)\r
* @param surname Surname\r
* @param familyName Family name\r
*/\r
@Override\r
public void changeAddressData (final Contact contact, final Client client) {\r
- throw new UnsupportedOperationException("Not supported yet.");\r
+ // First display it again\r
+ client.displayAddressBox(contact);\r
+\r
+ // Is it own data?\r
+ if (contact.isOwnContact()) {\r
+ // Own address data\r
+ String street = this.enterOwnStreet();\r
+\r
+ // Get zip code\r
+ int zipCode = this.enterOwnZipCode();\r
+\r
+ // Get city name\r
+ String city = this.enterOwnCity();\r
+\r
+ // Get country code\r
+ String countryCode = this.enterOwnCountryCode();\r
+\r
+ // Update address data\r
+ contact.updateAddressData(street, zipCode, city, countryCode);\r
+ } else {\r
+ // Other contact's address data to change\r
+ throw new UnsupportedOperationException("Changing contact entries not finished.");\r
+ }\r
}\r
\r
/**\r
*/\r
if (!this.isOwnContactAdded()) {\r
// Not added\r
- this.getClient().displayMessage("Sie haben noch nicht Ihre Daten eingegeben.");\r
+ this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben.");\r
\r
// Skip any below code\r
return;\r
return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);\r
}\r
\r
+ /**\r
+ * Asks the user for his/her city's name\r
+ * \r
+ * @return City's name of the user\r
+ */\r
+ private String enterOwnCity () {\r
+ return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);\r
+ }\r
+\r
+ /**\r
+ * Asks user for his/her own country code\r
+ * \r
+ * @return User's own country code\r
+ */\r
+ private String enterOwnCountryCode () {\r
+ return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false);\r
+ }\r
+\r
/**\r
* Asks the user for family name\r
* @return Family name of the user\r
return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");\r
}\r
\r
+ /**\r
+ * Asks the user for own street (including number)\r
+ */\r
+ private String enterOwnStreet () {\r
+ return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);\r
+ }\r
+\r
/**\r
* Asks the user for surname\r
* @return Surname of the user\r
return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);\r
}\r
\r
+ /**\r
+ * Asks the user for own ZIP code\r
+ * @return ZIP code\r
+ */\r
+ private int enterOwnZipCode () {\r
+ return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");\r
+ }\r
+\r
/**\r
* "Getter" for own contact instance or null if not found\r
*\r