2 * Copyright (C) 2015 Roland Haeder
\r
4 * This program is free software: you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation, either version 3 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
17 package org.mxchange.addressbook.client.console;
\r
19 import java.util.Arrays;
\r
20 import java.util.HashMap;
\r
21 import java.util.Map;
\r
22 import java.util.Scanner;
\r
23 import org.mxchange.addressbook.UnhandledUserChoiceException;
\r
24 import org.mxchange.addressbook.application.AddressbookApplication;
\r
25 import org.mxchange.addressbook.application.Application;
\r
26 import org.mxchange.addressbook.client.BaseClient;
\r
27 import org.mxchange.addressbook.client.Client;
\r
28 import org.mxchange.addressbook.contact.Contact;
\r
29 import org.mxchange.addressbook.menu.Menu;
\r
30 import org.mxchange.addressbook.menu.MenuTools;
\r
31 import org.mxchange.addressbook.menu.console.ConsoleMenu;
\r
32 import org.mxchange.addressbook.menu.item.SelectableMenuItem;
\r
33 import org.mxchange.addressbook.menu.item.console.ConsoleMenuItem;
\r
36 * A client for the console
\r
38 * @author Roland Haeder
\r
40 public class ConsoleClient extends BaseClient implements Client {
\r
44 private final Map<String, Menu> menus;
\r
47 * Scanner instance for reading data from console input
\r
49 private final Scanner scanner;
\r
52 * Parameterless constructor
\r
53 * @param application An instance of an Application class
\r
55 public ConsoleClient (final Application application) {
\r
58 // Set application instance
\r
59 this.setApplication(application);
\r
61 // Init contact manager here
\r
62 this.initContactManager(this);
\r
64 // Init scanner instance
\r
65 this.scanner = new Scanner(System.in);
\r
68 this.menus = new HashMap<>(10);
\r
71 this.fillConsoleMenuMap();
\r
75 * Displays a textual address "box" of given contact
\r
77 * @param contact Contact to show address for
\r
81 public void displayAddressBox (final Contact contact) {
\r
82 // Simple display ...
\r
84 this.displayMessage("Strasse, PLZ Ort, Land: " + contact.getStreet() + "\n" + contact.getZipCode() + " " + contact.getCity() + "\n" + contact.getCountryCode());
\r
88 * Displays textural message to the user
\r
92 public void displayMessage (final String message) {
\r
93 System.out.println(message);
\r
97 * Displays a textual name "box" of given contact
\r
99 * @param contact Contact to show name for
\r
102 public void displayNameBox (final Contact contact) {
\r
103 // Get translated gender as the user may want to see "Mr.", "Mrs."
\r
104 String gender = contact.getTranslatedGender();
\r
106 // Get company name
\r
107 String companyName = contact.getCompanyName();
\r
109 // If it is empty/null, then assume private contact
\r
110 if ((companyName == null) || (companyName.isEmpty())) {
\r
111 // Now put all together: gender, surname, family name
\r
113 this.displayMessage("Anrede, Vorname, Name: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName());
\r
116 this.displayMessage("Firma: " + companyName + "\nAnsprechpartner: " + gender + " " + contact.getSurname() + " " + contact.getFamilyName());
\r
121 * Displays a textual other data "box" of given contact
\r
123 * @param contact Contact to show other data for
\r
126 public void displayOtherDataBox (final Contact contact) {
\r
127 // Cellphone and such ...
\r
128 this.displayMessage("Telefonnumer: " + contact.getPhoneNumber() + "\nFaxnummer: " + contact.getFaxNumber() + "\nHandy: " + contact.getCellphoneNumber() + "\nKommentar:\n" + contact.getComment());
\r
132 public void doUserChangeAdressChoice (final Contact contact) throws UnhandledUserChoiceException {
\r
133 // Ask the user for editing [name], [a]ddress or [other] data
\r
134 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) ");
\r
136 // @TODO Get rid of this ugly switch block, too
\r
138 case 'n': // Name data
\r
139 this.getContactManager().changeNameData(contact);
\r
142 case 'a': // Address data
\r
143 this.getContactManager().changeAddressData(contact);
\r
146 case 'o': // Other data
\r
147 this.getContactManager().changeOtherData(contact);
\r
150 case 'x': // Exit this menu
\r
151 // Ignored as it should go back
\r
155 // @TODO throw own exception
\r
156 throw new UnhandledUserChoiceException("Choice '" + choice + "' not handled yet.");
\r
161 public void doUserMenuChoice () throws UnhandledUserChoiceException {
\r
162 // Get all access keys from menu
\r
163 char[] accessKeys = MenuTools.getAccessKeysFromMenuMap(this.menus, this.getCurrentMenu());
\r
165 // Output textural message and ask for a char as input
\r
166 char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Programm beenden): ");
\r
168 // @TODO Rewrite this ugly switch() block
\r
170 case '1': // Enter/add own data
\r
171 this.getContactManager().enterOwnData();
\r
174 case '2': // Change own data
\r
175 this.getContactManager().changeOwnData();
\r
178 case '3': // Add new addess
\r
179 this.getContactManager().addOtherAddress();
\r
182 case '4': // Change other addess
\r
183 this.getContactManager().changeOtherAddress();
\r
186 case '5': // Delete other address
\r
187 this.getContactManager().deleteOtherAddress();
\r
190 case '0': // Program exit
\r
191 this.disableIsRunning();
\r
195 // @TODO throw own exception
\r
196 throw new UnhandledUserChoiceException("Choice '" + choice + "' not handled yet.");
\r
201 * Asks the the user to enter a single character which must match validChars
\r
202 * @param validChars Valid chars that are accepted
\r
203 * @param message Message to user
\r
204 * @return Allowed character
\r
207 public char enterChar (final char[] validChars, final String message) {
\r
210 // Sort array, else binarySearch() won't work
\r
211 Arrays.sort(validChars);
\r
213 // Keep asking until valid char has been entered
\r
214 while (Arrays.binarySearch(validChars, input) < 0) {
\r
216 System.out.print(message);
\r
219 input = this.readChar();
\r
222 // Return read char
\r
227 * Reads a string of minimum and maximum length from the user
\r
228 * @param minLength Minimum length of the string to read
\r
229 * @param maxLength Maximum length of the string to read
\r
230 * @param message Message to user
\r
234 public String enterString (final int minLength, final int maxLength, final String message) {
\r
236 String input = null;
\r
238 // Check if it is to short or to long
\r
239 while ((input == null) || (input.length() < minLength) || (input.length() > maxLength)) {
\r
241 System.out.print(message);
\r
244 input = this.readString();
\r
252 * Returns a console menu item
\r
254 * @param accessKey Key to access the menu
\r
255 * @param text Text to show to user
\r
256 * @return A SelectableMenuItem
\r
257 * @todo Make sure the access key is unique
\r
260 public SelectableMenuItem getMenuItem (final char accessKey, final String text) {
\r
261 // Return a new console menu item
\r
262 return new ConsoleMenuItem(accessKey,text);
\r
266 * Shows textural menu on console
\r
269 public void showCurrentMenu () {
\r
270 this.showMenu(this.getCurrentMenu());
\r
274 public void showEntry (final SelectableMenuItem item) {
\r
275 // Access key then text
\r
276 this.displayMessage("[" + item.getAccessKey() + "] " + item.getText());
\r
280 * Shows a textural message to the user
\r
283 public void showWelcome () {
\r
284 this.displayMessage("Welcome to " + AddressbookApplication.APP_TITLE + " v" + AddressbookApplication.APP_VERSION);
\r
285 this.displayMessage("");
\r
286 this.displayMessage("Copyright(c) 2015 by Roland Haeder, this is free software");
\r
289 this.getLogger().debug("Intro shown to user");
\r
293 * Fills menu map with menu entries
\r
295 private void fillConsoleMenuMap () {
\r
296 // Initialize first (main) menu
\r
297 Menu menu = new ConsoleMenu("main", this);
\r
300 this.menus.put("main", menu);
\r
304 * "Getter" for given menu type
\r
306 * @param menuType Menu type instance to return
\r
307 * @return Menu or null if not found
\r
309 private Menu getMenu (final String menuType) {
\r
310 // Default is not found
\r
314 if (this.menus.containsKey(menuType)) {
\r
316 menu = this.menus.get(menuType);
\r
324 * Reads one character
\r
327 private char readChar () {
\r
329 String input = this.scanner.nextLine();
\r
331 // This must be only one character
\r
332 if (input.length() != 1) {
\r
337 // Get char from first (and only) position
\r
338 return input.charAt(0);
\r
342 * Reads a string from a scanner until RETURN is pressed
\r
344 * @return Read string from scanner
\r
346 private String readString () {
\r
347 return this.scanner.nextLine();
\r
353 * @param menuType Given menu to show
\r
355 private void showMenu (final String menuType) {
\r
356 Menu menu = this.getMenu(menuType);
\r
358 // Is the menu set?
\r
359 if (!(menu instanceof Menu)) {
\r
361 // @todo Own exception?
\r
362 throw new NullPointerException("Menu '" + menuType + "' not found.");
\r