From: Roland Haeder Date: Wed, 15 Jul 2015 12:40:14 +0000 (+0200) Subject: Introduced updateAddressData() + changing own "address data" is basicly finished X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a754609aa28461bbd8c95337a5260e1ebc5629df;p=jaddressbook-share-lib.git Introduced updateAddressData() + changing own "address data" is basicly finished Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/client/Client.java b/Addressbook/src/org/mxchange/addressbook/client/Client.java index eeb66b7..4a48b04 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/Client.java +++ b/Addressbook/src/org/mxchange/addressbook/client/Client.java @@ -16,8 +16,8 @@ */ package org.mxchange.addressbook.client; -import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.FrameworkInterface; +import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.menu.item.SelectableMenuItem; @@ -39,7 +39,7 @@ public interface Client extends FrameworkInterface { * * @param message Message to show to the user */ - public void displayMessage (final String message); + public void outputMessage (final String message); /** * Displays a "box" for the name @@ -58,6 +58,7 @@ public interface Client extends FrameworkInterface { /** * Let the user choose what to change on the address: [n]ame, [a]ddress, * [o]ther + * * @param contact Contact instance to let the user change data * @throws UnhandledUserChoiceException If choice is not supported */ @@ -65,6 +66,7 @@ public interface Client extends FrameworkInterface { /** * Asks the user for a choice and proceeds accordingly + * * @throws UnhandledUserChoiceException If choice is not supported */ public void doUserMenuChoice () throws UnhandledUserChoiceException ; @@ -76,6 +78,7 @@ public interface Client extends FrameworkInterface { /** * Asks the the user to enter a single character which must match validChars + * * @param validChars Valid chars that are accepted * @param message Message to user * @return Allowed character @@ -84,6 +87,7 @@ public interface Client extends FrameworkInterface { /** * Reads a string of minimum and maximum length from the user + * * @param minLength Minimum length of the string to read * @param maxLength Maximum length of the string to read * @param message Message to user @@ -92,14 +96,26 @@ public interface Client extends FrameworkInterface { */ public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty); + /** + * Reads an integer (int) from the user + * + * @param minimum Minimum allowed number + * @param maximum Maximum allowed number + * @param message Message to user + * @return Entered string by user or null if empty string is allowed + */ + public int enterInt (final int minimum, final int maximum, final String message); + /** * Setter for current menu choice + * * @param currentMenu Current menu choice */ public void setCurrentMenu (final String currentMenu); /** * Some "Getter" for menu item + * * @param accessKey Key to press to access this menu * @param text Text to show to user * @return diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index a67de85..532ba61 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -16,6 +16,7 @@ */ package org.mxchange.addressbook.client.console; +import java.text.MessageFormat; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -81,16 +82,7 @@ public class ConsoleClient extends BaseClient implements Client { public void displayAddressBox (final Contact contact) { // Simple display ... // @todo Use mask - this.displayMessage("Strasse, PLZ Ort, Land: " + contact.getStreet() + "\n" + contact.getZipCode() + " " + contact.getCity() + "\n" + contact.getCountryCode()); - } - - /** - * Displays textural message to the user - * @param message - */ - @Override - public void displayMessage (final String message) { - System.out.println(message); + this.outputMessage("Strasse, PLZ Ort, Land: " + contact.getStreet() + "\n" + contact.getZipCode() + " " + contact.getCity() + "\n" + contact.getCountryCode()); } /** @@ -110,10 +102,10 @@ public class ConsoleClient extends BaseClient implements Client { if ((companyName == null) || (companyName.isEmpty())) { // Now put all together: gender, surname, family name // @todo Use mask - this.displayMessage("Anrede, Vorname, Name: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName()); + this.outputMessage("Anrede, Vorname, Name: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName()); } else { // Company contact - this.displayMessage("Firma: " + companyName + "\nAnsprechpartner: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName()); + this.outputMessage("Firma: " + companyName + "\nAnsprechpartner: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName()); } } @@ -125,7 +117,7 @@ public class ConsoleClient extends BaseClient implements Client { @Override public void displayOtherDataBox (final Contact contact) { // Cellphone and such ... - this.displayMessage("Telefonnumer: " + contact.getPhoneNumber() + "\nFaxnummer: " + contact.getFaxNumber() + "\nHandy: " + contact.getCellphoneNumber() + "\nKommentar:\n" + contact.getComment()); + this.outputMessage("Telefonnumer: " + contact.getPhoneNumber() + "\nFaxnummer: " + contact.getFaxNumber() + "\nHandy: " + contact.getCellphoneNumber() + "\nKommentar:\n" + contact.getComment()); } @Override @@ -223,8 +215,38 @@ public class ConsoleClient extends BaseClient implements Client { return input; } + /** + * Reads an integer (int) with a textural message from the user + * + * @param minimum Minimum allowed number + * @param maximum Maximum allowed number + * @param message Messager to display in console + * @return + */ + @Override + public int enterInt (final int minimum, final int maximum, final String message) { + // Minimum should not be below zero + assert(minimum >= 0); + assert(maximum > minimum); + + // Init input + int input = -1; + + while ((input < minimum) || (input > maximum)) { + // Output message + System.out.print(message); + + // Read integer from user + input = this.readInt(); + } + + // Return it + return input; + } + /** * Reads a string of minimum and maximum length from the user + * * @param minLength Minimum length of the string to read * @param maxLength Maximum length of the string to read * @param message Message to user @@ -233,6 +255,9 @@ public class ConsoleClient extends BaseClient implements Client { */ @Override public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty) { + // Check on length, e.g. country codes are excactly 2 chars long + assert(maxLength >= minLength); + // Init input String input = null; @@ -263,6 +288,15 @@ public class ConsoleClient extends BaseClient implements Client { return new ConsoleMenuItem(accessKey,text); } + /** + * Displays textural message to the user + * @param message + */ + @Override + public void outputMessage (final String message) { + System.out.println(message); + } + /** * Shows textural menu on console */ @@ -274,7 +308,7 @@ public class ConsoleClient extends BaseClient implements Client { @Override public void showEntry (final SelectableMenuItem item) { // Access key then text - this.displayMessage("[" + item.getAccessKey() + "] " + item.getText()); + this.outputMessage("[" + item.getAccessKey() + "] " + item.getText()); } /** @@ -282,9 +316,9 @@ public class ConsoleClient extends BaseClient implements Client { */ @Override public void showWelcome () { - this.displayMessage("Welcome to " + AddressbookApplication.APP_TITLE + " v" + AddressbookApplication.APP_VERSION); - this.displayMessage(""); - this.displayMessage("Copyright(c) 2015 by Roland Haeder, this is free software"); + this.outputMessage("Welcome to " + AddressbookApplication.APP_TITLE + " v" + AddressbookApplication.APP_VERSION); + this.outputMessage(""); + this.outputMessage("Copyright(c) 2015 by Roland Haeder, this is free software"); // Debug message this.getLogger().debug("Intro shown to user"); @@ -323,7 +357,8 @@ public class ConsoleClient extends BaseClient implements Client { /** * Reads one character - * @return + * + * @return A single character */ private char readChar () { // Read line @@ -339,6 +374,30 @@ public class ConsoleClient extends BaseClient implements Client { return input.charAt(0); } + /** + * Reads an integer (int) from user + * + * @return An integer number + */ + private int readInt () { + // First read a string + String input = this.readString(); + + // Init number with invalid value + int num = -1; + + // Parse number, this can be risky + try { + num = Integer.parseInt(input); + } catch (final NumberFormatException e) { + this.outputMessage("Bitte geben Sie nur Zahlen ein!"); + this.getLogger().warn(MessageFormat.format("No numbers-only entered. input={0},message={1}", input, e.getMessage())); + } + + // Return read number + return num; + } + /** * Reads a string from a scanner until RETURN is pressed * diff --git a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java index 7bf18b0..da93b3d 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java @@ -468,6 +468,22 @@ public class BaseContact extends BaseFrameworkSystem { client.displayOtherDataBox((Contact) this); } + /** + * Updates address data in this Contact instance + * + * @param street Street + * @param zipCode ZIP code + * @param city City + * @param countryCode Country code + */ + public void updateAddressData (final String street, final int zipCode, final String city, final String countryCode) { + // Set all + this.setStreet(street); + this.setZipCode(zipCode); + this.setCity(city); + this.setCountryCode(countryCode); + } + /** * Updates name data in this Contact instance * @param gender Gender (M, F, C) diff --git a/Addressbook/src/org/mxchange/addressbook/contact/Contact.java b/Addressbook/src/org/mxchange/addressbook/contact/Contact.java index ee52c3c..b027237 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/Contact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/Contact.java @@ -156,8 +156,19 @@ public interface Contact extends FrameworkInterface { */ public void show (final Client client); + /** + * Updates address data in this Contact instance + * + * @param street Street + * @param zipCode ZIP code + * @param city City + * @param countryCode Country code + */ + public void updateAddressData (final String street, final int zipCode, final String city, final String countryCode); + /** * Updates name data in this Contact instance + * * @param gender Gender (M, F, C) * @param surname Surname * @param familyName Family name diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 7dca9da..0b4b47b 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -97,7 +97,29 @@ public class ContactManager extends BaseManager implements ManageableContact { */ @Override public void changeAddressData (final Contact contact, final Client client) { - throw new UnsupportedOperationException("Not supported yet."); + // First display it again + client.displayAddressBox(contact); + + // Is it own data? + if (contact.isOwnContact()) { + // Own address data + String street = this.enterOwnStreet(); + + // Get zip code + int zipCode = this.enterOwnZipCode(); + + // Get city name + String city = this.enterOwnCity(); + + // Get country code + String countryCode = this.enterOwnCountryCode(); + + // Update address data + contact.updateAddressData(street, zipCode, city, countryCode); + } else { + // Other contact's address data to change + throw new UnsupportedOperationException("Changing contact entries not finished."); + } } /** @@ -164,7 +186,7 @@ public class ContactManager extends BaseManager implements ManageableContact { */ if (!this.isOwnContactAdded()) { // Not added - this.getClient().displayMessage("Sie haben noch nicht Ihre Daten eingegeben."); + this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); // Skip any below code return; @@ -240,6 +262,24 @@ public class ContactManager extends BaseManager implements ManageableContact { return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true); } + /** + * Asks the user for his/her city's name + * + * @return City's name of the user + */ + private String enterOwnCity () { + return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false); + } + + /** + * Asks user for his/her own country code + * + * @return User's own country code + */ + private String enterOwnCountryCode () { + return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false); + } + /** * Asks the user for family name * @return Family name of the user @@ -256,6 +296,13 @@ public class ContactManager extends BaseManager implements ManageableContact { return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): "); } + /** + * Asks the user for own street (including number) + */ + private String enterOwnStreet () { + return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false); + } + /** * Asks the user for surname * @return Surname of the user @@ -264,6 +311,14 @@ public class ContactManager extends BaseManager implements ManageableContact { return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false); } + /** + * Asks the user for own ZIP code + * @return ZIP code + */ + private int enterOwnZipCode () { + return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: "); + } + /** * "Getter" for own contact instance or null if not found *