]> git.mxchange.org Git - jaddressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java
Introduced own exception + added first sub menu for changing address data
[jaddressbook-lib.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.client.Client;\r
23 import org.mxchange.addressbook.contact.Contact;\r
24 import org.mxchange.addressbook.contact.user.UserContact;\r
25 import org.mxchange.addressbook.manager.BaseManager;\r
26 \r
27 /**\r
28  * A manager for contacts\r
29  *\r
30  * @author Roland Haeder\r
31  * @version 0.0\r
32  * @since 0.0\r
33  */\r
34 public class ContactManager extends BaseManager implements ManageableContact {\r
35 \r
36     /**\r
37      * All contacts\r
38      */\r
39     private final List<Contact> contacts;\r
40 \r
41     /**\r
42      * @param maxContacts Maximum allowed contacts\r
43      * @param client Client instance to use\r
44      */\r
45     public ContactManager (final int maxContacts, final Client client) {\r
46         // Always call super constructor first\r
47         super();\r
48 \r
49         // Init contacts\r
50         this.contacts = new ArrayList<>(maxContacts);\r
51 \r
52         // Debug message\r
53         //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);\r
54 \r
55         // Init client\r
56         this.setClient(client);\r
57     }\r
58 \r
59     /**\r
60      * Adds given contact to address book\r
61      *\r
62      * @param contact Contact being added\r
63      * @todo Add check for book size\r
64      */\r
65     @Override\r
66     public void addContact (final Contact contact) {\r
67         // Check if contact is found\r
68         if (this.isContactAlreadyAdded(contact)) {\r
69             // Contact already added\r
70             // @todo Do something here\r
71         } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {\r
72             // Own contact already added\r
73             // @todo Do something\r
74         }\r
75 \r
76         // Debug message\r
77         /* NOISY-DEBUG: */ this.getLogger().debug("Adding '" + contact.getSurname() + "' '" + contact.getFamilyName() + "' at pos '" + this.size () + "' ...");\r
78 \r
79         // Add contact\r
80         this.contacts.add(contact);\r
81     }\r
82 \r
83     /**\r
84      * Let the user add a new other address\r
85      */\r
86     @Override\r
87     public void addOtherAddress () {\r
88         throw new UnsupportedOperationException("Not supported yet.");\r
89     }\r
90 \r
91     /**\r
92      * Let the user change address data\r
93      * \r
94      * @param contact Instance to change data\r
95      */\r
96     @Override\r
97     public void changeAddressData (final Contact contact) {\r
98         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.\r
99     }\r
100 \r
101     /**\r
102      * Let the user change "name data"\r
103      * \r
104      * @param contact Instance to change data\r
105      */\r
106     @Override\r
107     public void changeNameData (final Contact contact) {\r
108         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.\r
109     }\r
110 \r
111     /**\r
112      * Let the user change other address\r
113      */\r
114     @Override\r
115     public void changeOtherAddress () {\r
116         throw new UnsupportedOperationException("Not supported yet.");\r
117     }\r
118 \r
119     /**\r
120      * Let the user change other data\r
121      *\r
122      * @param contact Instance to change data\r
123      */\r
124     @Override\r
125     public void changeOtherData (final Contact contact) {\r
126         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.\r
127     }\r
128 \r
129     /**\r
130      * Allows the user to change his/her own data\r
131      */\r
132     @Override\r
133     public void changeOwnData () {\r
134         /*\r
135          * First check if the user has registered own contact, before that \r
136          * nothing can be changed.\r
137          */\r
138         if (!this.isOwnContactAdded()) {\r
139             // Not added\r
140             this.getClient().displayMessage("Sie haben noch nicht Ihre Daten eingegeben.");\r
141 \r
142             // Skip any below code\r
143             return;\r
144         }\r
145 \r
146         // Instance\r
147         Contact contact = this.getOwnContact();\r
148 \r
149         // It must be found\r
150         assert(contact instanceof Contact);\r
151 \r
152         // Display contact\r
153         contact.show(this.getClient());\r
154 \r
155         try {\r
156             // Ask user what to change\r
157             this.getClient().doUserChangeAdressChoice(contact);\r
158         } catch (final Exception ex) {\r
159             this.getLogger().catching(ex);\r
160         }\r
161     }\r
162 \r
163     /**\r
164      * Let the user delete other address\r
165      */\r
166     @Override\r
167     public void deleteOtherAddress () {\r
168         throw new UnsupportedOperationException("Not supported yet.");\r
169     }\r
170 \r
171     /**\r
172      * Asks user for own data\r
173      */\r
174     @Override\r
175     public void enterOwnData () {\r
176         // First ask for gender\r
177         char gender = this.enterOwnGender();\r
178 \r
179         // 2nd for surname\r
180         String surname = this.enterOwnSurname();\r
181         \r
182         // And 3rd for family name\r
183         String familyName = this.enterOwnFamilyName();\r
184 \r
185         // Construct UserContact instance\r
186         Contact contact = new UserContact(gender, surname, familyName);\r
187 \r
188         // Mark contact as own\r
189         contact.enableFlagOwnContact();\r
190 \r
191         // Add it to contact "book"\r
192         this.addContact(contact);\r
193     }\r
194 \r
195     /**\r
196      * Getter for size\r
197      *\r
198      * @return size of contact "book"\r
199      */\r
200     @Override\r
201     public int size () {\r
202         return this.contacts.size();\r
203     }\r
204 \r
205     /**\r
206      * Asks the user for family name\r
207      * @return Family name of the user\r
208      */\r
209     private String enterOwnFamilyName () {\r
210         return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ");\r
211     }\r
212 \r
213     /**\r
214      * Asks the user for gender, until a valid has been entered\r
215      * @return Gender of the user\r
216      */\r
217     private char enterOwnGender () {\r
218         return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");\r
219     }\r
220 \r
221     /**\r
222      * Asks the user for surname\r
223      * @return Surname of the user\r
224      */\r
225     private String enterOwnSurname () {\r
226         return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ");\r
227     }\r
228 \r
229     /**\r
230      * "Getter" for own contact instance or null if not found\r
231      *\r
232      * @return Contact instance or null\r
233      */\r
234     private Contact getOwnContact () {\r
235         // Now get it back from address book, first get an iterator\r
236         Iterator<Contact> iterator = this.contacts.iterator();\r
237 \r
238         // Init instance\r
239         Contact contact = null;\r
240 \r
241         // Search all contact\r
242         while (iterator.hasNext()) {\r
243             // Get next instance\r
244             Contact next = iterator.next();\r
245 \r
246             // Is this own contact?\r
247             if (next.isOwnContact()) {\r
248                 // Found it\r
249                 contact = next;\r
250                 break;\r
251                 \r
252             }\r
253         }\r
254 \r
255         // Return instance or null\r
256         return contact;\r
257     }\r
258 \r
259     /**\r
260      * Checks whether given contact was found in "address book"\r
261      *\r
262      * @param checkContact Contact to be checked\r
263      * @return TRUE if found, FALSE if not found\r
264      */\r
265     private boolean isContactAlreadyAdded (final Contact checkContact) throws NullPointerException {\r
266         // Default is not found\r
267         boolean isFound = false;\r
268 \r
269         // Debug message\r
270         //* NOISY-DEBUG: */ this.getLogger().debug("Checking '" +  this.contacts.length + "' entries...");\r
271 \r
272         // Now get it back from address book, first get an iterator\r
273         Iterator<Contact> iterator = this.contacts.iterator();\r
274 \r
275         // Check entries\r
276         while (iterator.hasNext()) {\r
277             // Get next entry\r
278             Contact contact = iterator.next();\r
279 \r
280             // Debug message\r
281             //* NOISY-DEBUG: */ this.getLogger().debug("contact=" + contact + ",checkContent=" + checkContact);\r
282 \r
283             // Is it valid?\r
284             if ((contact instanceof Contact) && ((contact.equals(checkContact)))) {\r
285                 // Found matching entry\r
286                 isFound = true;\r
287                 break;\r
288             }\r
289         }\r
290 \r
291         // Return result\r
292         return isFound;\r
293     }\r
294 \r
295     /**\r
296      * Checks whether own contact is already added by checking all entries for isOwnContact flag\r
297      * @return Whether own contact is already added\r
298      */\r
299     private boolean isOwnContactAdded () {\r
300         // Default is not added\r
301         boolean isAdded = false;\r
302 \r
303         // Now get it back from address book, first get an iterator\r
304         Iterator<Contact> iterator = this.contacts.iterator();\r
305 \r
306         // Check entries\r
307         while (iterator.hasNext()) {\r
308             // Get next entry\r
309             Contact contact = iterator.next();\r
310 \r
311             // Is it valid?\r
312             if (contact instanceof Contact) {\r
313                 // Get flag\r
314                 isAdded = contact.isOwnContact();\r
315 \r
316                 // Is this own contact?\r
317                 if (isAdded) {\r
318                     // Then abort loop\r
319                     break;\r
320                 }\r
321             }\r
322         }\r
323         // Return result\r
324         return isAdded;\r
325     }\r
326 }\r