]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java
Introduced updateAddressData() + changing own "address data" is basicly finished
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / manager / contact / ContactManager.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\r
4  * This program is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16  */\r
17 package org.mxchange.addressbook.manager.contact;\r
18 \r
19 import java.util.ArrayList;\r
20 import java.util.Iterator;\r
21 import java.util.List;\r
22 import org.mxchange.addressbook.UnhandledUserChoiceException;\r
23 import org.mxchange.addressbook.client.Client;\r
24 import org.mxchange.addressbook.contact.Contact;\r
25 import org.mxchange.addressbook.contact.user.UserContact;\r
26 import org.mxchange.addressbook.manager.BaseManager;\r
27 \r
28 /**\r
29  * A manager for contacts\r
30  *\r
31  * @author Roland Haeder\r
32  * @version 0.0\r
33  * @since 0.0\r
34  */\r
35 public class ContactManager extends BaseManager implements ManageableContact {\r
36 \r
37     /**\r
38      * All contacts\r
39      */\r
40     private final List<Contact> contacts;\r
41 \r
42     /**\r
43      * @param maxContacts Maximum allowed contacts\r
44      * @param client Client instance to use\r
45      */\r
46     public ContactManager (final int maxContacts, final Client client) {\r
47         // Always call super constructor first\r
48         super();\r
49 \r
50         // Init contacts\r
51         this.contacts = new ArrayList<>(maxContacts);\r
52 \r
53         // Debug message\r
54         //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);\r
55 \r
56         // Init client\r
57         this.setClient(client);\r
58     }\r
59 \r
60     /**\r
61      * Adds given contact to address book\r
62      *\r
63      * @param contact Contact being added\r
64      * @todo Add check for book size\r
65      */\r
66     @Override\r
67     public void addContact (final Contact contact) {\r
68         // Check if contact is found\r
69         if (this.isContactAlreadyAdded(contact)) {\r
70             // Contact already added\r
71             // @todo Do something here\r
72         } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {\r
73             // Own contact already added\r
74             // @todo Do something\r
75         }\r
76 \r
77         // Debug message\r
78         /* NOISY-DEBUG: */ this.getLogger().debug("Adding '" + contact.getSurname() + "' '" + contact.getFamilyName() + "' at pos '" + this.size () + "' ...");\r
79 \r
80         // Add contact\r
81         this.contacts.add(contact);\r
82     }\r
83 \r
84     /**\r
85      * Let the user add a new other address\r
86      */\r
87     @Override\r
88     public void addOtherAddress () {\r
89         throw new UnsupportedOperationException("Not supported yet.");\r
90     }\r
91 \r
92     /**\r
93      * Let the user change address data\r
94      * \r
95      * @param contact Instance to change data\r
96      * @param client Client instance to call back\r
97      */\r
98     @Override\r
99     public void changeAddressData (final Contact contact, final Client client) {\r
100         // First display it again\r
101         client.displayAddressBox(contact);\r
102 \r
103         // Is it own data?\r
104         if (contact.isOwnContact()) {\r
105             // Own address data\r
106             String street = this.enterOwnStreet();\r
107 \r
108             // Get zip code\r
109             int zipCode = this.enterOwnZipCode();\r
110 \r
111             // Get city name\r
112             String city = this.enterOwnCity();\r
113 \r
114             // Get country code\r
115             String countryCode = this.enterOwnCountryCode();\r
116 \r
117             // Update address data\r
118             contact.updateAddressData(street, zipCode, city, countryCode);\r
119         } else {\r
120             // Other contact's address data to change\r
121             throw new UnsupportedOperationException("Changing contact entries not finished.");\r
122         }\r
123     }\r
124 \r
125     /**\r
126      * Let the user change "name data"\r
127      * \r
128      * @param contact Instance to change data\r
129      * @param client Client instance to call back\r
130      */\r
131     @Override\r
132     public void changeNameData (final Contact contact, final Client client) {\r
133         // First display them again\r
134         client.displayNameBox(contact);\r
135 \r
136         // Is this own data?\r
137         if (contact.isOwnContact()) {\r
138             // Re-ask own data\r
139             // Gender:\r
140             char gender = this.enterOwnGender();\r
141 \r
142             // Surname\r
143             String surname = this.enterOwnSurname();\r
144 \r
145             // Family name\r
146             String familyName = this.enterOwnFamilyName();\r
147 \r
148             // And company\r
149             String companyName = this.enterCompanyName();\r
150 \r
151             // Update contact instance\r
152             contact.updateNameData(gender, surname, familyName, companyName);\r
153         } else {\r
154             // Then re-ask them ...\r
155             throw new UnsupportedOperationException("Changing contact entries not finished.");\r
156         }\r
157     }\r
158 \r
159     /**\r
160      * Let the user change other address\r
161      */\r
162     @Override\r
163     public void changeOtherAddress () {\r
164         throw new UnsupportedOperationException("Not supported yet.");\r
165     }\r
166 \r
167     /**\r
168      * Let the user change other data\r
169      *\r
170      * @param contact Instance to change data\r
171      * @param client Client instance to call back\r
172      */\r
173     @Override\r
174     public void changeOtherData (final Contact contact, final Client client) {\r
175         throw new UnsupportedOperationException("Not supported yet.");\r
176     }\r
177 \r
178     /**\r
179      * Allows the user to change his/her own data\r
180      */\r
181     @Override\r
182     public void changeOwnData () {\r
183         /*\r
184          * First check if the user has registered own contact, before that \r
185          * nothing can be changed.\r
186          */\r
187         if (!this.isOwnContactAdded()) {\r
188             // Not added\r
189             this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben.");\r
190 \r
191             // Skip any below code\r
192             return;\r
193         }\r
194 \r
195         // Instance\r
196         Contact contact = this.getOwnContact();\r
197 \r
198         // It must be found\r
199         assert(contact instanceof Contact);\r
200 \r
201         // Display contact\r
202         contact.show(this.getClient());\r
203 \r
204         try {\r
205             // Ask user what to change\r
206             this.getClient().doUserChangeAdressChoice(contact);\r
207         } catch (final UnhandledUserChoiceException ex) {\r
208             this.getLogger().catching(ex);\r
209         }\r
210     }\r
211 \r
212     /**\r
213      * Let the user delete other address\r
214      */\r
215     @Override\r
216     public void deleteOtherAddress () {\r
217         throw new UnsupportedOperationException("Not supported yet.");\r
218     }\r
219 \r
220     /**\r
221      * Asks user for own data\r
222      */\r
223     @Override\r
224     public void enterOwnData () {\r
225         // First ask for gender\r
226         char gender = this.enterOwnGender();\r
227 \r
228         // 2nd for surname\r
229         String surname = this.enterOwnSurname();\r
230         \r
231         // And 3rd for family name\r
232         String familyName = this.enterOwnFamilyName();\r
233 \r
234         // Company name ...\r
235         String companyName = this.enterCompanyName();\r
236 \r
237         // Construct UserContact instance\r
238         Contact contact = new UserContact(gender, surname, familyName, companyName);\r
239 \r
240         // Mark contact as own\r
241         contact.enableFlagOwnContact();\r
242 \r
243         // Add it to contact "book"\r
244         this.addContact(contact);\r
245     }\r
246 \r
247     /**\r
248      * Getter for size\r
249      *\r
250      * @return size of contact "book"\r
251      */\r
252     @Override\r
253     public int size () {\r
254         return this.contacts.size();\r
255     }\r
256 \r
257     /**\r
258      * Asks the user for his/her company name\r
259      * @return \r
260      */\r
261     private String enterCompanyName () {\r
262         return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);\r
263     }\r
264 \r
265     /**\r
266      * Asks the user for his/her city's name\r
267      * \r
268      * @return City's name of the user\r
269      */\r
270     private String enterOwnCity () {\r
271         return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);\r
272     }\r
273 \r
274     /**\r
275      * Asks user for his/her own country code\r
276      * \r
277      * @return User's own country code\r
278      */\r
279     private String enterOwnCountryCode () {\r
280         return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false);\r
281     }\r
282 \r
283     /**\r
284      * Asks the user for family name\r
285      * @return Family name of the user\r
286      */\r
287     private String enterOwnFamilyName () {\r
288         return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);\r
289     }\r
290 \r
291     /**\r
292      * Asks the user for gender, until a valid has been entered\r
293      * @return Gender of the user\r
294      */\r
295     private char enterOwnGender () {\r
296         return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");\r
297     }\r
298 \r
299     /**\r
300      * Asks the user for own street (including number)\r
301      */\r
302     private String enterOwnStreet () {\r
303         return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);\r
304     }\r
305 \r
306     /**\r
307      * Asks the user for surname\r
308      * @return Surname of the user\r
309      */\r
310     private String enterOwnSurname () {\r
311         return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);\r
312     }\r
313 \r
314     /**\r
315      * Asks the user for own ZIP code\r
316      * @return ZIP code\r
317      */\r
318     private int enterOwnZipCode () {\r
319         return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");\r
320     }\r
321 \r
322     /**\r
323      * "Getter" for own contact instance or null if not found\r
324      *\r
325      * @return Contact instance or null\r
326      */\r
327     private Contact getOwnContact () {\r
328         // Now get it back from address book, first get an iterator\r
329         Iterator<Contact> iterator = this.contacts.iterator();\r
330 \r
331         // Init instance\r
332         Contact contact = null;\r
333 \r
334         // Search all contact\r
335         while (iterator.hasNext()) {\r
336             // Get next instance\r
337             Contact next = iterator.next();\r
338 \r
339             // Is this own contact?\r
340             if (next.isOwnContact()) {\r
341                 // Found it\r
342                 contact = next;\r
343                 break;\r
344                 \r
345             }\r
346         }\r
347 \r
348         // Return instance or null\r
349         return contact;\r
350     }\r
351 \r
352     /**\r
353      * Checks whether given contact was found in "address book"\r
354      *\r
355      * @param checkContact Contact to be checked\r
356      * @return TRUE if found, FALSE if not found\r
357      */\r
358     private boolean isContactAlreadyAdded (final Contact checkContact) throws NullPointerException {\r
359         // Default is not found\r
360         boolean isFound = false;\r
361 \r
362         // Debug message\r
363         //* NOISY-DEBUG: */ this.getLogger().debug("Checking '" +  this.contacts.length + "' entries...");\r
364 \r
365         // Now get it back from address book, first get an iterator\r
366         Iterator<Contact> iterator = this.contacts.iterator();\r
367 \r
368         // Check entries\r
369         while (iterator.hasNext()) {\r
370             // Get next entry\r
371             Contact contact = iterator.next();\r
372 \r
373             // Debug message\r
374             //* NOISY-DEBUG: */ this.getLogger().debug("contact=" + contact + ",checkContent=" + checkContact);\r
375 \r
376             // Is it valid?\r
377             if ((contact instanceof Contact) && ((contact.equals(checkContact)))) {\r
378                 // Found matching entry\r
379                 isFound = true;\r
380                 break;\r
381             }\r
382         }\r
383 \r
384         // Return result\r
385         return isFound;\r
386     }\r
387 \r
388     /**\r
389      * Checks whether own contact is already added by checking all entries for isOwnContact flag\r
390      * @return Whether own contact is already added\r
391      */\r
392     private boolean isOwnContactAdded () {\r
393         // Default is not added\r
394         boolean isAdded = false;\r
395 \r
396         // Now get it back from address book, first get an iterator\r
397         Iterator<Contact> iterator = this.contacts.iterator();\r
398 \r
399         // Check entries\r
400         while (iterator.hasNext()) {\r
401             // Get next entry\r
402             Contact contact = iterator.next();\r
403 \r
404             // Is it valid?\r
405             if (contact instanceof Contact) {\r
406                 // Get flag\r
407                 isAdded = contact.isOwnContact();\r
408 \r
409                 // Is this own contact?\r
410                 if (isAdded) {\r
411                     // Then abort loop\r
412                     break;\r
413                 }\r
414             }\r
415         }\r
416         // Return result\r
417         return isAdded;\r
418     }\r
419 }\r