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=532ba61c1811d5114b9281b068da34b03ee30630;hpb=a754609aa28461bbd8c95337a5260e1ebc5629df;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 532ba61..4803fee 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -18,8 +18,6 @@ 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; @@ -39,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 */ @@ -59,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(); } /** @@ -81,7 +65,7 @@ public class ConsoleClient extends BaseClient implements Client { @Override public void displayAddressBox (final Contact contact) { // Simple display ... - // @todo Use mask + // @todo Use mask this.outputMessage("Strasse, PLZ Ort, Land: " + contact.getStreet() + "\n" + contact.getZipCode() + " " + contact.getCity() + "\n" + contact.getCountryCode()); } @@ -117,42 +101,24 @@ public class ConsoleClient extends BaseClient implements Client { @Override public void displayOtherDataBox (final Contact contact) { // Cellphone and such ... - this.outputMessage("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, this); - break; - - case 'a': // Address data - this.getContactManager().changeAddressData(contact, this); - break; - - case 'o': // Other data - this.getContactManager().changeOtherData(contact, this); - break; - - case 'x': // Exit this menu - // Ignored as it should go back - break; + public void doShutdown () { + // Parent call + super.doShutdown(); - 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): "); @@ -160,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 @@ -171,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)); } } @@ -288,6 +262,18 @@ 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 @@ -305,6 +291,11 @@ 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 @@ -316,43 +307,41 @@ public class ConsoleClient extends BaseClient implements Client { */ @Override public void showWelcome () { - 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"); - } - - /** - * 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."); + } } /** @@ -408,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); } }