]> git.mxchange.org Git - jaddressbook-lib.git/blobdiff - Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java
Fixes for missing throws statements (because jcore has changed).
[jaddressbook-lib.git] / Addressbook / src / org / mxchange / addressbook / client / console / ConsoleClient.java
index bbefa9e05969d8e0faba7ba3c0ca2ea817fd778e..176418b4f38849891d7cf7936bf4a40a467dff13 100644 (file)
  */
 package org.mxchange.addressbook.client.console;
 
+import java.sql.SQLException;
 import java.text.MessageFormat;
 import java.util.Arrays;
 import java.util.Scanner;
 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.contact.Gender;
+import org.mxchange.addressbook.client.AddressbookClient;
+import org.mxchange.addressbook.client.BaseAddressbookClient;
 import org.mxchange.addressbook.contact.user.UserContact;
-import org.mxchange.addressbook.exceptions.UnhandledUserChoiceException;
+import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
+import org.mxchange.addressbook.manager.contact.ManageableAddressbookContact;
 import org.mxchange.addressbook.menu.Menu;
 import org.mxchange.addressbook.menu.MenuTools;
 import org.mxchange.addressbook.menu.console.ConsoleMenu;
 import org.mxchange.addressbook.menu.item.SelectableMenuItem;
 import org.mxchange.addressbook.menu.item.console.ConsoleMenuItem;
+import org.mxchange.jcore.application.Application;
+import org.mxchange.jcore.contact.Contact;
+import org.mxchange.jcore.contact.Gender;
+import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
+import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
 
 /**
  * A client for the console
  *
  * @author Roland Haeder
  */
