X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=Addressbook%2Fsrc%2Forg%2Fmxchange%2Faddressbook%2Fclient%2Fconsole%2FConsoleClient.java;h=4803fee54a1594f19e6483198a3f897b08a2741a;hb=e06df9f02fa3fc03c21d5b2c3e2c8cb476be4ea3;hp=36c6faab4aab073962a392f34009c2299d7b6b5f;hpb=d43d6638fbacdd05fc605609a24de099c0864cae;p=jfinancials-swing.git diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index 36c6faa..4803fee 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -16,9 +16,8 @@ */ 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; @@ -38,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 */ @@ -58,17 +52,8 @@ 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); - - // Fill menu map - this.fillConsoleMenuMap(); } /** @@ -80,17 +65,8 @@ public class ConsoleClient extends BaseClient implements Client { @Override 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); + // @todo Use mask + this.outputMessage("Strasse, PLZ Ort, Land: " + contact.getStreet() + "\n" + contact.getZipCode() + " " + contact.getCity() + "\n" + contact.getCountryCode()); } /** @@ -110,10 +86,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,42 +101,24 @@ 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(MessageFormat.format("Telefonnumer: {0}\nFaxnummer: {1}\nHandy: {2}\nKommentar:\n{3}", contact.getPhoneNumber(), contact.getFaxNumber(), contact.getCellphoneNumber(), contact.getComment())); } + /** + * Shutdown this client + */ @Override - public void doUserChangeAdressChoice (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) "); - - // @TODO Get rid of this ugly switch block, too - switch (choice) { - case 'n': // Name data - this.getContactManager().changeNameData(contact); - break; - - case 'a': // Address data - this.getContactManager().changeAddressData(contact); - break; - - case 'o': // Other data - this.getContactManager().changeOtherData(contact); - break; + public void doShutdown () { + // Parent call + super.doShutdown(); - 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."); - } + // @TODO Add other shutdown stuff } @Override 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=Programm beenden): "); @@ -168,7 +126,7 @@ public class ConsoleClient extends BaseClient implements Client { // @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 @@ -179,21 +137,29 @@ 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; case '0': // Program exit - this.disableIsRunning(); + this.getApplication().doShutdown(); break; default: // @TODO throw own exception - throw new UnhandledUserChoiceException("Choice '" + choice + "' not handled yet."); + throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); } } @@ -223,20 +189,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); @@ -262,6 +262,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 */ @@ -270,10 +291,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()); } /** @@ -281,48 +307,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.printableTitle()); + 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 @@ -338,6 +363,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 * @@ -348,21 +397,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); } }