From c3a6502c11c40b41c5c20988dd7344526d583b14 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 15 Jul 2015 08:31:34 +0200 Subject: [PATCH] =?utf8?q?Initial=20commit=20Signed-off-by:Roland=20H?= =?utf8?q?=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Addressbook/build.xml | 73 +++ Addressbook/src/log4j2.xml | 29 ++ .../addressbook/BaseFrameworkSystem.java | 140 ++++++ .../addressbook/FrameworkInterface.java | 32 ++ .../application/AddressbookApplication.java | 204 +++++++++ .../addressbook/application/Application.java | 35 ++ .../addressbook/client/BaseClient.java | 76 ++++ .../mxchange/addressbook/client/Client.java | 99 ++++ .../client/console/ConsoleClient.java | 289 ++++++++++++ .../addressbook/contact/BaseContact.java | 422 ++++++++++++++++++ .../mxchange/addressbook/contact/Contact.java | 144 ++++++ .../addressbook/contact/book/BookContact.java | 31 ++ .../addressbook/contact/user/UserContact.java | 52 +++ .../addressbook/manager/BaseManager.java | 36 ++ .../addressbook/manager/Manageable.java | 26 ++ .../application/ApplicationManager.java | 60 +++ .../application/ManageableApplication.java | 30 ++ .../manager/contact/ContactManager.java | 289 ++++++++++++ .../manager/contact/ManageableContact.java | 66 +++ .../addressbook/menu/AddressbookMenu.java | 79 ++++ .../org/mxchange/addressbook/menu/Menu.java | 47 ++ .../mxchange/addressbook/menu/MenuTools.java | 78 ++++ .../addressbook/menu/console/BaseMenu.java | 95 ++++ .../addressbook/menu/console/ConsoleMenu.java | 35 ++ .../addressbook/menu/item/BaseMenuItem.java | 27 ++ .../menu/item/SelectableMenuItem.java | 44 ++ .../menu/item/console/ConsoleMenuItem.java | 89 ++++ 27 files changed, 2627 insertions(+) create mode 100644 Addressbook/build.xml create mode 100644 Addressbook/src/log4j2.xml create mode 100644 Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java create mode 100644 Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java create mode 100644 Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java create mode 100644 Addressbook/src/org/mxchange/addressbook/application/Application.java create mode 100644 Addressbook/src/org/mxchange/addressbook/client/BaseClient.java create mode 100644 Addressbook/src/org/mxchange/addressbook/client/Client.java create mode 100644 Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java create mode 100644 Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java create mode 100644 Addressbook/src/org/mxchange/addressbook/contact/Contact.java create mode 100644 Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java create mode 100644 Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java create mode 100644 Addressbook/src/org/mxchange/addressbook/manager/BaseManager.java create mode 100644 Addressbook/src/org/mxchange/addressbook/manager/Manageable.java create mode 100644 Addressbook/src/org/mxchange/addressbook/manager/application/ApplicationManager.java create mode 100644 Addressbook/src/org/mxchange/addressbook/manager/application/ManageableApplication.java create mode 100644 Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java create mode 100644 Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java create mode 100644 Addressbook/src/org/mxchange/addressbook/menu/AddressbookMenu.java create mode 100644 Addressbook/src/org/mxchange/addressbook/menu/Menu.java create mode 100644 Addressbook/src/org/mxchange/addressbook/menu/MenuTools.java create mode 100644 Addressbook/src/org/mxchange/addressbook/menu/console/BaseMenu.java create mode 100644 Addressbook/src/org/mxchange/addressbook/menu/console/ConsoleMenu.java create mode 100644 Addressbook/src/org/mxchange/addressbook/menu/item/BaseMenuItem.java create mode 100644 Addressbook/src/org/mxchange/addressbook/menu/item/SelectableMenuItem.java create mode 100644 Addressbook/src/org/mxchange/addressbook/menu/item/console/ConsoleMenuItem.java diff --git a/Addressbook/build.xml b/Addressbook/build.xml new file mode 100644 index 0000000..366ade1 --- /dev/null +++ b/Addressbook/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Addressbook. + + + diff --git a/Addressbook/src/log4j2.xml b/Addressbook/src/log4j2.xml new file mode 100644 index 0000000..d3ade86 --- /dev/null +++ b/Addressbook/src/log4j2.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + diff --git a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java new file mode 100644 index 0000000..093797a --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.mxchange.addressbook.application.Application; +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.manager.contact.ContactManager; +import org.mxchange.addressbook.manager.contact.ManageableContact; + +/** + * General class + * + * @author Roland Haeder + */ +public class BaseFrameworkSystem { + /** + * Class' logger + */ + private final Logger LOG; + + /** + * Application instance + */ + private Application application; + + /** + * Client instance + */ + private Client client; + + /** + * Contact manager instance + */ + private ManageableContact contactManager; + + /** + * Initialize object + */ + { + LOG = LogManager.getLogger(this); + } + + /** + * No instances can be created of this class + */ + protected BaseFrameworkSystem () { + } + + /** + * Application instance + * @return the application + */ + public Application getApplication () { + return this.application; + } + + /** + * Application instance + * @param application the application to set + */ + protected void setApplication (final Application application) { + this.application = application; + } + + /** + * Client instance + * @return the client + */ + public Client getClient () { + return this.client; + } + + /** + * Client instance + * @param client the client to set + */ + protected void setClient (final Client client) { + this.client = client; + } + + /** + * Contact manager instance + * @return the contactManager + */ + public ManageableContact getContactManager () { + return this.contactManager; + } + + /** + * Contact manager instance + * @param contactManager the contactManager to set + */ + protected void setContactManager(final ManageableContact contactManager) { + this.contactManager = contactManager; + } + + /** + * Getter for logger + * + * @return Logger + */ + protected Logger getLogger () { + return this.LOG; + } + + /** + * Initializes contact manager + * @param client Client instance + */ + protected void initContactManager (final Client client) { + // Debug message + this.getLogger().debug("Initializing contact manager ..."); + + // Init contact manager with console client + // @TODO Static initial amount of contacts + ManageableContact manager = new ContactManager (100, client); + + // Set it here + this.setContactManager(manager); + + // Debug message + this.getLogger().debug("Contact manager has been initialized."); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java b/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java new file mode 100644 index 0000000..5bc5d24 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2015 KLC + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook; + +import org.mxchange.addressbook.manager.contact.ManageableContact; + +/** + * + * @author KLC + */ +public interface FrameworkInterface { + + /** + * Getter for contact manager + * @return Contact manager instance + */ + public ManageableContact getContactManager (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java new file mode 100644 index 0000000..7799aca --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java @@ -0,0 +1,204 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.application; + +import org.mxchange.addressbook.BaseFrameworkSystem; +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.client.console.ConsoleClient; +import org.mxchange.addressbook.manager.application.ApplicationManager; + +/** + * ============================================ + * AddressbookApplication management: + * ============================================ + * + * Inernet("public" service) and Intranet + * + * Version 1.0+: + * - Single-user local application + * - Fields: + * + Gender + * + Surname + * + Family name + * + Company name + * + Street + number + * + ZIP code + * + City + * + Landline number + * + Fax number + * + Cell phone number + * + Email address + * + Birth day + * + Comment (?) + * - Edit own data + * - Add new contact + * - Edit contacts + * - Delete contacts + * - Categorization of contacts + * + * Version 1.1+: + * - Permanent storage in database + * + * Version 2.0+: + * - Multi-user web application + * - Local user registration / login / resend confirmation link / password + * recovery + * - User groups (aka. teams) + * - Administration area (user role) + * + Create/edit/delete groups + * + Edit/delete/lock/unlock user + * + Assign user roles/rights + * - Allow other users / groups to view addressbook + * + Full addressbook + * + Only some categories + * - VCard export + * + Allow users/guests (not recommended) + * - XML export of addressbook and compressable (ZIP) + * - Form to contact other user/group without need of mail program + * + User can disabled this + * - Directory for ussers/groups (who allowed to be listed) + * + Simple click to add user to own addressbook + * + Search form? + * + * Version 2.1+: + * - Multi-language support + * + * Version 2.2+:("socialized") + * - "Social login" (OpenID consumer) + * + Connect user account to social account + * + Sync own data? + * - "Social profile" + * + OpenID provider + * + RSS/activity feed + * + * ============================================ + * Time esitmation: + * ============================================ + * 1.0 (console): + * + 2 days + * + * 1.1 (database): + * + 2 day + * + Initial tables: contacts, categories, contact_category + * + * 2.0 (web): + * + 3 days + * + Additional tables: admins (?), admin_rights, groups, + * users, user_contacts, user_user_rights, user_category_rights, + * + * 2.1 (language) + * + 1 day + * + Additional tables: languages (disable, enable language "pack" ?) + * + * 2.2 (social): + * + 3 days + * + Additional tables: ??? + * + * @author Roland Haeder + * @version 0.0 + * @since 0.0 + */ +public class AddressbookApplication extends BaseFrameworkSystem implements Application { + /** + * Application title + */ + public static final String APP_TITLE = "Addressbuch"; + + /** + * Application version + */ + public static final String APP_VERSION = "0.0"; + + /** + * Main method (entry point) + * + * @param args the command line arguments + */ + public static void main (String[] args) { + // Start application + new AddressbookApplication().start (); + } + + /** + * Bootstraps application + */ + @Override + public void doBootstrap () { + this.getLogger().debug("Initializing application ..."); + + // Init client instance + Client client = new ConsoleClient(this); + + // Init client instance + this.setClient(client); + + // The application is running at this point + this.getClient().enableIsRunning(); + } + + /** + * Main loop of the application + */ + @Override + public void doMainLoop () { + // @TODO The application should be running now + + // Output introduction + this.showIntro(); + + // Set current menu to main + this.getClient().setCurrentMenu("main"); + + // --- Main loop starts here --- + while (this.getClient().isApplicationRunning()) { + // The application is still active, show menu selection + this.getClient().showCurrentMenu(); + + try { + // Ask for user input and run proper method + this.getClient().doUserChoice(); + } catch (final Exception ex) { + this.getLogger().catching(ex); + } + } + // --- Main loop ends here --- + + // Debug message + this.getLogger().debug("Main loop exit - shutting down ..."); + } + + /** + * Show introduction which depends on client + */ + private void showIntro () { + // Let the client show it + this.getClient().showWelcome(); + } + + /** + * Launches the application + */ + private void start () { + this.getLogger().info("Program is started."); + + // Launch application + ApplicationManager.getManager(this).start (); + + this.getLogger().info("End of program (last line)"); + } + +} diff --git a/Addressbook/src/org/mxchange/addressbook/application/Application.java b/Addressbook/src/org/mxchange/addressbook/application/Application.java new file mode 100644 index 0000000..67241ad --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/application/Application.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.application; + +import org.mxchange.addressbook.FrameworkInterface; + +/** + * + * @author Roland Haeder + */ +public interface Application extends FrameworkInterface { + /** + * Bootstraps the application + */ + public void doBootstrap (); + + /** + * Run the main loop + */ + public void doMainLoop (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java new file mode 100644 index 0000000..09c7b0f --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.client; + +import org.mxchange.addressbook.BaseFrameworkSystem; + +/** + * A general client + * + * @author Roland Haeder + */ +public class BaseClient extends BaseFrameworkSystem { + + /** + * Current menu choice + */ + private String currentMenu; + + /** + * Application is not running by default + */ + private boolean isRunning; + + /** + * No instances can be created of this class + */ + protected BaseClient () { + super(); + } + + /** + * Disables running state, so the main loop can abort. + */ + public void disableIsRunning () { + this.isRunning = false; + } + + public void enableIsRunning () { + this.isRunning = true; + } + + /** + * Current menu choice + * @return the currentMenu + */ + public String getCurrentMenu () { + return this.currentMenu; + } + + /** + * Current menu choice + * @param currentMenu the currentMenu to set + */ + public void setCurrentMenu (final String currentMenu) { + this.currentMenu = currentMenu; + } + + public boolean isApplicationRunning () { + // In console client, 0 may have been used + return this.isRunning; + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/client/Client.java b/Addressbook/src/org/mxchange/addressbook/client/Client.java new file mode 100644 index 0000000..740a366 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/client/Client.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.client; + +import org.mxchange.addressbook.FrameworkInterface; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; + +/** + * + * @author Roland Haeder + */ +public interface Client extends FrameworkInterface { + + /** + * Displays a message to the user + * @param message Message to show to the user + */ + public void displayMessage (final String message); + + /** + * Asks the user for a choice and proceeds accordingly + * @throws java.lang.Exception + */ + public void doUserChoice () throws Exception ; + + /** + * Enables isRunning attribute which singals that the client is running + */ + public void enableIsRunning (); + + /** + * Asks the the user to enter a single character which must match validChars + * @param validChars Valid chars that are accepted + * @param message Message to user + * @return Allowed character + */ + public char enterChar (final char[] validChars, final String message); + + /** + * 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 + */ + public String enterString (final int minLength, final int maxLength, final String message); + + /** + * Setter for current menu choice + * @param currentMenu Current menu choice + */ + public void setCurrentMenu (final String currentMenu); + + /** + * Some "Getter" for menu item + * @param accessKey Key to press to access this menu + * @param text Text to show to user + * @return + */ + public SelectableMenuItem getMenuItem (final char accessKey, final String text); + + /** + * Determines whether the application is still active by checking some + * conditions + * + * @return Whether the application is still active + */ + public boolean isApplicationRunning (); + + /** + * Shows given menu entry in client + * @param item Menu item to show + */ + public void showEntry (final SelectableMenuItem item); + + /** + * Shows introduction to user + */ + public void showWelcome (); + + /** + * Shows current menu selection to the user + */ + public void showCurrentMenu (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java new file mode 100644 index 0000000..364c8db --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -0,0 +1,289 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.client.console; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +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.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; + +/** + * A client for the console + * + * @author Roland Haeder + */ +public class ConsoleClient extends BaseClient implements Client { + /** + * Menu system + */ + private final Map menus; + + /** + * Scanner instance for reading data from console input + */ + private final Scanner scanner; + + /** + * Parameterless constructor + * @param application An instance of an Application class + */ + public ConsoleClient (final Application application) { + super(); + + // 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(); + } + + /** + * Displays textural message to the user + * @param message + */ + @Override + public void displayMessage (final String message) { + System.out.println(message); + } + + @Override + public void doUserChoice () throws Exception { + // Get all access keys from menu + char[] accessKeys = MenuTools.getAccessKeysFromMenuMap(this.menus, this.getCurrentMenu()); + + // Output textural message and ask for a char as input + char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Beenden/Zurück in's vorherhige Menü): "); + + // @TODO Rewrite this ugly switch() block + switch (choice) { + case '1': // Enter/add own data + this.getContactManager().enterOwnData(); + break; + + case '2': // Change own data + this.getContactManager().changeOwnData(); + break; + + case '3': // Add new addess + this.getContactManager().addOtherAddress(); + break; + + case '4': // Change other addess + this.getContactManager().changeOtherAddress(); + break; + + case '5': // Delete other address + this.getContactManager().deleteOtherAddress(); + break; + + case '0': // Program exit + this.disableIsRunning(); + break; + + default: + // @TODO throw on exception + throw new Exception("choice " + choice + " invalid"); + } + } + + /** + * Asks the the user to enter a single character which must match validChars + * @param validChars Valid chars that are accepted + * @param message Message to user + * @return Allowed character + */ + @Override + public char enterChar (final char[] validChars, final String message) { + char input = 0; + + // Sort array, else binarySearch() won't work + Arrays.sort(validChars); + + // Keep asking until valid char has been entered + while (Arrays.binarySearch(validChars, input) < 0) { + // Output message + System.out.print(message); + + // Read char + input = this.readChar(); + } + + // Return read char + 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 + */ + @Override + public String enterString (final int minLength, final int maxLength, final String message) { + // Init input + String input = null; + + // Check if it is to short or to long + while ((input == null) || (input.length() < minLength) || (input.length() > maxLength)) { + // Output message + System.out.print(message); + + // Read line + input = this.readString(); + } + + // Return it + return input; + } + + /** + * Returns a console menu item + * + * @param accessKey Key to access the menu + * @param text Text to show to user + * @return A SelectableMenuItem + * @todo Make sure the access key is unique + */ + @Override + public SelectableMenuItem getMenuItem (final char accessKey, final String text) { + // Return a new console menu item + return new ConsoleMenuItem(accessKey,text); + } + + /** + * Shows textural menu on console + */ + @Override + public void showCurrentMenu () { + this.showMenu(this.getCurrentMenu()); + } + + @Override + public void showEntry (final SelectableMenuItem item) { + // Access key then text + this.displayMessage("[" + item.getAccessKey() + "] " + item.getText()); + } + + /** + * Shows a textural message to the user + */ + @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); + } + + /** + * "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); + } + + // Return it + return menu; + } + + /** + * Reads one character + * @return + */ + private char readChar () { + // Read line + String input = this.scanner.nextLine(); + + // This must be only one character + if (input.length() != 1) { + // Return zero + return 0; + } + + // Get char from first (and only) position + return input.charAt(0); + } + + /** + * Reads a string from a scanner until RETURN is pressed + * + * @return Read string from scanner + */ + private String readString () { + return this.scanner.nextLine(); + } + + /** + * Shows given menu + * + * @param menuType Given menu to show + */ + 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."); + } + + // Show menu + menu.show(this); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java new file mode 100644 index 0000000..44cfcce --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java @@ -0,0 +1,422 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.contact; + +import org.mxchange.addressbook.BaseFrameworkSystem; + +/** + * A general contact + * + * @author Roland Haeder + * @version 0.0 + * @since 0.0 + */ +public class BaseContact extends BaseFrameworkSystem { + /** + * Birth day + */ + private String birthday; + + /** + * Cellphone number + */ + private String cellphoneNumber; + + /** + * City + */ + private String city; + + /** + * Optional comments + */ + private String comment; + + /** + * Companyname + */ + private String companyName; + + /** + * Country code + */ + private String countryCode; + + /** + * Email address + */ + private String emailAddress; + + /** + * Family name + */ + private String familyName; + + /** + * Fax number + */ + private String faxNumber; + + /** + * Gender code of the contact: - M = Mr. (male) - F = Mrs. (female) - C = + * Company + */ + private char gender; + + /** + * House number + */ + private int houseNumber; + + /** + * Marker whether this contact is user's own data + */ + private boolean ownContact; + + /** + * Phone number + */ + private String phoneNumber; + + /** + * Street + */ + private String street; + + /** + * Surname + */ + private String surname; + + /** + * ZIP code + */ + private int zipCode; + + /** + * No instances can be created of this class + */ + protected BaseContact () { + super(); + } + + /** + * Enables the flag "own data" which signals that this contact is the user's + * own data. + */ + public void enableFlagOwnContact () { + this.ownContact = true; + } + + /** + * Check if contacts are same or throw an exception + * + * @param object Other possible contact class + * @return Whether both contacts are same + */ + @Override + public boolean equals (Object object) { + // Try to cast + BaseContact c = (BaseContact) object; + + /* + * Now test some data @todo Definedly needs improvement + */ + return ((this.getGender() == c.getGender()) + && (this.getSurname().toLowerCase().equals(c.getSurname().toLowerCase())) + && (this.getFamilyName().toLowerCase().equals(c.getFamilyName().toLowerCase()))); + } + + /** + * Birth day + * + * @return the birthday + */ + public String getBirthday () { + return this.birthday; + } + + /** + * Birth day + * + * @param birthday the birthday to set + */ + public void setBirthday (final String birthday) { + this.birthday = birthday; + } + + /** + * Cellphone number + * + * @return the cellphoneNumber + */ + public String getCellphoneNumber () { + return this.cellphoneNumber; + } + + /** + * Cellphone number + * + * @param cellphoneNumber the cellphoneNumber to set + */ + public void setCellphoneNumber (final String cellphoneNumber) { + this.cellphoneNumber = cellphoneNumber; + } + + /** + * City + * + * @return the city + */ + public String getCity () { + return this.city; + } + + /** + * City + * + * @param city the city to set + */ + public void setCity (final String city) { + this.city = city; + } + + /** + * Comments + * + * @return the comment + */ + public String getComment () { + return this.comment; + } + + /** + * Comments + * + * @param comment the comment to set + */ + public void setComment (final String comment) { + this.comment = comment; + } + + /** + * Companyname + * + * @return the companyName + */ + public String getCompanyName () { + return this.companyName; + } + + /** + * Companyname + * + * @param companyName the companyName to set + */ + public void setCompanyName (final String companyName) { + this.companyName = companyName; + } + + /** + * Country code + * + * @return the countryCode + */ + public String getCountryCode () { + return this.countryCode; + } + + /** + * Country code + * + * @param countryCode the countryCode to set + */ + public void setCountryCode (final String countryCode) { + this.countryCode = countryCode; + } + + /** + * Email address + * + * @return the emailAddress + */ + public String getEmailAddress () { + return this.emailAddress; + } + + /** + * Email address + * + * @param emailAddress the emailAddress to set + */ + public void setEmailAddress (final String emailAddress) { + this.emailAddress = emailAddress; + } + + /** + * Family name + * + * @return the familyName + */ + public String getFamilyName () { + return this.familyName; + } + + /** + * Family name + * + * @param familyName the familyName to set + */ + public void setFamilyName (final String familyName) { + this.familyName = familyName; + } + + /** + * Fax number + * + * @return the faxNumber + */ + public String getFaxNumber () { + return this.faxNumber; + } + + /** + * Fax number + * + * @param faxNumber the faxNumber to set + */ + public void setFaxNumber (final String faxNumber) { + this.faxNumber = faxNumber; + } + + /** + * Gender of the contact + * + * @return the gender + */ + public char getGender () { + return this.gender; + } + + /** + * Gender of the contact + * + * @param gender the gender to set + */ + public void setGender (final char gender) { + this.gender = gender; + } + + /** + * House number + * + * @return the houseNumber + */ + public int getHouseNumber () { + return this.houseNumber; + } + + /** + * House number + * + * @param houseNumber the houseNumber to set + */ + public void setHouseNumber (final int houseNumber) { + this.houseNumber = houseNumber; + } + + /** + * Phone number + * + * @return the phoneNumber + */ + public String getPhoneNumber () { + return this.phoneNumber; + } + + /** + * Phone number + * + * @param phoneNumber the phoneNumber to set + */ + public void setPhoneNumber (final String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + /** + * Street + * + * @return the street + */ + public String getStreet () { + return this.street; + } + + /** + * Street + * + * @param street the street to set + */ + public void setStreet (final String street) { + this.street = street; + } + + /** + * Surname + * + * @return the surname + */ + public String getSurname () { + return this.surname; + } + + /** + * Surname + * + * @param surname the surname to set + */ + public void setSurname (final String surname) { + this.surname = surname; + } + + /** + * ZIP code + * + * @return the zipCode + */ + public int getZipCode () { + return this.zipCode; + } + + /** + * ZIP code + * + * @param zipCode the zipCode to set + */ + public void setZipCode (final int zipCode) { + this.zipCode = zipCode; + } + + /** + * Checks whether the contact is user's own data + * + * @return Own data? + */ + public boolean isOwnContact () { + return this.ownContact; + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/contact/Contact.java b/Addressbook/src/org/mxchange/addressbook/contact/Contact.java new file mode 100644 index 0000000..db90506 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/contact/Contact.java @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.contact; + +import org.mxchange.addressbook.FrameworkInterface; + +/** + * + * @author Roland Haeder + */ +public interface Contact extends FrameworkInterface { + + /** + * Enables the flag "own data" which signals that this contact is the user's + * own data. + */ + public void enableFlagOwnContact (); + + /** + * Checks whether the contact is user's own data + * + * @return Own data? + */ + public boolean isOwnContact (); + + /** + * Gender of the contact + * + * @return the gender + */ + public char getGender (); + + /** + * Surname + * + * @return the surname + */ + public String getSurname (); + + /** + * Family name + * + * @return the familyName + */ + public String getFamilyName (); + + /** + * Companyname + * + * @return the companyName + */ + public String getCompanyName (); + + /** + * Street + * + * @return the street + */ + public String getStreet (); + + /** + * House number + * + * @return the houseNumber + */ + public int getHouseNumber (); + + /** + * ZIP code + * + * @return the zipCode + */ + public int getZipCode (); + + /** + * City + * + * @return the city + */ + public String getCity (); + + /** + * Country code + * + * @return the countryCode + */ + public String getCountryCode (); + + /** + * Email address + * + * @return the emailAddress + */ + public String getEmailAddress (); + + /** + * Phone number + * + * @return the phoneNumber + */ + public String getPhoneNumber (); + + /** + * Fax number + * + * @return the faxNumber + */ + public String getFaxNumber (); + + /** + * Cellphone number + * + * @return the cellphoneNumber + */ + public String getCellphoneNumber (); + + /** + * Birth day + * + * @return the birthday + */ + public String getBirthday (); + + /** + * Comments + * + * @return the comment + */ + public String getComment (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java b/Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java new file mode 100644 index 0000000..ae49d5c --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.contact.book; + +import org.mxchange.addressbook.contact.BaseContact; +import org.mxchange.addressbook.contact.Contact; + +/** + * A contact that can be placed into "contact books" + * + * @author Roland Haeder + * @version 0.0 + * @since 0.0 + */ +public class BookContact extends BaseContact implements Contact { + +} diff --git a/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java b/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java new file mode 100644 index 0000000..b86e2d9 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.contact.user; + +import org.mxchange.addressbook.contact.Contact; +import org.mxchange.addressbook.contact.book.BookContact; + +/** + * + * @author Roland Haeder + * @todo After a Collection has been used in ContactManager, change to BaseContact + */ +public class UserContact extends BookContact implements Contact { + + /** + * Creates own contact entry + * + * @param gender Gender to be set + * @param surname Surname to be set + * @param familyName Family name to be set + * @todo Add validation of data + */ + public UserContact (final char gender, final String surname, final String familyName) { + // Make sure all constructors are called + this(); + + this.setGender(gender); + this.setSurname(surname); + this.setFamilyName(familyName); + } + + /** + * No empty instances can be created of this class + */ + protected UserContact () { + super(); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/manager/BaseManager.java b/Addressbook/src/org/mxchange/addressbook/manager/BaseManager.java new file mode 100644 index 0000000..2790075 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/manager/BaseManager.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.manager; + +import org.mxchange.addressbook.BaseFrameworkSystem; + +/** + * A general manager + * + * @author Roland Haeder + * @version 0.0 + * @since 0.0 + */ +public class BaseManager extends BaseFrameworkSystem { + /** + * No instances can be created of this class + */ + protected BaseManager () { + // Call any other super constructors + super(); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/manager/Manageable.java b/Addressbook/src/org/mxchange/addressbook/manager/Manageable.java new file mode 100644 index 0000000..af3cc6c --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/manager/Manageable.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.manager; + +import org.mxchange.addressbook.FrameworkInterface; + +/** + * + * @author Roland Haeder + */ +public interface Manageable extends FrameworkInterface { +} diff --git a/Addressbook/src/org/mxchange/addressbook/manager/application/ApplicationManager.java b/Addressbook/src/org/mxchange/addressbook/manager/application/ApplicationManager.java new file mode 100644 index 0000000..9afad3a --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/manager/application/ApplicationManager.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.manager.application; + +import org.mxchange.addressbook.application.Application; +import org.mxchange.addressbook.manager.BaseManager; + +/** + * + * @author Roland Haeder + */ +public class ApplicationManager extends BaseManager implements ManageableApplication { + + /** + * Getter for application manager + * @param application An instance of a Application class + * @return + */ + public static final ManageableApplication getManager (final Application application) { + // Get manager + ManageableApplication manager = new ApplicationManager(application); + + // Return manager + return manager; + } + + /** + * Constructor for this manager + * @param application An instance of an Application class + */ + private ApplicationManager (final Application application) { + super(); + + // Set application instance + this.setApplication (application); + } + + @Override + public void start () { + // Bootstrap application + this.getApplication().doBootstrap(); + + // Run the main loop + this.getApplication().doMainLoop(); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/manager/application/ManageableApplication.java b/Addressbook/src/org/mxchange/addressbook/manager/application/ManageableApplication.java new file mode 100644 index 0000000..dec9fe1 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/manager/application/ManageableApplication.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.manager.application; + +import org.mxchange.addressbook.manager.Manageable; + +/** + * + * @author Roland Haeder + */ +public interface ManageableApplication extends Manageable { + /** + * Launches application + */ + public void start (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java new file mode 100644 index 0000000..096197b --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -0,0 +1,289 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.manager.contact; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.contact.Contact; +import org.mxchange.addressbook.contact.user.UserContact; +import org.mxchange.addressbook.manager.BaseManager; + +/** + * A manager for contacts + * + * @author Roland Haeder + * @version 0.0 + * @since 0.0 + */ +public class ContactManager extends BaseManager implements ManageableContact { + + /** + * All contacts + */ + private final List contacts; + + /** + * @param maxContacts Maximum allowed contacts + * @param client Client instance to use + */ + public ContactManager (final int maxContacts, final Client client) { + // Always call super constructor first + super(); + + // Init contacts + this.contacts = new ArrayList<>(maxContacts); + + // Debug message + //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client); + + // Init client + this.setClient(client); + } + + /** + * Adds given contact to address book + * + * @param contact Contact being added + * @todo Add check for book size + */ + @Override + public void addContact (final Contact contact) { + // Check if contact is found + if (this.isContactAlreadyAdded(contact)) { + // Contact already added + // @todo Do something here + } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) { + // Own contact already added + // @todo Do something + } + + // Debug message + /* NOISY-DEBUG: */ this.getLogger().debug("Adding '" + contact.getSurname() + "' '" + contact.getFamilyName() + "' at pos '" + this.size () + "' ..."); + + // Add contact + this.contacts.add(contact); + } + + /** + * Let the user add a new other address + */ + @Override + public void addOtherAddress () { + throw new UnsupportedOperationException("Not supported yet."); + } + + /** + * Let the user change other address + */ + @Override + public void changeOtherAddress () { + throw new UnsupportedOperationException("Not supported yet."); + } + + /** + * Allows the user to change his/her own data + */ + @Override + public void changeOwnData () { + /* + * First check if the user has registered own contact, before that + * nothing can be changed. + */ + if (!this.isOwnContactAdded()) { + // Not added + this.getClient().displayMessage("Sie haben noch nicht Ihre Daten eingegeben."); + + // Skip any below code + return; + } + + // Instance + Contact contact = this.getOwnContact(); + + // It must be found + assert(contact instanceof Contact); + + // @TODO Unfinished + throw new UnsupportedOperationException("Method is not finished."); + } + + /** + * Let the user delete other address + */ + @Override + public void deleteOtherAddress () { + throw new UnsupportedOperationException("Not supported yet."); + } + + /** + * Asks user for own data + */ + @Override + public void enterOwnData () { + // First ask for gender + char gender = this.enterOwnGender(); + + // 2nd for surname + String surname = this.enterOwnSurname(); + + // And 3rd for family name + String familyName = this.enterOwnFamilyName(); + + // Construct UserContact instance + Contact contact = new UserContact(gender, surname, familyName); + + // Mark contact as own + contact.enableFlagOwnContact(); + + // Add it to contact "book" + this.addContact(contact); + } + + /** + * Getter for size + * + * @return size of contact "book" + */ + @Override + public int size () { + return this.contacts.size(); + } + + /** + * Asks the user for family name + * @return Family name of the user + */ + private String enterOwnFamilyName () { + return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: "); + } + + /** + * Asks the user for gender, until a valid has been entered + * @return Gender of the user + */ + private char enterOwnGender () { + return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): "); + } + + /** + * Asks the user for surname + * @return Surname of the user + */ + private String enterOwnSurname () { + return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: "); + } + + /** + * "Getter" for own contact instance or null if not found + * + * @return Contact instance or null + */ + private Contact getOwnContact () { + // Now get it back from address book, first get an iterator + Iterator iterator = this.contacts.iterator(); + + // Init instance + Contact contact = null; + + // Search all contact + while (iterator.hasNext()) { + // Get next instance + Contact next = iterator.next(); + + // Is this own contact? + if (next.isOwnContact()) { + // Found it + contact = next; + break; + + } + } + + // Return instance or null + return contact; + } + + /** + * Checks whether given contact was found in "address book" + * + * @param checkContact Contact to be checked + * @return TRUE if found, FALSE if not found + */ + private boolean isContactAlreadyAdded (final Contact checkContact) throws NullPointerException { + // Default is not found + boolean isFound = false; + + // Debug message + //* NOISY-DEBUG: */ this.getLogger().debug("Checking '" + this.contacts.length + "' entries..."); + + // Now get it back from address book, first get an iterator + Iterator iterator = this.contacts.iterator(); + + // Check entries + while (iterator.hasNext()) { + // Get next entry + Contact contact = iterator.next(); + + // Debug message + //* NOISY-DEBUG: */ this.getLogger().debug("contact=" + contact + ",checkContent=" + checkContact); + + // Is it valid? + if ((contact instanceof Contact) && ((contact.equals(checkContact)))) { + // Found matching entry + isFound = true; + break; + } + } + + // Return result + return isFound; + } + + /** + * Checks whether own contact is already added by checking all entries for isOwnContact flag + * @return Whether own contact is already added + */ + private boolean isOwnContactAdded () { + // Default is not added + boolean isAdded = false; + + // Now get it back from address book, first get an iterator + Iterator iterator = this.contacts.iterator(); + + // Check entries + while (iterator.hasNext()) { + // Get next entry + Contact contact = iterator.next(); + + // Is it valid? + if (contact instanceof Contact) { + // Get flag + isAdded = contact.isOwnContact(); + + // Is this own contact? + if (isAdded) { + // Then abort loop + break; + } + } + } + // Return result + return isAdded; + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java new file mode 100644 index 0000000..03bdd8e --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.manager.contact; + +import org.mxchange.addressbook.contact.Contact; +import org.mxchange.addressbook.manager.Manageable; + +/** + * + * @author Roland Haeder + */ +public interface ManageableContact extends Manageable { + /** + * Adds given contact to address book + * + * @param contact Contact being added + * @todo Add check for book size + */ + public void addContact (final Contact contact); + + /** + * Let the user add a new other address + */ + public void addOtherAddress (); + + /** + * Let the user change other address + */ + public void changeOtherAddress (); + + /** + * Let the user change own data + */ + public void changeOwnData (); + + /** + * Let the user delete other address + */ + public void deleteOtherAddress (); + + /** + * Asks user for own data + */ + public void enterOwnData (); + + /** + * Getter for size + * + * @return size of contact "book" + */ + public int size (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/menu/AddressbookMenu.java b/Addressbook/src/org/mxchange/addressbook/menu/AddressbookMenu.java new file mode 100644 index 0000000..6282a42 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/menu/AddressbookMenu.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.menu; + +import java.util.List; +import org.apache.logging.log4j.Logger; +import org.mxchange.addressbook.BaseFrameworkSystem; +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; + +/** + * Utility class for menu structure + * + * @author Roland Haeder + */ +public class AddressbookMenu extends BaseFrameworkSystem { + /** + * Copies entries for given type into the menu list + * + * @param menuList Menu list for later showing + * @param menuType Type of menu + * @param client Client instance to call back + */ + public static void addItemsToList (final List menuList, final String menuType, final Client client) { + // Get logger + Logger log = new AddressbookMenu().getLogger(); + + // Get list size + int size = menuList.size(); + + // Debug message + log.debug("Adding menu for '" + menuType + "' (old size: '" + size + "') ..."); + + // Depends on type + switch (menuType) { + case "main": // Main menu + // Enter own data + menuList.add(client.getMenuItem('1', "Eigene Adresse anlegen")); + + // Change own data + menuList.add(client.getMenuItem('2', "Eigene Adresse ändern")); + + // Add new addess + menuList.add(client.getMenuItem('3', "Neue Adresse hinzufügen")); + + // Change other address + menuList.add(client.getMenuItem('4', "Adresse ändern")); + + // Delete other address + menuList.add(client.getMenuItem('5', "Adresse löschen")); + + // Always last line: Exit program + menuList.add(client.getMenuItem('0', "Programm verlassen")); + break; + + default: // Not supported + log.error("Menu type '" + menuType + "' ont supported"); + System.exit(1); + } + + // Size must have changed to more entries than before + assert(menuList.size() > size); + } + +} diff --git a/Addressbook/src/org/mxchange/addressbook/menu/Menu.java b/Addressbook/src/org/mxchange/addressbook/menu/Menu.java new file mode 100644 index 0000000..809357c --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/menu/Menu.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.menu; + +import java.util.Iterator; +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; + +/** + * + * @author Roland Haeder + * @todo find better name + */ +public interface Menu { + + /** + * "Getter" for an iterator on all menu items of the current menu + * @return Iterator on all menu items + */ + public Iterator getMenuItemsIterator (); + + /** + * Shows this menu + * @param client Client instance + */ + public void show (final Client client); + + /** + * Size of all menu items + * @return + */ + public int getMenuItemsCount (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/menu/MenuTools.java b/Addressbook/src/org/mxchange/addressbook/menu/MenuTools.java new file mode 100644 index 0000000..75bfb7e --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/menu/MenuTools.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.menu; + +import java.util.Iterator; +import java.util.Map; +import org.apache.logging.log4j.Logger; +import org.mxchange.addressbook.BaseFrameworkSystem; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; + +/** + * + * @author Roland Haeder + */ +public class MenuTools extends BaseFrameworkSystem { + + /** + * Gets an array with all available access keys back from given menu map. + * This can later be handle to the client's enterChar() method. + * + * @param menus A Map with all menus and their entries + * @param menuType Menu type + * @return An array with available access chars + */ + public static char[] getAccessKeysFromMenuMap (final Map menus, final String menuType) { + // Get logger + Logger logger = new MenuTools().getLogger(); + + // First search for the proper menu class + Menu menu = menus.get(menuType); + + // Is it there? + if (!(menu instanceof Menu)) { + // Not found + // @todo Rewrite to exception + logger.error("Menu '" + menuType + "' not found."); + System.exit(1); + } + + // Get iterator + Iterator iterator = menu.getMenuItemsIterator(); + + // Init return array and counter 'i' + char[] accessKeys = new char[menu.getMenuItemsCount()]; + int i = 0; + + // Now "walk" through all menu entries + while (iterator.hasNext()) { + // Get item + SelectableMenuItem item = iterator.next(); + //* NOISY-DEBUG: */ logger.debug("item=" + item); + + // Get access key from item and add it to the array + accessKeys[i] = item.getAccessKey(); + //* NOISY-DEBUG: */ logger.debug("accessKeys[" + i + "]=" + accessKeys[i]); + + // Increment counter + i++; + } + + // Return finished array + return accessKeys; + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/menu/console/BaseMenu.java b/Addressbook/src/org/mxchange/addressbook/menu/console/BaseMenu.java new file mode 100644 index 0000000..02bc5b6 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/menu/console/BaseMenu.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.menu.console; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import org.mxchange.addressbook.BaseFrameworkSystem; +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.menu.AddressbookMenu; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; + +/** + * + * @author Roland Haeder + */ +public class BaseMenu extends BaseFrameworkSystem { + /** + * Menu list + */ + private List menuList; + + /** + * No instance from this object + */ + protected BaseMenu () { + super(); + } + + /** + * "Getter" for an iterator of this menu's items + * + * @return An iterator of all menu items + */ + public Iterator getMenuItemsIterator () { + return this.menuList.iterator(); + } + + /** + * Size of menu items + * @return Count of menu items + */ + public int getMenuItemsCount () { + return this.menuList.size(); + } + + /** + * Shows this menu + * + * @param client Client instance to call back + */ + public void show (final Client client) { + // Get values + Iterator iterator = this.menuList.iterator(); + + // Debug message + this.getLogger().debug("Showing menu with '" + this.menuList.size() + "' entries."); + + // Output all menus + while (iterator.hasNext()) { + // Get item + SelectableMenuItem item = iterator.next(); + + // Show this item + item.show(client); + } + } + + /** + * Initializes menu + * @param menuType Menu type to initialize + * @param client CLient to call back + */ + protected void initMenu (final String menuType, final Client client) { + // Init menu list + this.menuList = new ArrayList<>(5); + + // Add all items + AddressbookMenu.addItemsToList(this.menuList, menuType, client); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/menu/console/ConsoleMenu.java b/Addressbook/src/org/mxchange/addressbook/menu/console/ConsoleMenu.java new file mode 100644 index 0000000..82d9f6b --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/menu/console/ConsoleMenu.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.menu.console; + +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.menu.Menu; + +/** + * + * @author Roland Haeder + */ +public class ConsoleMenu extends BaseMenu implements Menu { + /** + * Constructor for this menu + * @param menuType Menu type to initialize + * @param client CLient to call back + */ + public ConsoleMenu (final String menuType, final Client client) { + this.initMenu(menuType, client); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/menu/item/BaseMenuItem.java b/Addressbook/src/org/mxchange/addressbook/menu/item/BaseMenuItem.java new file mode 100644 index 0000000..bc1bba7 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/menu/item/BaseMenuItem.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.menu.item; + +import org.mxchange.addressbook.BaseFrameworkSystem; + +/** + * + * @author Roland Haeder + */ +public class BaseMenuItem extends BaseFrameworkSystem { + +} diff --git a/Addressbook/src/org/mxchange/addressbook/menu/item/SelectableMenuItem.java b/Addressbook/src/org/mxchange/addressbook/menu/item/SelectableMenuItem.java new file mode 100644 index 0000000..b57dfa6 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/menu/item/SelectableMenuItem.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.menu.item; + +import org.mxchange.addressbook.client.Client; + +/** + * + * @author Roland Haeder + */ +public interface SelectableMenuItem { + + /** + * Shows this menu item + * @param client Client instance + */ + public void show (final Client client); + + /** + * Access key + * @return the accessKey + */ + public char getAccessKey (); + + /** + * Text to user + * @return the text + */ + public String getText (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/menu/item/console/ConsoleMenuItem.java b/Addressbook/src/org/mxchange/addressbook/menu/item/console/ConsoleMenuItem.java new file mode 100644 index 0000000..85a784d --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/menu/item/console/ConsoleMenuItem.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.menu.item.console; + +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.menu.item.BaseMenuItem; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; + +/** + * + * @author Roland Haeder + */ +public class ConsoleMenuItem extends BaseMenuItem implements SelectableMenuItem { + /** + * Access key + */ + private char accessKey; + + /** + * Text to user + */ + private String text; + + /** + * Constructor for building a console menu with access key and text + * + * @param accessKey Access key for this menu entry + * @param text Text to show to user + */ + public ConsoleMenuItem (final char accessKey, final String text) { + this.accessKey = accessKey; + this.text = text; + } + + /** + * Access key + * @return the accessKey + */ + @Override + public char getAccessKey () { + return this.accessKey; + } + + /** + * Text to user + * @return the text + */ + @Override + public String getText () { + return this.text; + } + + @Override + public void show (final Client client) { + // Call-back client over menu + client.showEntry(this); + } + + /** + * Text to user + * @param text the text to set + */ + private void setText (String text) { + this.text = text; + } + + /** + * Access key + * @param accessKey the accessKey to set + */ + private void setAccessKey (char accessKey) { + this.accessKey = accessKey; + } + +} -- 2.39.2