From 3457fa799a3756c45072acd0da5ed82de5535aa0 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 15 Jul 2015 14:02:13 +0200 Subject: [PATCH] =?utf8?q?Introduced=20updateNameData()=20+=20changing=20o?= =?utf8?q?wn=20"name=20data"=20is=20basicly=20finished=20Signed-off-by:Rol?= =?utf8?q?and=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../mxchange/addressbook/client/Client.java | 5 +- .../client/console/ConsoleClient.java | 13 +++-- .../addressbook/contact/BaseContact.java | 15 +++++ .../mxchange/addressbook/contact/Contact.java | 9 +++ .../addressbook/contact/user/UserContact.java | 4 +- .../manager/contact/ContactManager.java | 58 +++++++++++++++---- .../manager/contact/ManageableContact.java | 10 +++- 7 files changed, 92 insertions(+), 22 deletions(-) diff --git a/Addressbook/src/org/mxchange/addressbook/client/Client.java b/Addressbook/src/org/mxchange/addressbook/client/Client.java index 2c4336c5..eeb66b73 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 36c6faab..a67de855 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 95335ff7..7bf18b09 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 1ea7d578..ee52c3c6 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 b86e2d9e..4cf38b14 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 e4e842ce..7dca9da4 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 ac0db41e..1d41304d 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 -- 2.39.5