From: Roland Haeder Date: Wed, 15 Jul 2015 12:02:13 +0000 (+0200) Subject: Introduced updateNameData() + changing own "name data" is basicly finished X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3457fa799a3756c45072acd0da5ed82de5535aa0;p=addressbook-lib.git Introduced updateNameData() + changing own "name 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 2c4336c..eeb66b7 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/Client.java +++ b/Addressbook/src/org/mxchange/addressbook/client/Client.java @@ -87,9 +87,10 @@ public interface Client extends FrameworkInterface { * @param minLength Minimum length of the string to read * @param maxLength Maximum length of the string to read * @param message Message to user - * @return + * @param allowEmpty Whether empty strings are allowed + * @return Entered string by user or null if empty string is allowed */ - public String enterString (final int minLength, final int maxLength, final String message); + public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty); /** * Setter for current menu choice diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index 36c6faa..a67de85 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -136,15 +136,15 @@ public class ConsoleClient extends BaseClient implements Client { // @TODO Get rid of this ugly switch block, too switch (choice) { case 'n': // Name data - this.getContactManager().changeNameData(contact); + this.getContactManager().changeNameData(contact, this); break; case 'a': // Address data - this.getContactManager().changeAddressData(contact); + this.getContactManager().changeAddressData(contact, this); break; case 'o': // Other data - this.getContactManager().changeOtherData(contact); + this.getContactManager().changeOtherData(contact, this); break; case 'x': // Exit this menu @@ -228,15 +228,16 @@ public class ConsoleClient extends BaseClient implements Client { * @param minLength Minimum length of the string to read * @param maxLength Maximum length of the string to read * @param message Message to user - * @return + * @param allowEmpty Whether to allow empty string + * @return Entered string by user or null for empty strings */ @Override - public String enterString (final int minLength, final int maxLength, final String message) { + public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty) { // Init input String input = null; // Check if it is to short or to long - while ((input == null) || (input.length() < minLength) || (input.length() > maxLength)) { + while (((input == null) || ((input.length() < minLength) && (!allowEmpty))) || ((input.length() > 0) && (input.length() < minLength) && (allowEmpty)) || ((input instanceof String) && (input.length() > maxLength))) { // Output message System.out.print(message); diff --git a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java index 95335ff..7bf18b0 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java @@ -467,4 +467,19 @@ public class BaseContact extends BaseFrameworkSystem { // Display other data "box" client.displayOtherDataBox((Contact) this); } + + /** + * Updates name data in this Contact instance + * @param gender Gender (M, F, C) + * @param surname Surname + * @param familyName Family name + * @param companyName Company name + */ + public void updateNameData (final char gender, final String surname, final String familyName, final String companyName) { + // Set all + this.setGender(gender); + this.setSurname(surname); + this.setFamilyName(familyName); + this.setCompanyName(companyName); + } } diff --git a/Addressbook/src/org/mxchange/addressbook/contact/Contact.java b/Addressbook/src/org/mxchange/addressbook/contact/Contact.java index 1ea7d57..ee52c3c 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/Contact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/Contact.java @@ -155,4 +155,13 @@ public interface Contact extends FrameworkInterface { * @param client Client instance to call back */ public void show (final Client client); + + /** + * Updates name data in this Contact instance + * @param gender Gender (M, F, C) + * @param surname Surname + * @param familyName Family name + * @param companyName Company name + */ + public void updateNameData (final char gender, final String surname, final String familyName, final String companyName); } diff --git a/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java b/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java index b86e2d9..4cf38b1 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java @@ -32,15 +32,17 @@ public class UserContact extends BookContact implements Contact { * @param gender Gender to be set * @param surname Surname to be set * @param familyName Family name to be set + * @param companyName Company name * @todo Add validation of data */ - public UserContact (final char gender, final String surname, final String familyName) { + public UserContact (final char gender, final String surname, final String familyName, final String companyName) { // Make sure all constructors are called this(); this.setGender(gender); this.setSurname(surname); this.setFamilyName(familyName); + this.setCompanyName(companyName); } /** diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index e4e842c..7dca9da 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -19,6 +19,7 @@ package org.mxchange.addressbook.manager.contact; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.client.Client; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.contact.user.UserContact; @@ -92,20 +93,45 @@ public class ContactManager extends BaseManager implements ManageableContact { * Let the user change address data * * @param contact Instance to change data + * @param client Client instance to call back */ @Override - public void changeAddressData (final Contact contact) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + public void changeAddressData (final Contact contact, final Client client) { + throw new UnsupportedOperationException("Not supported yet."); } /** * Let the user change "name data" * * @param contact Instance to change data + * @param client Client instance to call back */ @Override - public void changeNameData (final Contact contact) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + public void changeNameData (final Contact contact, final Client client) { + // First display them again + client.displayNameBox(contact); + + // Is this own data? + if (contact.isOwnContact()) { + // Re-ask own data + // Gender: + char gender = this.enterOwnGender(); + + // Surname + String surname = this.enterOwnSurname(); + + // Family name + String familyName = this.enterOwnFamilyName(); + + // And company + String companyName = this.enterCompanyName(); + + // Update contact instance + contact.updateNameData(gender, surname, familyName, companyName); + } else { + // Then re-ask them ... + throw new UnsupportedOperationException("Changing contact entries not finished."); + } } /** @@ -120,10 +146,11 @@ public class ContactManager extends BaseManager implements ManageableContact { * Let the user change other data * * @param contact Instance to change data + * @param client Client instance to call back */ @Override - public void changeOtherData (final Contact contact) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + public void changeOtherData (final Contact contact, final Client client) { + throw new UnsupportedOperationException("Not supported yet."); } /** @@ -155,7 +182,7 @@ public class ContactManager extends BaseManager implements ManageableContact { try { // Ask user what to change this.getClient().doUserChangeAdressChoice(contact); - } catch (final Exception ex) { + } catch (final UnhandledUserChoiceException ex) { this.getLogger().catching(ex); } } @@ -182,8 +209,11 @@ public class ContactManager extends BaseManager implements ManageableContact { // And 3rd for family name String familyName = this.enterOwnFamilyName(); + // Company name ... + String companyName = this.enterCompanyName(); + // Construct UserContact instance - Contact contact = new UserContact(gender, surname, familyName); + Contact contact = new UserContact(gender, surname, familyName, companyName); // Mark contact as own contact.enableFlagOwnContact(); @@ -202,12 +232,20 @@ public class ContactManager extends BaseManager implements ManageableContact { return this.contacts.size(); } + /** + * Asks the user for his/her company name + * @return + */ + private String enterCompanyName () { + return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true); + } + /** * Asks the user for family name * @return Family name of the user */ private String enterOwnFamilyName () { - return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: "); + return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false); } /** @@ -223,7 +261,7 @@ public class ContactManager extends BaseManager implements ManageableContact { * @return Surname of the user */ private String enterOwnSurname () { - return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: "); + return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false); } /** diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java index ac0db41..1d41304 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java @@ -16,6 +16,7 @@ */ package org.mxchange.addressbook.manager.contact; +import org.mxchange.addressbook.client.Client; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.manager.Manageable; @@ -42,16 +43,18 @@ public interface ManageableContact extends Manageable { * of given Contact instance. * * @param contact Instance to change data + * @param client Client instance to call back */ - public void changeAddressData (final Contact contact); + public void changeAddressData (final Contact contact, final Client client); /** * The user can change name data, like gender, surname, family name and * company name (if business contact). * * @param contact Instance to change data + * @param client Client instance to call back */ - public void changeNameData (final Contact contact); + public void changeNameData (final Contact contact, final Client client); /** * Let the user change other address @@ -62,8 +65,9 @@ public interface ManageableContact extends Manageable { * The user can change other data, like phone numbers or comments. * * @param contact Instance to change data + * @param client Client instance to call back */ - public void changeOtherData (final Contact contact); + public void changeOtherData (final Contact contact, final Client client); /** * Let the user change own data