-public class ConsoleClient extends BaseClient implements Client {
+public class ConsoleClient extends BaseAddressbookClient implements AddressbookClient {
 
        /**
         * Scanner instance for reading data from console input
@@ -58,7 +62,7 @@ public class ConsoleClient extends BaseClient implements Client {
                this.setApplication(application);
 
                // Init scanner instance
-               this.scanner = new Scanner(System.in);
+               this.scanner = new Scanner(System.in, "UTF-8"); //NOI18N
 
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
@@ -72,12 +76,12 @@ public class ConsoleClient extends BaseClient implements Client {
        @Override
        public void displayAddressBox (final Contact contact) {
                // Trace message
-               this.getLogger().trace("contact=" + contact + " - CALLED!"); //NOI18N
+               this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
 
                // Is it null?
                if (contact == null) {
                        // Abort here
-                       throw new NullPointerException("contact is null");
+                       throw new NullPointerException("contact is null"); //NOI18N
                }
 
                // Simple display ...
@@ -100,7 +104,7 @@ public class ConsoleClient extends BaseClient implements Client {
                // Is it null?
                if (contact == null) {
                        // Abort here
-                       throw new NullPointerException("contact is null");
+                       throw new NullPointerException("contact is null"); //NOI18N
                }
 
                // Get translated gender as the user may want to see "Mr.", "Mrs."
@@ -136,7 +140,7 @@ public class ConsoleClient extends BaseClient implements Client {
                // Is it null?
                if (contact == null) {
                        // Abort here
-                       throw new NullPointerException("contact is null");
+                       throw new NullPointerException("contact is null"); //NOI18N
                }
 
                // Cellphone and such ...
@@ -154,29 +158,35 @@ public class ConsoleClient extends BaseClient implements Client {
                // Is it null?
                if (contact == null) {
                        // Abort here
-                       throw new NullPointerException("contact is null");
+                       throw new NullPointerException("contact is null"); //NOI18N
                }
 
                // Make sure it is own contact
                if (!contact.isOwnContact()) {
                        // Not own contact
-                       throw new IllegalArgumentException("Contact is not own data.");
+                       throw new IllegalArgumentException("Contact is not own data."); //NOI18N
                }
 
-               // Own address data
-               String street = this.getContactManager().enterOwnStreet();
+               // Get manager and cast it
+               ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager();
+
+               // Own street and number
+               String streetNumber = manager.enterOwnStreet();
 
                // Get zip code
-               int zipCode = this.getContactManager().enterOwnZipCode();
+               Long zipCode = (long) manager.enterOwnZipCode();
 
                // Get city name
-               String city = this.getContactManager().enterOwnCity();
+               String city = manager.enterOwnCity();
 
                // Get country code
-               String countryCode = this.getContactManager().enterOwnCountryCode();
+               String countryCode = manager.enterOwnCountryCode();
 
                // Update address data
-               contact.updateAddressData(street, zipCode, city, countryCode);
+               contact.setStreet(streetNumber);
+               contact.setZipCode(zipCode);
+               contact.setCity(city);
+               contact.setCountryCode(countryCode);
 
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
@@ -190,29 +200,35 @@ public class ConsoleClient extends BaseClient implements Client {
                // Is it null?
                if (contact == null) {
                        // Abort here
-                       throw new NullPointerException("contact is null");
+                       throw new NullPointerException("contact is null"); //NOI18N
                }
 
                // Make sure it is own contact
                if (!contact.isOwnContact()) {
                        // Not own contact
-                       throw new IllegalArgumentException("Contact is not own data.");
+                       throw new IllegalArgumentException("Contact is not own data."); //NOI18N
                }
 
+               // Get manager and cast it
+               ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager();
+
                // Gender:
-               Gender gender = this.getContactManager().enterOwnGender();
+               Gender gender = manager.enterOwnGender();
 
                // Surname
-               String surname = this.getContactManager().enterOwnSurname();
+               String surname = manager.enterOwnSurname();
 
                // Family name
-               String familyName = this.getContactManager().enterOwnFamilyName();
+               String familyName = manager.enterOwnFamilyName();
 
                // And company
-               String companyName = this.getContactManager().enterOwnCompanyName();
+               String companyName = manager.enterOwnCompanyName();
 
                // Update contact instance
-               contact.updateNameData(gender, surname, familyName, companyName);
+               contact.setGender(gender);
+               contact.setSurname(surname);
+               contact.setFamilyName(familyName);
+               contact.setCompanyName(companyName);
 
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
@@ -226,32 +242,39 @@ public class ConsoleClient extends BaseClient implements Client {
                // Is it null?
                if (contact == null) {
                        // Abort here
-                       throw new NullPointerException("contact is null");
+                       throw new NullPointerException("contact is null"); //NOI18N
                }
 
                // Make sure it is own contact
                if (!contact.isOwnContact()) {
                        // Not own contact
-                       throw new IllegalArgumentException("Contact is not own data.");
+                       throw new IllegalArgumentException("Contact is not own data."); //NOI18N
                }
 
+               // Get manager and cast it
+               ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager();
+
                // Phone number
-               String phoneNumber = this.getContactManager().enterOwnPhoneNumber();
+               String phoneNumber = manager.enterOwnPhoneNumber();
 
                // Phone number
-               String cellNumber = this.getContactManager().enterOwnCellNumber();
+               String cellphonePhoneNumber = manager.enterOwnCellNumber();
 
                // Fax number
-               String faxNumber = this.getContactManager().enterOwnFaxNumber();
+               String faxNumber = manager.enterOwnFaxNumber();
 
                // Email address
-               String email = this.getContactManager().enterOwnEmailAddress();
+               String email = manager.enterOwnEmailAddress();
 
                // Comment
-               String comment = this.getContactManager().enterOwnComment();
+               String comment = manager.enterOwnComment();
 
                // Update contact instance
-               contact.updateOtherData(phoneNumber, cellNumber, faxNumber, email, null, comment);
+               contact.setPhoneNumber(phoneNumber);
+               contact.setCellphoneNumber(cellphonePhoneNumber);
+               contact.setFaxNumber(faxNumber);
+               contact.setEmailAddress(email);
+               contact.setComment(comment);
 
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
@@ -262,17 +285,20 @@ public class ConsoleClient extends BaseClient implements Client {
                // Trace message
                this.getLogger().trace("CALLED!"); //NOI18N
 
+               // Get manager and cast it
+               ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager();
+
                // First ask for gender
-               Gender gender = this.getContactManager().enterOwnGender();
+               Gender gender = manager.enterOwnGender();
 
                // 2nd for surname
-               String surname = this.getContactManager().enterOwnSurname();
+               String surname = manager.enterOwnSurname();
 
                // And 3rd for family name
-               String familyName = this.getContactManager().enterOwnFamilyName();
+               String familyName = manager.enterOwnFamilyName();
 
                // Company name ...
-               String companyName = this.getContactManager().enterOwnCompanyName();
+               String companyName = manager.enterOwnCompanyName();
 
                // Construct UserContact instance
                Contact contact = new UserContact(gender, surname, familyName, companyName);
@@ -312,34 +338,43 @@ public class ConsoleClient extends BaseClient implements Client {
                // Output textural message and ask for a char as input
                char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Programm beenden): ");
 
+               // Get manager and cast it
+               ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager();
+
                // @TODO Rewrite this ugly switch() block
                switch (choice) {
-                       case '1': // Enter/add own data
-                               this.getContactManager().doEnterOwnData();
+                       case '1':
+                               try {
+                                       // Enter/add own data
+                                       manager.doEnterOwnData();
+                               } catch (final ContactAlreadyAddedException ex) {
+                                       // Already added
+                                       this.outputMessage("Sie haben bereits Ihre eigenen Daten eingegeben.");
+                               }
                                break;
 
                        case '2': // Change own data
-                               this.getContactManager().doChangeOwnData();
+                               manager.doChangeOwnData();
                                break;
 
                        case '3': // Add new addess
-                               this.getContactManager().doAddOtherAddress();
+                               manager.doAddOtherAddress();
                                break;
 
                        case '4': // List contacts
-                               this.getContactManager().doListContacts();
+                               manager.doListContacts();
                                break;
 
                        case '5': // Search addresses
-                               this.getContactManager().doSearchContacts();
+                               manager.doSearchContacts();
                                break;
 
                        case '6': // Change other addess
-                               this.getContactManager().doChangeOtherAddress();
+                               manager.doChangeOtherAddress();
                                break;
 
                        case '7': // Delete other address
-                               this.getContactManager().doDeleteOtherAddress();
+                               manager.doDeleteOtherAddress();
                                break;
 
                        case '0': // Program exit
@@ -348,7 +383,7 @@ public class ConsoleClient extends BaseClient implements Client {
 
                        default:
                                // @TODO throw own exception
-                               throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice));
+                               throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); //NOI18N
                }
 
                // Trace message
@@ -370,10 +405,10 @@ public class ConsoleClient extends BaseClient implements Client {
                // The validChars must not null be null and filled with at least one char
                if (validChars == null) {
                        // Is null
-                       throw new NullPointerException("validChars is null");
+                       throw new NullPointerException("validChars is null"); //NOI18N
                } else if (validChars.length == 0) {
                        // Is not filled
-                       throw new IllegalArgumentException("validChars is not filled.");
+                       throw new IllegalArgumentException("validChars is not filled."); //NOI18N
                }
 
                char input = 0;
@@ -420,10 +455,10 @@ public class ConsoleClient extends BaseClient implements Client {
                Gender g = Gender.fromChar(gender);
 
                // g must not be null
-               assert(g instanceof Gender) : "g is not set.";
+               assert(g instanceof Gender) : "g is not set."; //NOI18N
 
                // Trace message
-               this.getLogger().trace("g=" + g + " - EXIT!"); //NOI18N
+               this.getLogger().trace(MessageFormat.format("g={0} - EXIT!", g)); //NOI18N
 
                // Return it
                return g;
@@ -458,7 +493,7 @@ public class ConsoleClient extends BaseClient implements Client {
                }
 
                // Trace message
-               this.getLogger().trace("input=" + input + " - EXIT!"); //NOI18N
+               this.getLogger().trace(MessageFormat.format("input={0} - EXIT!", input)); //NOI18N
 
                // Return it
                return input;
@@ -515,7 +550,7 @@ public class ConsoleClient extends BaseClient implements Client {
        }
 
        /**
-        * Inizializes this client
+        * Initializes this client
         */
        @Override
        public void init () {
@@ -523,7 +558,12 @@ public class ConsoleClient extends BaseClient implements Client {
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Init contact manager here
-               this.initContactManager();
+               try {
+                       this.initContactManager();
+               } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
+                       // End here
+                       this.abortProgramWithException(ex);
+               }
 
                // Fill menu map
                this.fillMenuMap();
@@ -558,7 +598,7 @@ public class ConsoleClient extends BaseClient implements Client {
        @Override
        public void showEntry (final SelectableMenuItem item) {
                // Access key then text
-               this.outputMessage("[" + item.getAccessKey() + "] " + item.getText());
+               this.outputMessage(MessageFormat.format("[{0}] {1}", item.getAccessKey(), item.getText()));
        }
 
        /**
@@ -571,7 +611,7 @@ public class ConsoleClient extends BaseClient implements Client {
                this.outputMessage("Copyright(c) 2015 by Roland Haeder, this is free software");
 
                // Debug message
-               this.getLogger().debug("Intro shown to user");
+               this.getLogger().debug("Intro shown to user"); //NOI18N
        }
 
        @Override
@@ -582,24 +622,27 @@ public class ConsoleClient extends BaseClient implements Client {
                // Contact must not be null
                if (contact == null) {
                        // Abort here
-                       throw new NullPointerException("contact is null");
+                       throw new NullPointerException("contact is null"); //NOI18N
                }
 
                // 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) ");
 
+               // Get manager and cast it
+               ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager();
+
                // @TODO Get rid of this ugly switch block, too
                switch (choice) {
                        case 'n': // Name data
-                               this.getContactManager().doChangeNameData(contact);
+                               manager.doChangeNameData(contact);
                                break;
 
                        case 'a': // Address data
-                               this.getContactManager().doChangeAddressData(contact);
+                               manager.doChangeAddressData(contact);
                                break;
 
                        case 'o': // Other data
-                               this.getContactManager().doChangeOtherData(contact);
+                               manager.doChangeOtherData(contact);
                                break;
 
                        case 'x': // Exit this menu
@@ -608,7 +651,7 @@ public class ConsoleClient extends BaseClient implements Client {
 
                        default:
                                // @TODO throw own exception
-                               throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice));
+                               throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); //NOI18N
                }
 
                // Trace message
@@ -624,6 +667,9 @@ public class ConsoleClient extends BaseClient implements Client {
                // Read line
                String input = this.readString();
 
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("input={0}", input)); //NOI18N
+
                // This must be only one character
                if (input.length() != 1) {
                        // Return zero
@@ -643,6 +689,9 @@ public class ConsoleClient extends BaseClient implements Client {
                // First read a string
                String input = this.readString();
 
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("input={0}", input)); //NOI18N
+
                // Init number with invalid value
                int num = -1;
 
@@ -651,9 +700,12 @@ public class ConsoleClient extends BaseClient implements Client {
                        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()));
+                       this.getLogger().warn(MessageFormat.format("No numbers-only entered. input={0},message={1}", input, e.getMessage())); //NOI18N
                }
 
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("num={0} - EXIT!", num)); //NOI18N
+
                // Return read number
                return num;
        }
@@ -676,10 +728,10 @@ public class ConsoleClient extends BaseClient implements Client {
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Initialize first (main) menu
-               Menu menu = new ConsoleMenu("main", this);
+               Menu menu = new ConsoleMenu("main", this); //NOI18N
 
                // Add it
-               this.getMenus().put("main", menu);
+               this.getMenus().put("main", menu); //NOI18N
 
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N