X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=Addressbook%2Fsrc%2Forg%2Fmxchange%2Faddressbook%2Fmanager%2Fcontact%2FContactManager.java;h=049901197963149f28112ebcb53c9e920a1ff983;hb=d16c6288f14c09b779d9d8f395529d411cdd156e;hp=7dca9da4d0705fea2aca57287c90299b123ade72;hpb=3457fa799a3756c45072acd0da5ed82de5535aa0;p=jbonuscard-lib.git diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 7dca9da..0499011 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -16,17 +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 @@ -35,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; @@ -50,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); @@ -58,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); } @@ -89,6 +91,56 @@ public class ContactManager extends BaseManager implements ManageableContact { 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().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); + + // Display contact + contact.show(this.getClient()); + + try { + // Ask user what to change + this.getClient().userChooseChangeContactData(contact); + } catch (final UnhandledUserChoiceException ex) { + this.getLogger().catching(ex); + } + } + + /** + * Let the user delete other address + */ + @Override + public void deleteOtherAddress () { + throw new UnsupportedOperationException("Not supported yet."); + } + /** * Let the user change address data * @@ -96,8 +148,33 @@ public class ContactManager extends BaseManager implements ManageableContact { * @param client Client instance to call back */ @Override - public void changeAddressData (final Contact contact, final Client client) { - throw new UnsupportedOperationException("Not supported yet."); + 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(); } /** @@ -107,7 +184,7 @@ public class ContactManager extends BaseManager implements ManageableContact { * @param client Client instance to call back */ @Override - public void changeNameData (final Contact contact, final Client client) { + public void doChangeNameData (final Contact contact, final Client client) { // First display them again client.displayNameBox(contact); @@ -124,7 +201,7 @@ public class ContactManager extends BaseManager implements ManageableContact { String familyName = this.enterOwnFamilyName(); // And company - String companyName = this.enterCompanyName(); + String companyName = this.enterOwnCompanyName(); // Update contact instance contact.updateNameData(gender, surname, familyName, companyName); @@ -132,14 +209,9 @@ public class ContactManager extends BaseManager implements ManageableContact { // Then re-ask them ... throw new UnsupportedOperationException("Changing contact entries not finished."); } - } - /** - * Let the user change other address - */ - @Override - public void changeOtherAddress () { - throw new UnsupportedOperationException("Not supported yet."); + // Flush whole list + this.flush(); } /** @@ -147,59 +219,47 @@ public class ContactManager extends BaseManager implements ManageableContact { * * @param contact Instance to change data * @param client Client instance to call back + * @todo Didn't handle birthday */ @Override - public void changeOtherData (final Contact contact, final Client client) { - throw new UnsupportedOperationException("Not supported yet."); - } + public void doChangeOtherData (final Contact contact, final Client client) { + // First display them again + client.displayOtherDataBox(contact); - /** - * 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."); + // Is this own data? + if (contact.isOwnContact()) { + // Re-ask own data + // Phone number + String phoneNumber = this.enterOwnPhoneNumber(); - // Skip any below code - return; - } + // Phone number + String cellNumber = this.enterOwnCellNumber(); - // Instance - Contact contact = this.getOwnContact(); + // Fax number + String faxNumber = this.enterOwnFaxNumber(); - // It must be found - assert(contact instanceof Contact); + // Email address + String email = this.enterOwnEmailAddress(); - // Display contact - contact.show(this.getClient()); + // Comment + String comment = this.enterOwnComment(); - try { - // Ask user what to change - this.getClient().doUserChangeAdressChoice(contact); - } catch (final UnhandledUserChoiceException ex) { - this.getLogger().catching(ex); + // Update contact instance + contact.updateOtherData(phoneNumber, cellNumber, faxNumber, email, null, comment); + } else { + // Then re-ask them ... + throw new UnsupportedOperationException("Changing contact entries not finished."); } - } - /** - * Let the user delete other address - */ - @Override - public void deleteOtherAddress () { - throw new UnsupportedOperationException("Not supported yet."); + // 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(); @@ -210,16 +270,60 @@ public class ContactManager extends BaseManager implements ManageableContact { String familyName = this.enterOwnFamilyName(); // Company name ... - String companyName = this.enterCompanyName(); + String companyName = this.enterOwnCompanyName(); // Construct UserContact instance Contact contact = new UserContact(gender, surname, familyName, companyName); - // Mark contact as own - contact.enableFlagOwnContact(); - // Add it to contact "book" + this.registerContact(contact); + } + + /** + * 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."); } /** @@ -232,30 +336,103 @@ 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 + * + * @return User's company name */ - private String enterCompanyName () { + 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: ", 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 @@ -264,6 +441,31 @@ public class ContactManager extends BaseManager implements ManageableContact { 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; + } + /** * "Getter" for own contact instance or null if not found * @@ -331,7 +533,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 () {