X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=Addressbook%2Fsrc%2Forg%2Fmxchange%2Faddressbook%2Fclient%2Fconsole%2FConsoleClient.java;h=82d80d824e86ceebc5353cf7cda31952d2467662;hb=27f135983830bf9fc71b909db6c0fb97c618fccc;hp=364c8db55a9a2bb3f44391945c4e06718f167a34;hpb=c3a6502c11c40b41c5c20988dd7344526d583b14;p=jaddressbook-share-lib.git diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index 364c8db..82d80d8 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -16,14 +16,15 @@ */ package org.mxchange.addressbook.client.console; +import java.text.MessageFormat; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; import java.util.Scanner; +import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.application.AddressbookApplication; import org.mxchange.addressbook.application.Application; import org.mxchange.addressbook.client.BaseClient; import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.menu.Menu; import org.mxchange.addressbook.menu.MenuTools; import org.mxchange.addressbook.menu.console.ConsoleMenu; @@ -36,11 +37,6 @@ import org.mxchange.addressbook.menu.item.console.ConsoleMenuItem; * @author Roland Haeder */ public class ConsoleClient extends BaseClient implements Client { - /** - * Menu system - */ - private final Map menus; - /** * Scanner instance for reading data from console input */ @@ -56,40 +52,70 @@ public class ConsoleClient extends BaseClient implements Client { // Set application instance this.setApplication(application); - // Init contact manager here - this.initContactManager(this); - // Init scanner instance this.scanner = new Scanner(System.in); + } - // Init menu map - this.menus = new HashMap<>(10); + /** + * Displays a textual address "box" of given contact + * + * @param contact Contact to show address for + * @todo Use mask + */ + @Override + public void displayAddressBox (final Contact contact) { + // Simple display ... + // @todo Use mask + this.outputMessage("Strasse, PLZ Ort, Land: " + contact.getStreet() + "\n" + contact.getZipCode() + " " + contact.getCity() + "\n" + contact.getCountryCode()); + } - // Fill menu map - this.fillConsoleMenuMap(); + /** + * Displays a textual name "box" of given contact + * + * @param contact Contact to show name for + */ + @Override + public void displayNameBox (final Contact contact) { + // Get translated gender as the user may want to see "Mr.", "Mrs." + String gender = contact.getTranslatedGender(); + + // Get company name + String companyName = contact.getCompanyName(); + + // If it is empty/null, then assume private contact + if ((companyName == null) || (companyName.isEmpty())) { + // Now put all together: gender, surname, family name + // @todo Use mask + this.outputMessage("Anrede, Vorname, Name: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName()); + } else { + // Company contact + this.outputMessage("Firma: " + companyName + "\nAnsprechpartner: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName()); + } } /** - * Displays textural message to the user - * @param message + * Displays a textual other data "box" of given contact + * + * @param contact Contact to show other data for */ @Override - public void displayMessage (final String message) { - System.out.println(message); + public void displayOtherDataBox (final Contact contact) { + // Cellphone and such ... + this.outputMessage("Telefonnumer: " + contact.getPhoneNumber() + "\nFaxnummer: " + contact.getFaxNumber() + "\nHandy: " + contact.getCellphoneNumber() + "\nKommentar:\n" + contact.getComment()); } @Override - public void doUserChoice () throws Exception { + public void doUserMenuChoice () throws UnhandledUserChoiceException { // Get all access keys from menu - char[] accessKeys = MenuTools.getAccessKeysFromMenuMap(this.menus, this.getCurrentMenu()); + char[] accessKeys = MenuTools.getAccessKeysFromMenuMap(this.getMenus(), this.getCurrentMenu()); // Output textural message and ask for a char as input - char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Beenden/Zurück in's vorherhige Menü): "); + char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Programm beenden): "); // @TODO Rewrite this ugly switch() block switch (choice) { case '1': // Enter/add own data - this.getContactManager().enterOwnData(); + this.getContactManager().doEnterOwnData(); break; case '2': // Change own data @@ -100,11 +126,19 @@ public class ConsoleClient extends BaseClient implements Client { this.getContactManager().addOtherAddress(); break; - case '4': // Change other addess + case '4': // List contacts + this.getContactManager().listContacts(); + break; + + case '5': // Search addresses + this.getContactManager().searchContacts(); + break; + + case '6': // Change other addess this.getContactManager().changeOtherAddress(); break; - case '5': // Delete other address + case '7': // Delete other address this.getContactManager().deleteOtherAddress(); break; @@ -113,8 +147,8 @@ public class ConsoleClient extends BaseClient implements Client { break; default: - // @TODO throw on exception - throw new Exception("choice " + choice + " invalid"); + // @TODO throw own exception + throw new UnhandledUserChoiceException("Choice '" + choice + "' not handled yet."); } } @@ -144,20 +178,54 @@ 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 - * @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) { + // Check on length, e.g. country codes are excactly 2 chars long + assert(maxLength >= minLength); + // 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); @@ -183,6 +251,27 @@ public class ConsoleClient extends BaseClient implements Client { return new ConsoleMenuItem(accessKey,text); } + /** + * Inizializes this client + */ + @Override + public void initClient () { + // Init contact manager here + this.initContactManager(); + + // Fill menu map + this.fillMenuMap(); + } + + /** + * Displays textural message to the user + * @param message + */ + @Override + public void outputMessage (final String message) { + System.out.println(message); + } + /** * Shows textural menu on console */ @@ -191,10 +280,15 @@ public class ConsoleClient extends BaseClient implements Client { 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.displayMessage("[" + item.getAccessKey() + "] " + item.getText()); + this.outputMessage("[" + item.getAccessKey() + "] " + item.getText()); } /** @@ -202,48 +296,47 @@ 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"); - - // Debug message - this.getLogger().debug("Intro shown to user"); - } - - /** - * Fills menu map with menu entries - */ - private void fillConsoleMenuMap () { - // Initialize first (main) menu - Menu menu = new ConsoleMenu("main", this); - - // Add it - this.menus.put("main", menu); + 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"); } - /** - * "Getter" for given menu type - * - * @param menuType Menu type instance to return - * @return Menu or null if not found - */ - private Menu getMenu (final String menuType) { - // Default is not found - Menu menu = null; - - // Check array - if (this.menus.containsKey(menuType)) { - // Found! - menu = this.menus.get(menuType); - } + @Override + public void userChooseChangeContactData (final Contact contact) throws UnhandledUserChoiceException { + // Ask the user for editing [name], [a]ddress or [other] data + 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) "); - // Return it - return menu; + // @TODO Get rid of this ugly switch block, too + switch (choice) { + case 'n': // Name data + this.getContactManager().doChangeNameData(contact, this); + break; + + case 'a': // Address data + this.getContactManager().doChangeAddressData(contact, this); + break; + + case 'o': // Other data + this.getContactManager().doChangeOtherData(contact, this); + break; + + case 'x': // Exit this menu + // Ignored as it should go back + break; + + default: + // @TODO throw own exception + throw new UnhandledUserChoiceException("Choice '" + choice + "' not handled yet."); + } } /** * Reads one character - * @return + * + * @return A single character */ private char readChar () { // Read line @@ -259,6 +352,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 * @@ -269,21 +386,14 @@ public class ConsoleClient extends BaseClient implements Client { } /** - * Shows given menu - * - * @param menuType Given menu to show + * Fills menu map with menu entries */ - private void showMenu (final String menuType) { - Menu menu = this.getMenu(menuType); - - // Is the menu set? - if (!(menu instanceof Menu)) { - // Not found - // @todo Own exception? - throw new NullPointerException("Menu '" + menuType + "' not found."); - } + @Override + protected final void fillMenuMap () { + // Initialize first (main) menu + Menu menu = new ConsoleMenu("main", this); - // Show menu - menu.show(this); + // Add it + this.getMenus().put("main", menu); } }