2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.client.console;
19 import java.text.MessageFormat;
20 import java.util.Arrays;
21 import java.util.Scanner;
22 import org.mxchange.addressbook.application.AddressbookApplication;
23 import org.mxchange.addressbook.application.Application;
24 import org.mxchange.addressbook.client.BaseClient;
25 import org.mxchange.addressbook.client.Client;
26 import org.mxchange.addressbook.contact.Contact;
27 import org.mxchange.addressbook.contact.Gender;
28 import org.mxchange.addressbook.contact.user.UserContact;
29 import org.mxchange.addressbook.exceptions.UnhandledUserChoiceException;
30 import org.mxchange.addressbook.menu.Menu;
31 import org.mxchange.addressbook.menu.MenuTools;
32 import org.mxchange.addressbook.menu.console.ConsoleMenu;
33 import org.mxchange.addressbook.menu.item.SelectableMenuItem;
34 import org.mxchange.addressbook.menu.item.console.ConsoleMenuItem;
37 * A client for the console
39 * @author Roland Haeder
41 public class ConsoleClient extends BaseClient implements Client {
44 * Scanner instance for reading data from console input
46 private final Scanner scanner;
49 * Parameterless constructor
51 * @param application An instance of an Application class
53 public ConsoleClient (final Application application) {
56 // Set application instance
57 this.setApplication(application);
59 // Init scanner instance
60 this.scanner = new Scanner(System.in);
64 * Displays a textual address "box" of given contact
66 * @param contact Contact to show address for
69 public void displayAddressBox (final Contact contact) {
71 this.outputMessage(MessageFormat.format("Strasse, PLZ Ort, Land: {0}\n{1} {2}\n{3}", contact.getStreet(), contact.getZipCode(), contact.getCity(), contact.getCountryCode()));
75 * Displays a textual name "box" of given contact
77 * @param contact Contact to show name for
80 public void displayNameBox (final Contact contact) {
81 // Get translated gender as the user may want to see "Mr.", "Mrs."
82 String gender = contact.getTranslatedGender();
85 String companyName = contact.getCompanyName();
87 // If it is empty/null, then assume private contact
88 if ((companyName == null) || (companyName.isEmpty())) {
89 // Now put all together: gender, surname, family name
91 this.outputMessage(MessageFormat.format("Anrede, Vorname, Name: {0} {1} {2}", gender, contact.getSurname(), contact.getFamilyName()));
94 this.outputMessage(MessageFormat.format("Firma: {0}\nAnsprechpartner: {1} {2} {3}", companyName, gender, contact.getSurname(), contact.getFamilyName()));
99 * Displays a textual other data "box" of given contact
101 * @param contact Contact to show other data for
104 public void displayOtherDataBox (final Contact contact) {
105 // Cellphone and such ...
106 this.outputMessage(MessageFormat.format("Telefonnumer: {0}\nFaxnummer: {1}\nHandy: {2}\nKommentar:\n{3}", contact.getPhoneNumber(), contact.getFaxNumber(), contact.getCellphoneNumber(), contact.getComment()));
110 public void doChangeOwnAddressData (final Contact contact) {
111 // Make sure it is own contact
112 if (!contact.isOwnContact()) {
114 throw new IllegalArgumentException("Contact is not own data.");
118 String street = this.getContactManager().enterOwnStreet();
121 int zipCode = this.getContactManager().enterOwnZipCode();
124 String city = this.getContactManager().enterOwnCity();
127 String countryCode = this.getContactManager().enterOwnCountryCode();
129 // Update address data
130 contact.updateAddressData(street, zipCode, city, countryCode);
134 public void doChangeOwnNameData (final Contact contact) {
135 // Make sure it is own contact
136 if (!contact.isOwnContact()) {
138 throw new IllegalArgumentException("Contact is not own data.");
142 Gender gender = this.getContactManager().enterOwnGender();
145 String surname = this.getContactManager().enterOwnSurname();
148 String familyName = this.getContactManager().enterOwnFamilyName();
151 String companyName = this.getContactManager().enterOwnCompanyName();
153 // Update contact instance
154 contact.updateNameData(gender, surname, familyName, companyName);
158 public void doChangeOwnOtherData (final Contact contact) {
159 // Make sure it is own contact
160 if (!contact.isOwnContact()) {
162 throw new IllegalArgumentException("Contact is not own data.");
166 String phoneNumber = this.getContactManager().enterOwnPhoneNumber();
169 String cellNumber = this.getContactManager().enterOwnCellNumber();
172 String faxNumber = this.getContactManager().enterOwnFaxNumber();
175 String email = this.getContactManager().enterOwnEmailAddress();
178 String comment = this.getContactManager().enterOwnComment();
180 // Update contact instance
181 contact.updateOtherData(phoneNumber, cellNumber, faxNumber, email, null, comment);
185 public Contact doEnterOwnData () {
186 // First ask for gender
187 Gender gender = this.getContactManager().enterOwnGender();
190 String surname = this.getContactManager().enterOwnSurname();
192 // And 3rd for family name
193 String familyName = this.getContactManager().enterOwnFamilyName();
196 String companyName = this.getContactManager().enterOwnCompanyName();
198 // Construct UserContact instance
199 Contact contact = new UserContact(gender, surname, familyName, companyName);
206 * Shutdown this client
209 public void doShutdown () {
213 // @TODO Add other shutdown stuff
217 public void doUserMenuChoice () throws UnhandledUserChoiceException {
218 // Get all access keys from menu
219 char[] accessKeys = MenuTools.getAccessKeysFromMenuMap(this.getMenus(), this.getCurrentMenu());
221 // Output textural message and ask for a char as input
222 char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Programm beenden): ");
224 // @TODO Rewrite this ugly switch() block
226 case '1': // Enter/add own data
227 this.getContactManager().doEnterOwnData();
230 case '2': // Change own data
231 this.getContactManager().doChangeOwnData();
234 case '3': // Add new addess
235 this.getContactManager().doAddOtherAddress();
238 case '4': // List contacts
239 this.getContactManager().doListContacts();
242 case '5': // Search addresses
243 this.getContactManager().doSearchContacts();
246 case '6': // Change other addess
247 this.getContactManager().doChangeOtherAddress();
250 case '7': // Delete other address
251 this.getContactManager().doDeleteOtherAddress();
254 case '0': // Program exit
255 this.getApplication().doShutdown();
259 // @TODO throw own exception
260 throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice));
265 * Asks the the user to enter a single character which must match validChars
267 * @param validChars Valid chars that are accepted
268 * @param message Message to user
269 * @return Allowed character
272 public char enterChar (final char[] validChars, final String message) {
275 // Sort array, else binarySearch() won't work
276 Arrays.sort(validChars);
278 // Keep asking until valid char has been entered
279 while (Arrays.binarySearch(validChars, input) < 0) {
281 System.out.print(message);
284 input = this.readChar();
292 * Asks the user to enter his/her gender
294 * @param message Message to the user
295 * @return Gender enum
298 public Gender enterGender (final String message) {
300 char[] validChars = Gender.validChars();
303 //* NOISY-DEBUG: */ System.out.println(validChars);
305 char gender = this.enterChar(validChars, message);
307 // Now get a Gender instance back
308 Gender g = Gender.fromChar(gender);
315 * Reads an integer (int) with a textural message from the user
317 * @param minimum Minimum allowed number
318 * @param maximum Maximum allowed number
319 * @param message Messager to display in console
323 public int enterInt (final int minimum, final int maximum, final String message) {
324 // Minimum should not be below zero
325 assert (minimum >= 0);
326 assert (maximum > minimum);
331 while ((input < minimum) || (input > maximum)) {
333 System.out.print(message);
335 // Read integer from user
336 input = this.readInt();
344 * Reads a string of minimum and maximum length from the user
346 * @param minLength Minimum length of the string to read
347 * @param maxLength Maximum length of the string to read
348 * @param message Message to user
349 * @param allowEmpty Whether to allow empty string
350 * @return Entered string by user or null for empty strings
353 public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty) {
354 // Check on length, e.g. country codes are excactly 2 chars long
355 assert (maxLength >= minLength);
360 // Check if it is to short or to long
361 while (((input == null) || ((input.length() < minLength) && (!allowEmpty))) || ((input.length() > 0) && (input.length() < minLength) && (allowEmpty)) || ((input instanceof String) && (input.length() > maxLength))) {
363 System.out.print(message);
366 input = this.readString();
374 * Returns a console menu item
376 * @param accessKey Key to access the menu
377 * @param text Text to show to user
378 * @return A SelectableMenuItem
379 * @todo Make sure the access key is unique
382 public SelectableMenuItem getMenuItem (final char accessKey, final String text) {
383 // Return a new console menu item
384 return new ConsoleMenuItem(accessKey, text);
388 * Inizializes this client
391 public void init () {
392 // Init contact manager here
393 this.initContactManager();
400 * Displays textural message to the user
405 public void outputMessage (final String message) {
406 System.out.println(message);
410 * Shows textural menu on console
413 public void showCurrentMenu () {
414 this.showMenu(this.getCurrentMenu());
418 * Shows given menu entry to user
420 * @param item Menu entry
423 public void showEntry (final SelectableMenuItem item) {
424 // Access key then text
425 this.outputMessage("[" + item.getAccessKey() + "] " + item.getText());
429 * Shows a textural message to the user
432 public void showWelcome () {
433 this.outputMessage(MessageFormat.format("Welcome to {0}", AddressbookApplication.printableTitle()));
434 this.outputMessage("");
435 this.outputMessage("Copyright(c) 2015 by Roland Haeder, this is free software");
438 this.getLogger().debug("Intro shown to user");
442 public void userChooseChangeContactData (final Contact contact) throws UnhandledUserChoiceException {
443 // Ask the user for editing [name], [a]ddress or [other] data
444 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) ");
446 // @TODO Get rid of this ugly switch block, too
448 case 'n': // Name data
449 this.getContactManager().doChangeNameData(contact, this);
452 case 'a': // Address data
453 this.getContactManager().doChangeAddressData(contact, this);
456 case 'o': // Other data
457 this.getContactManager().doChangeOtherData(contact, this);
460 case 'x': // Exit this menu
461 // Ignored as it should go back
465 // @TODO throw own exception
466 throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice));
471 * Reads one character
473 * @return A single character
475 private char readChar () {
477 String input = this.readString();
479 // This must be only one character
480 if (input.length() != 1) {
485 // Get char from first (and only) position
486 return input.charAt(0);
490 * Reads an integer (int) from user
492 * @return An integer number
494 private int readInt () {
495 // First read a string
496 String input = this.readString();
498 // Init number with invalid value
501 // Parse number, this can be risky
503 num = Integer.parseInt(input);
504 } catch (final NumberFormatException e) {
505 this.outputMessage("Bitte geben Sie nur Zahlen ein!");
506 this.getLogger().warn(MessageFormat.format("No numbers-only entered. input={0},message={1}", input, e.getMessage()));
509 // Return read number
514 * Reads a string from a scanner until RETURN is pressed
516 * @return Read string from scanner
518 private String readString () {
519 return this.scanner.nextLine();
523 * Fills menu map with menu entries
526 protected final void fillMenuMap () {
527 // Initialize first (main) menu
528 Menu menu = new ConsoleMenu("main", this);
531 this.getMenus().put("main", menu);