Advertisement
kmitche9

Lab 9 - ContactViewModel

Apr 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. package edu.westga.cs1302.contacts.viewmodel;
  2.  
  3. import edu.westga.cs1302.contacts.model.Contact;
  4. import edu.westga.cs1302.contacts.model.Contacts;
  5. import javafx.beans.property.BooleanProperty;
  6. import javafx.beans.property.ListProperty;
  7. import javafx.beans.property.SimpleBooleanProperty;
  8. import javafx.beans.property.SimpleListProperty;
  9. import javafx.beans.property.SimpleStringProperty;
  10. import javafx.beans.property.StringProperty;
  11. import javafx.collections.FXCollections;
  12.  
  13. /**
  14. * The Class ContactViewModel.
  15. *
  16. * @author CS1302
  17. * @version 1.0
  18. */
  19. public class ContactViewModel {
  20.  
  21. private final StringProperty firstNameProperty;
  22. private final StringProperty lastNameProperty;
  23. private final StringProperty phoneNumberProperty;
  24. private Contacts contacts;
  25. private final ListProperty<Contact> contactsProperty;
  26. private final BooleanProperty contactSelectedProperty;
  27.  
  28. /**
  29. * Instantiates a new ContactViewModel view model.
  30. */
  31. public ContactViewModel() {
  32. this.firstNameProperty = new SimpleStringProperty();
  33. this.lastNameProperty = new SimpleStringProperty();
  34. this.phoneNumberProperty = new SimpleStringProperty();
  35. this.contacts = new Contacts();
  36. this.contactsProperty = new SimpleListProperty<Contact>();
  37. this.contactSelectedProperty = new SimpleBooleanProperty();
  38. }
  39.  
  40. /**
  41. * Contact selected property.
  42. *
  43. * @precondition none
  44. * @postcondition none
  45. *
  46. * @return the boolean property
  47. */
  48. public BooleanProperty contactSelectedProperty() {
  49. return this.contactSelectedProperty;
  50. }
  51.  
  52. /**
  53. * Contacts property.
  54. *
  55. * @precondition none
  56. * @postcondition none
  57. *
  58. * @return the list property
  59. */
  60. public ListProperty<Contact> contactsProperty() {
  61. return this.contactsProperty;
  62. }
  63.  
  64. /**
  65. * Gets the first name property.
  66. *
  67. * @precondition none
  68. * @postcondition none
  69. *
  70. * @return the first name property
  71. */
  72. public StringProperty firstNameProperty() {
  73. return this.firstNameProperty;
  74. }
  75.  
  76. /**
  77. * Gets the last name property.
  78. *
  79. * @precondition none
  80. * @postcondition none
  81. *
  82. * @return the last name property
  83. */
  84. public StringProperty lastNameProperty() {
  85. return this.lastNameProperty;
  86. }
  87.  
  88. /**
  89. * Gets the phone number property.
  90. *
  91. * @precondition none
  92. * @postcondition none
  93. *
  94. * @return the phone number property
  95. */
  96. public StringProperty phoneNumberProperty() {
  97. return this.phoneNumberProperty;
  98. }
  99.  
  100. /**
  101. * Adds the contact.
  102. *
  103. * @return true, if successful
  104. */
  105. public boolean addContact() {
  106. String firstName = this.firstNameProperty.get();
  107. String lastName = this.lastNameProperty.get();
  108. String phoneNumber = this.phoneNumberProperty.get();
  109. Contact newContact = new Contact(firstName, lastName, phoneNumber);
  110. int prevSize = this.contacts.size();
  111. this.contacts.put(phoneNumber, newContact);
  112. if (this.contacts.size() > prevSize) {
  113. this.firstNameProperty.set("");
  114. this.lastNameProperty.set("");
  115. this.phoneNumberProperty.set("");
  116. this.contactsProperty.set(FXCollections.observableArrayList(this.contacts.values()));
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }
  122.  
  123. /**
  124. * Updates contact.
  125. *
  126. * @return true, if successful
  127. */
  128. public boolean updateContact() {
  129. String searchTerm = this.phoneNumberProperty.getValue();
  130. Contact contact = this.contacts.get(searchTerm);
  131. boolean itemUpdated = false;
  132. if (!contact.getFirstName().equalsIgnoreCase(this.firstNameProperty.getValue())) {
  133. if (!contact.getLastName().equalsIgnoreCase(this.lastNameProperty.getValue())) {
  134. this.contacts.get(searchTerm).setFirstName(this.firstNameProperty.getValue());
  135. this.contacts.get(searchTerm).setLastName(this.lastNameProperty.getValue());
  136. itemUpdated = true;
  137. this.contactsProperty.set(FXCollections.observableArrayList(this.contacts.values()));
  138. }
  139. }
  140. if (itemUpdated == false) {
  141. if (!contact.getFirstName().equalsIgnoreCase(this.firstNameProperty.getValue())) {
  142. this.contacts.get(searchTerm).setFirstName(this.firstNameProperty.getValue());
  143. itemUpdated = true;
  144. this.contactsProperty.set(FXCollections.observableArrayList(this.contacts.values()));
  145. } else if (!contact.getLastName().equalsIgnoreCase(this.lastNameProperty.getValue())) {
  146. this.contacts.get(searchTerm).setLastName(this.lastNameProperty.getValue());
  147. itemUpdated = true;
  148. this.contactsProperty.set(FXCollections.observableArrayList(this.contacts.values()));
  149. }
  150. }
  151. return itemUpdated;
  152. }
  153.  
  154. /**
  155. * Search for a contact.
  156. *
  157. * @return true, if contact is contained in the collection.
  158. */
  159. public boolean searchForContact() {
  160. String searchTerm = "";
  161. Contact contact = null;
  162. boolean searchBoolean = false;
  163. if (this.firstNameProperty.getValue().isEmpty() && this.lastNameProperty.getValue().isEmpty()) {
  164. searchTerm = this.phoneNumberProperty.getValue();
  165. contact = this.contacts.get(searchTerm);
  166. if (contact != null) {
  167. this.firstNameProperty.set(contact.getFirstName());
  168. this.lastNameProperty.set(contact.getLastName());
  169. searchBoolean = true;
  170. }
  171. } else if (this.phoneNumberProperty.getValue().isEmpty() && this.lastNameProperty.getValue().isEmpty()) {
  172. searchTerm = this.firstNameProperty.getValue();
  173. for (Contact currContact : this.contacts.values()) {
  174. if (currContact.getFirstName().equalsIgnoreCase(searchTerm)) {
  175. this.lastNameProperty.set(currContact.getLastName());
  176. this.phoneNumberProperty.set(currContact.getPhoneNumber());
  177. searchBoolean = true;
  178. }
  179. }
  180. } else if (this.phoneNumberProperty.getValue().isEmpty() && this.firstNameProperty.getValue().isEmpty()) {
  181. searchTerm = this.lastNameProperty.getValue();
  182. for (Contact currContact : this.contacts.values()) {
  183. if (currContact.getLastName().equalsIgnoreCase(searchTerm)) {
  184. this.firstNameProperty.set(currContact.getFirstName());
  185. this.phoneNumberProperty.set(currContact.getPhoneNumber());
  186. searchBoolean = true;
  187. }
  188. }
  189. }
  190. return searchBoolean;
  191. }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement