From d43d6638fbacdd05fc605609a24de099c0864cae Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 15 Jul 2015 13:14:33 +0200 Subject: [PATCH] =?utf8?q?Introduced=20own=20exception=20+=20added=20first?= =?utf8?q?=20sub=20menu=20for=20changing=20address=20data=20Signed-off-by:?= =?utf8?q?Roland=20H=C3=A4der=20=20Signed-off-by:Rola?= =?utf8?q?nd=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../UnhandledUserChoiceException.java | 30 +++++++++++++++ .../application/AddressbookApplication.java | 2 +- .../mxchange/addressbook/client/Client.java | 13 ++++++- .../client/console/ConsoleClient.java | 38 +++++++++++++++++-- .../manager/contact/ContactManager.java | 38 ++++++++++++++++++- .../manager/contact/ManageableContact.java | 23 +++++++++++ 6 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 Addressbook/src/org/mxchange/addressbook/UnhandledUserChoiceException.java diff --git a/Addressbook/src/org/mxchange/addressbook/UnhandledUserChoiceException.java b/Addressbook/src/org/mxchange/addressbook/UnhandledUserChoiceException.java new file mode 100644 index 0000000..e97f20a --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/UnhandledUserChoiceException.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; + +/** + * This exception is thrown when the user made a valid choice but it was not + * handled by the program. + * + * @author Roland Haeder + */ +public class UnhandledUserChoiceException extends Exception { + + public UnhandledUserChoiceException (final String message) { + super(message); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java index 7799aca..44fe468 100644 --- a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java +++ b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java @@ -170,7 +170,7 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli try { // Ask for user input and run proper method - this.getClient().doUserChoice(); + this.getClient().doUserMenuChoice(); } catch (final Exception ex) { this.getLogger().catching(ex); } diff --git a/Addressbook/src/org/mxchange/addressbook/client/Client.java b/Addressbook/src/org/mxchange/addressbook/client/Client.java index 5453894..2c4336c 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/Client.java +++ b/Addressbook/src/org/mxchange/addressbook/client/Client.java @@ -16,6 +16,7 @@ */ package org.mxchange.addressbook.client; +import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.FrameworkInterface; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.menu.item.SelectableMenuItem; @@ -54,11 +55,19 @@ public interface Client extends FrameworkInterface { */ public void displayOtherDataBox (final Contact contact); + /** + * Let the user choose what to change on the address: [n]ame, [a]ddress, + * [o]ther + * @param contact Contact instance to let the user change data + * @throws UnhandledUserChoiceException If choice is not supported + */ + public void doUserChangeAdressChoice (final Contact contact) throws UnhandledUserChoiceException; + /** * Asks the user for a choice and proceeds accordingly - * @throws java.lang.Exception + * @throws UnhandledUserChoiceException If choice is not supported */ - public void doUserChoice () throws Exception ; + public void doUserMenuChoice () throws UnhandledUserChoiceException ; /** * Enables isRunning attribute which singals that the client is running diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index 66ace07..36c6faa 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; +import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.application.AddressbookApplication; import org.mxchange.addressbook.application.Application; import org.mxchange.addressbook.client.BaseClient; @@ -128,12 +129,41 @@ public class ConsoleClient extends BaseClient implements Client { } @Override - public void doUserChoice () throws Exception { + public void doUserChangeAdressChoice (final Contact contact) throws UnhandledUserChoiceException { + // Ask the user for editing [name], [a]ddress or [other] data + char choice = this.enterChar(new char[]{'n', 'a', 'o', 'x'}, "Welchen Daten möchten Sie ändern? (n=Namensdaten, a=Anschriftsdaten, o=Andere, x=Zurück zur Hauptauswahl) "); + + // @TODO Get rid of this ugly switch block, too + switch (choice) { + case 'n': // Name data + this.getContactManager().changeNameData(contact); + break; + + case 'a': // Address data + this.getContactManager().changeAddressData(contact); + break; + + case 'o': // Other data + this.getContactManager().changeOtherData(contact); + break; + + case 'x': // Exit this menu + // Ignored as it should go back + break; + + default: + // @TODO throw own exception + throw new UnhandledUserChoiceException("Choice '" + choice + "' not handled yet."); + } + } + + @Override + public void doUserMenuChoice () throws UnhandledUserChoiceException { // 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ü): "); + char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Programm beenden): "); // @TODO Rewrite this ugly switch() block switch (choice) { @@ -162,8 +192,8 @@ public class ConsoleClient extends BaseClient implements Client { break; default: - // @TODO throw on exception - throw new Exception("choice " + choice + " invalid"); + // @TODO throw own exception + throw new UnhandledUserChoiceException("Choice '" + choice + "' not handled yet."); } } diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 80cffd3..e4e842c 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -88,6 +88,26 @@ public class ContactManager extends BaseManager implements ManageableContact { throw new UnsupportedOperationException("Not supported yet."); } + /** + * Let the user change address data + * + * @param contact Instance to change data + */ + @Override + public void changeAddressData (final Contact contact) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + /** + * Let the user change "name data" + * + * @param contact Instance to change data + */ + @Override + public void changeNameData (final Contact contact) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + /** * Let the user change other address */ @@ -96,6 +116,16 @@ public class ContactManager extends BaseManager implements ManageableContact { throw new UnsupportedOperationException("Not supported yet."); } + /** + * Let the user change other data + * + * @param contact Instance to change data + */ + @Override + public void changeOtherData (final Contact contact) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + /** * Allows the user to change his/her own data */ @@ -122,8 +152,12 @@ public class ContactManager extends BaseManager implements ManageableContact { // Display contact contact.show(this.getClient()); - // @TODO Unfinished - throw new UnsupportedOperationException("Method is not finished."); + try { + // Ask user what to change + this.getClient().doUserChangeAdressChoice(contact); + } catch (final Exception ex) { + this.getLogger().catching(ex); + } } /** diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java index 03bdd8e..ac0db41 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java @@ -37,11 +37,34 @@ public interface ManageableContact extends Manageable { */ public void addOtherAddress (); + /** + * The user can change address data, like street, ZIP code, city and country + * of given Contact instance. + * + * @param contact Instance to change data + */ + public void changeAddressData (final Contact contact); + + /** + * The user can change name data, like gender, surname, family name and + * company name (if business contact). + * + * @param contact Instance to change data + */ + public void changeNameData (final Contact contact); + /** * Let the user change other address */ public void changeOtherAddress (); + /** + * The user can change other data, like phone numbers or comments. + * + * @param contact Instance to change data + */ + public void changeOtherData (final Contact contact); + /** * Let the user change own data */ -- 2.39.2