X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fmxchange%2Faddressbook%2Fclient%2Fconsole%2FConsoleClient.java;h=408ebf0107a5bb45ed9fc5f3787c64ab81f5757e;hb=6728dbd7598e463076ac0c433ee43022b19c32f1;hp=6ed15fb2a04a79feee994d412fdfc53bbc388a1c;hpb=f2010ed4e24bb3e08ee7884184ee23f35193200d;p=addressbook-lib.git diff --git a/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/src/org/mxchange/addressbook/client/console/ConsoleClient.java index 6ed15fb..408ebf0 100644 --- a/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -17,6 +17,7 @@ package org.mxchange.addressbook.client.console; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.text.MessageFormat; import java.util.Arrays; @@ -24,24 +25,23 @@ import java.util.Scanner; import org.mxchange.addressbook.application.AddressbookApplication; import org.mxchange.addressbook.client.AddressbookClient; import org.mxchange.addressbook.client.BaseAddressbookClient; -import org.mxchange.addressbook.contact.user.UserContact; import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException; -import org.mxchange.addressbook.manager.contact.ManageableAddressbookContact; +import org.mxchange.addressbook.manager.contact.ManageableContactAddressbook; import org.mxchange.addressbook.menu.Menu; import org.mxchange.addressbook.menu.MenuTools; import org.mxchange.addressbook.menu.console.ConsoleMenu; import org.mxchange.addressbook.menu.item.SelectableMenuItem; import org.mxchange.addressbook.menu.item.console.ConsoleMenuItem; import org.mxchange.jcore.application.Application; -import org.mxchange.jcore.contact.Contact; -import org.mxchange.jcore.contact.Gender; -import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.UnhandledUserChoiceException; -import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; +import org.mxchange.jcore.model.contact.Contact; +import org.mxchange.jcore.model.contact.UserContact; +import org.mxchange.jcore.model.contact.gender.Gender; +import org.mxchange.jcore.model.contact.gender.GenderUtils; /** * A client for the console - * + *

* @author Roland Haeder */ public class ConsoleClient extends BaseAddressbookClient implements AddressbookClient { @@ -53,7 +53,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC /** * Parameterless constructor - * + *

* @param application An instance of an Application class */ public ConsoleClient (final Application application) { @@ -70,18 +70,13 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace("EXIT!"); //NOI18N } - /** - * Displays a textual address "box" of given contact - * - * @param contact Contact to show address for - */ @Override public void displayAddressBox (final Contact contact) { // Trace message this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N // Is it null? - if (contact == null) { + if (null == contact) { // Abort here throw new NullPointerException("contact is null"); //NOI18N } @@ -93,54 +88,44 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace("EXIT!"); //NOI18N } - /** - * Displays a textual name "box" of given contact - * - * @param contact Contact to show name for - */ @Override public void displayNameBox (final Contact contact) { // Trace message this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N // Is it null? - if (contact == null) { + if (null == contact) { // Abort here throw new NullPointerException("contact is null"); //NOI18N } // Get translated gender as the user may want to see "Mr.", "Mrs." - String gender = contact.getTranslatedGender(); + String gender = GenderUtils.getTranslatedGender(contact); // Get company name String companyName = contact.getCompanyName(); // If it is empty/null, then assume private contact - if ((companyName == null) || (companyName.isEmpty())) { + if ((null == companyName) || (companyName.isEmpty())) { // Now put all together: gender, surname, family name - // @todo Use mask - this.outputMessage(MessageFormat.format("Anrede, Vorname, Name: {0} {1} {2}", gender, contact.getSurname(), contact.getFamilyName())); + // TODO Use mask + this.outputMessage(MessageFormat.format("Anrede, Vorname, Name: {0} {1} {2}", gender, contact.getFirstName(), contact.getFamilyName())); } else { // Company contact - this.outputMessage(MessageFormat.format("Firma: {0}\nAnsprechpartner: {1} {2} {3}", companyName, gender, contact.getSurname(), contact.getFamilyName())); + this.outputMessage(MessageFormat.format("Firma: {0}\nAnsprechpartner: {1} {2} {3}", companyName, gender, contact.getFirstName(), contact.getFamilyName())); } // Trace message this.getLogger().trace("EXIT!"); //NOI18N } - /** - * Displays a textual other data "box" of given contact - * - * @param contact Contact to show other data for - */ @Override public void displayOtherDataBox (final Contact contact) { // Trace message this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N // Is it null? - if (contact == null) { + if (null == contact) { // Abort here throw new NullPointerException("contact is null"); //NOI18N } @@ -158,7 +143,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N // Is it null? - if (contact == null) { + if (null == contact) { // Abort here throw new NullPointerException("contact is null"); //NOI18N } @@ -170,7 +155,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC } // Get manager and cast it - ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager(); + ManageableContactAddressbook manager = (ManageableContactAddressbook) this.getManager(); // Own street and number String streetNumber = manager.enterOwnStreet(); @@ -200,7 +185,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N // Is it null? - if (contact == null) { + if (null == contact) { // Abort here throw new NullPointerException("contact is null"); //NOI18N } @@ -212,13 +197,13 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC } // Get manager and cast it - ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager(); + ManageableContactAddressbook manager = (ManageableContactAddressbook) this.getManager(); // Gender: Gender gender = manager.enterOwnGender(); // Surname - String surname = manager.enterOwnSurname(); + String firstName = manager.enterOwnFirstName(); // Family name String familyName = manager.enterOwnFamilyName(); @@ -228,7 +213,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC // Update contact instance contact.setGender(gender); - contact.setSurname(surname); + contact.setFirstName(firstName); contact.setFamilyName(familyName); contact.setCompanyName(companyName); @@ -242,7 +227,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N // Is it null? - if (contact == null) { + if (null == contact) { // Abort here throw new NullPointerException("contact is null"); //NOI18N } @@ -254,7 +239,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC } // Get manager and cast it - ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager(); + ManageableContactAddressbook manager = (ManageableContactAddressbook) this.getManager(); // Phone number String phoneNumber = manager.enterOwnPhoneNumber(); @@ -288,13 +273,13 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace("CALLED!"); //NOI18N // Get manager and cast it - ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager(); + ManageableContactAddressbook manager = (ManageableContactAddressbook) this.getManager(); // First ask for gender Gender gender = manager.enterOwnGender(); - // 2nd for surname - String surname = manager.enterOwnSurname(); + // 2nd for first name + String firstName = manager.enterOwnFirstName(); // And 3rd for family name String familyName = manager.enterOwnFamilyName(); @@ -303,7 +288,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC String companyName = manager.enterOwnCompanyName(); // Construct UserContact instance - Contact contact = new UserContact(gender, surname, familyName, companyName); + Contact contact = new UserContact(gender, firstName, familyName, companyName); // Trace message this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N @@ -312,9 +297,6 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC return contact; } - /** - * Shutdown this client - */ @Override public void doShutdown () throws SQLException, IOException { // Trace message @@ -323,8 +305,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC // Parent call super.doShutdown(); - // @TODO Add other shutdown stuff - + // TODO Add other shutdown stuff // Trace message this.getLogger().trace("EXIT!"); //NOI18N } @@ -341,11 +322,11 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Programm beenden): "); // Get manager and cast it - ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager(); + ManageableContactAddressbook manager = (ManageableContactAddressbook) this.getManager(); // Try it! try { - // @TODO Rewrite this ugly switch() block + // TODO Rewrite this ugly switch() block switch (choice) { case '1': try { @@ -381,21 +362,20 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC manager.doDeleteOtherAddress(); break; - case '0': { - try { - // Program exit - this.getApplication().doShutdown(); - } catch (final SQLException | IOException ex) { - this.abortProgramWithException(ex); - } - } - break; + case '0': + try { + // Program exit + this.getApplication().doShutdown(); + } catch (final SQLException | IOException ex) { + this.abortProgramWithException(ex); + } + break; default: - // @TODO throw own exception + // TODO throw own exception throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); //NOI18N } - } catch (final IOException | BadTokenException ex) { + } catch (final IOException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { // Something bad happened this.abortProgramWithException(ex); } @@ -404,20 +384,13 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace("EXIT!"); //NOI18N } - /** - * 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 - */ @Override public char enterChar (final char[] validChars, final String message) { // Trace message this.getLogger().trace(MessageFormat.format("validChars={0},message={1} - CALLED!", Arrays.toString(validChars), message)); //NOI18N // The validChars must not null be null and filled with at least one char - if (validChars == null) { + if (null == validChars) { // Is null throw new NullPointerException("validChars is null"); //NOI18N } else if (validChars.length == 0) { @@ -446,12 +419,6 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC return input; } - /** - * Asks the user to enter his/her gender - * - * @param message Message to the user - * @return Gender enum - */ @Override public Gender enterGender (final String message) { // Trace message @@ -469,7 +436,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC Gender g = Gender.fromChar(gender); // g must not be null - assert(g instanceof Gender) : "g is not set."; //NOI18N + assert (g instanceof Gender) : "g is not set."; //NOI18N // Trace message this.getLogger().trace(MessageFormat.format("g={0} - EXIT!", g)); //NOI18N @@ -478,22 +445,14 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC return g; } - /** - * 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) { // Trace message this.getLogger().trace(MessageFormat.format("minimum={0},maximum={1},message={2} - CALLED!", minimum, maximum, message)); //NOI18N // Minimum should not be below zero - assert (minimum >= 0); - assert (maximum > minimum); + assert (minimum >= 0) : MessageFormat.format("minimum={0} is below zero", minimum); //NOI18N + assert (maximum > minimum) : MessageFormat.format("maximum {0} is smaller than minimum {1}", maximum, minimum); //NOI18N // Init input int input = -1; @@ -513,15 +472,6 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC 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 - * @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, final boolean allowEmpty) { // Trace message @@ -534,7 +484,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC String input = null; // Check if it is to short or to long - while (((input == null) || ((input.length() < minLength) && (!allowEmpty))) || ((input.length() > 0) && (input.length() < minLength) && (allowEmpty)) || ((input instanceof String) && (input.length() > maxLength))) { + while (((null == input) || ((input.length() < minLength) && (!allowEmpty))) || ((input.length() > 0) && (input.length() < minLength) && (allowEmpty)) || ((input instanceof String) && (input.length() > maxLength))) { // Output message System.out.print(message); @@ -549,23 +499,12 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC return input; } - /** - * Returns a console menu item - * - * @param accessKey Key to access the menu - * @param text Text to show to user - * @return A SelectableMenuItem - * @todo Make sure the access key is unique - */ @Override public SelectableMenuItem getMenuItem (final char accessKey, final String text) { // Return a new console menu item return new ConsoleMenuItem(accessKey, text); } - /** - * Initializes this client - */ @Override public void init () { // Trace message @@ -574,7 +513,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC // Init contact manager here try { this.initContactManager(); - } catch (final UnsupportedDatabaseBackendException | SQLException ex) { + } catch (final SQLException ex) { // End here this.abortProgramWithException(ex); } @@ -586,43 +525,27 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace("EXIT!"); //NOI18N } - /** - * Displays textural message to the user - * - * @param message - */ @Override public void outputMessage (final String message) { System.out.println(message); } - /** - * Shows textural menu on console - */ @Override public void showCurrentMenu () { this.showMenu(this.getCurrentMenu()); } - /** - * Shows given menu entry to user - * - * @param item Menu entry - */ @Override public void showEntry (final SelectableMenuItem item) { // Access key then text - this.outputMessage(MessageFormat.format("[{0}] {1}", item.getAccessKey(), item.getText())); + this.outputMessage(MessageFormat.format("[{0}] {1}", item.getAccessKey(), item.getText())); //NOI18N } - /** - * Shows a textural message to the user - */ @Override public void showWelcome () { - this.outputMessage(MessageFormat.format("Welcome to {0}", AddressbookApplication.printableTitle())); - this.outputMessage(""); - this.outputMessage("Copyright(c) 2015 by Roland Haeder, this is free software"); + this.outputMessage(MessageFormat.format("Welcome to {0}", AddressbookApplication.printableTitle())); //NOI18N + this.outputMessage(""); //NOI18N + this.outputMessage("Copyright(c) 2015 by Roland Haeder, this is free software"); //NOI18N // Debug message this.getLogger().debug("Intro shown to user"); //NOI18N @@ -634,7 +557,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N // Contact must not be null - if (contact == null) { + if (null == contact) { // Abort here throw new NullPointerException("contact is null"); //NOI18N } @@ -643,9 +566,9 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC char choice = this.enterChar(new char[] {'n', 'a', 'o', 'x'}, "Welchen Daten möchten Sie ändern? (n=Namensdaten, a=Anschriftsdaten, o=Andere, x=Zurück zur Hauptauswahl) "); // Get manager and cast it - ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager(); + ManageableContactAddressbook manager = (ManageableContactAddressbook) this.getManager(); - // @TODO Get rid of this ugly switch block, too + // TODO Get rid of this ugly switch block, too switch (choice) { case 'n': // Name data manager.doChangeNameData(contact); @@ -664,7 +587,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC break; default: - // @TODO throw own exception + // TODO throw own exception throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); //NOI18N } @@ -674,7 +597,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC /** * Reads one character - * + *

* @return A single character */ private char readChar () { @@ -696,7 +619,7 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC /** * Reads an integer (int) from user - * + *

* @return An integer number */ private int readInt () { @@ -726,16 +649,13 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC /** * Reads a string from a scanner until RETURN is pressed - * + *

* @return Read string from scanner */ private String readString () { return this.scanner.nextLine(); } - /** - * Fills menu map with menu entries - */ @Override protected final void fillMenuMap () { // Trace message