X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=Addressbook%2Fsrc%2Forg%2Fmxchange%2Faddressbook%2Fmanager%2Fcontact%2FContactManager.java;h=1b269ec3e2a0632fb4d178f4316c26ccf16fc72c;hb=e06df9f02fa3fc03c21d5b2c3e2c8cb476be4ea3;hp=096197be45fa6c1f9cdf312f87264662fce34293;hpb=c3a6502c11c40b41c5c20988dd7344526d583b14;p=addressbook-swing.git diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 096197b..1b269ec 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -16,16 +16,22 @@ */ package org.mxchange.addressbook.manager.contact; +import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; +import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.client.Client; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.contact.user.UserContact; +import org.mxchange.addressbook.database.frontend.contact.ContactDatabaseFrontend; +import org.mxchange.addressbook.database.frontend.contact.ContactWrapper; import org.mxchange.addressbook.manager.BaseManager; /** - * A manager for contacts + * A manager for contacts, please note that this implementation loads the whole + * list into RAM. * * @author Roland Haeder * @version 0.0 @@ -34,7 +40,12 @@ import org.mxchange.addressbook.manager.BaseManager; public class ContactManager extends BaseManager implements ManageableContact { /** - * All contacts + * A ContactWrapper instance + */ + private final ContactWrapper contactDatabase; + + /** + * A list of all contacts */ private final List contacts; @@ -49,6 +60,12 @@ public class ContactManager extends BaseManager implements ManageableContact { // Init contacts this.contacts = new ArrayList<>(maxContacts); + // Init database connection + this.contactDatabase = new ContactDatabaseFrontend(); + + // Read all entries + this.contactDatabase.readAllContacts(this); + // Debug message //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client); @@ -57,26 +74,12 @@ public class ContactManager extends BaseManager implements ManageableContact { } /** - * Adds given contact to address book + * Adds given Contact instance to list * - * @param contact Contact being added - * @todo Add check for book size + * @param contact Contact instance to add */ @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); } @@ -102,25 +105,32 @@ public class ContactManager extends BaseManager implements ManageableContact { @Override public void changeOwnData () { /* - * First check if the user has registered own contact, before that + * 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."); - + this.getClient().outputMessage("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."); + + // Display contact + contact.show(this.getClient()); + + try { + // Ask user what to change + this.getClient().userChooseChangeContactData(contact); + } catch (final UnhandledUserChoiceException ex) { + this.getLogger().catching(ex); + } } /** @@ -131,11 +141,125 @@ 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 + * @param client Client instance to call back + */ + @Override + public void doChangeAddressData (final Contact contact, final Client client) { + // First display it again + client.displayAddressBox(contact); + + // Is it own data? + if (contact.isOwnContact()) { + // Own address data + String street = this.enterOwnStreet(); + + // Get zip code + int zipCode = this.enterOwnZipCode(); + + // Get city name + String city = this.enterOwnCity(); + + // Get country code + String countryCode = this.enterOwnCountryCode(); + + // Update address data + contact.updateAddressData(street, zipCode, city, countryCode); + } else { + // Other contact's address data to change + throw new UnsupportedOperationException("Changing contact entries not finished."); + } + + // Flush whole list + this.flush(); + } + + /** + * Let the user change "name data" + * + * @param contact Instance to change data + * @param client Client instance to call back + */ + @Override + public void doChangeNameData (final Contact contact, final Client client) { + // First display them again + client.displayNameBox(contact); + + // Is this own data? + if (contact.isOwnContact()) { + // Re-ask own data + // Gender: + char gender = this.enterOwnGender(); + + // Surname + String surname = this.enterOwnSurname(); + + // Family name + String familyName = this.enterOwnFamilyName(); + + // And company + String companyName = this.enterOwnCompanyName(); + + // Update contact instance + contact.updateNameData(gender, surname, familyName, companyName); + } else { + // Then re-ask them ... + throw new UnsupportedOperationException("Changing contact entries not finished."); + } + + // Flush whole list + this.flush(); + } + + /** + * Let the user change other data + * + * @param contact Instance to change data + * @param client Client instance to call back + * @todo Didn't handle birthday + */ + @Override + public void doChangeOtherData (final Contact contact, final Client client) { + // First display them again + client.displayOtherDataBox(contact); + + // Is this own data? + if (contact.isOwnContact()) { + // Re-ask own data + // Phone number + String phoneNumber = this.enterOwnPhoneNumber(); + + // Phone number + String cellNumber = this.enterOwnCellNumber(); + + // Fax number + String faxNumber = this.enterOwnFaxNumber(); + + // Email address + String email = this.enterOwnEmailAddress(); + + // Comment + String comment = this.enterOwnComment(); + + // Update contact instance + contact.updateOtherData(phoneNumber, cellNumber, faxNumber, email, null, comment); + } else { + // Then re-ask them ... + throw new UnsupportedOperationException("Changing contact entries not finished."); + } + + // Flush whole list + this.flush(); + } + /** * Asks user for own data */ @Override - public void enterOwnData () { + public void doEnterOwnData () { // First ask for gender char gender = this.enterOwnGender(); @@ -145,14 +269,70 @@ public class ContactManager extends BaseManager implements ManageableContact { // And 3rd for family name String familyName = this.enterOwnFamilyName(); - // Construct UserContact instance - Contact contact = new UserContact(gender, surname, familyName); + // Company name ... + String companyName = this.enterOwnCompanyName(); - // Mark contact as own - contact.enableFlagOwnContact(); + // Construct UserContact instance + Contact contact = new UserContact(gender, surname, familyName, companyName); // Add it to contact "book" + this.registerContact(contact); + } + + /** + * Shuts down this contact manager + */ + @Override + public void doShutdown () { + // Shut down the database layer + this.contactDatabase.doShutdown(); + } + + /** + * Getter for whole contact list + * + * @return List of all contacts + */ + @Override + public List getList () { + return Collections.unmodifiableList(this.contacts); + } + + @Override + public void listContacts () { + throw new UnsupportedOperationException("Not supported yet."); + } + + /** + * Adds given contact to address book and flushes all entries to database + * + * @param contact Contact being added + * @todo Add check for book size + */ + @Override + public void registerContact (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(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getSurname(), contact.getFamilyName(), this.size())); + + // Add contact to internal list this.addContact(contact); + + // Flush whole list + this.flush(); + } + + @Override + public void searchContacts () { + throw new UnsupportedOperationException("Not supported yet."); } /** @@ -165,28 +345,134 @@ public class ContactManager extends BaseManager implements ManageableContact { return this.contacts.size(); } + /** + * Asks the user for his/her cellphone number + * + * @return User's cellphone number + */ + private String enterOwnCellNumber () { + return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true); + } + + /** + * Asks the user for his/her city's name + * + * @return City's name of the user + */ + private String enterOwnCity () { + return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false); + } + + /** + * Asks the user for his/her city's name + * + * @return City's name of the user + */ + private String enterOwnComment () { + return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true); + } + + /** + * Asks the user for his/her company name + * + * @return User's company name + */ + private String enterOwnCompanyName () { + return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true); + } + + /** + * Asks user for his/her own country code + * + * @return User's own country code + */ + private String enterOwnCountryCode () { + return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase(); + } + + /** + * Asks user for his/her own country code + * + * @return User's own country code + */ + private String enterOwnEmailAddress () { + return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true); + } + /** * 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: "); + return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false); + } + + /** + * Asks the user for family name + * + * @return Family name of the user + */ + private String enterOwnFaxNumber () { + return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true); } /** * 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 phone number + * + * @return Phone number of the user + */ + private String enterOwnPhoneNumber () { + return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true); + } + + /** + * Asks the user for own street (including number) + */ + private String enterOwnStreet () { + return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false); + } + /** * 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: "); + return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false); + } + + /** + * Asks the user for own ZIP code + * @return ZIP code + */ + private int enterOwnZipCode () { + return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: "); + } + + /** + * Flushes all entries by calling database backend + */ + private void flush () { + // Flusgh all + this.getContactDatabase().flushAllContacts(this); + } + + /** + * A ContactWrapper instance + * + * @return the database + */ + private ContactWrapper getContactDatabase () { + return this.contactDatabase; } /** @@ -256,7 +542,9 @@ public class ContactManager extends BaseManager implements ManageableContact { } /** - * Checks whether own contact is already added by checking all entries for isOwnContact flag + * Checks whether own contact is already added by checking all entries for + * isOwnContact flag + * * @return Whether own contact is already added */ private boolean isOwnContactAdded () {