Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.westga.cs1302.contacts.viewmodel;
- import edu.westga.cs1302.contacts.model.Contact;
- import edu.westga.cs1302.contacts.model.Contacts;
- import javafx.beans.property.BooleanProperty;
- import javafx.beans.property.ListProperty;
- import javafx.beans.property.SimpleBooleanProperty;
- import javafx.beans.property.SimpleListProperty;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.beans.property.StringProperty;
- import javafx.collections.FXCollections;
- /**
- * The Class ContactViewModel.
- *
- * @author CS1302
- * @version 1.0
- */
- public class ContactViewModel {
- private final StringProperty firstNameProperty;
- private final StringProperty lastNameProperty;
- private final StringProperty phoneNumberProperty;
- private Contacts contacts;
- private final ListProperty<Contact> contactsProperty;
- private final BooleanProperty contactSelectedProperty;
- /**
- * Instantiates a new ContactViewModel view model.
- */
- public ContactViewModel() {
- this.firstNameProperty = new SimpleStringProperty();
- this.lastNameProperty = new SimpleStringProperty();
- this.phoneNumberProperty = new SimpleStringProperty();
- this.contacts = new Contacts();
- this.contactsProperty = new SimpleListProperty<Contact>();
- this.contactSelectedProperty = new SimpleBooleanProperty();
- }
- /**
- * Contact selected property.
- *
- * @precondition none
- * @postcondition none
- *
- * @return the boolean property
- */
- public BooleanProperty contactSelectedProperty() {
- return this.contactSelectedProperty;
- }
- /**
- * Contacts property.
- *
- * @precondition none
- * @postcondition none
- *
- * @return the list property
- */
- public ListProperty<Contact> contactsProperty() {
- return this.contactsProperty;
- }
- /**
- * Gets the first name property.
- *
- * @precondition none
- * @postcondition none
- *
- * @return the first name property
- */
- public StringProperty firstNameProperty() {
- return this.firstNameProperty;
- }
- /**
- * Gets the last name property.
- *
- * @precondition none
- * @postcondition none
- *
- * @return the last name property
- */
- public StringProperty lastNameProperty() {
- return this.lastNameProperty;
- }
- /**
- * Gets the phone number property.
- *
- * @precondition none
- * @postcondition none
- *
- * @return the phone number property
- */
- public StringProperty phoneNumberProperty() {
- return this.phoneNumberProperty;
- }
- /**
- * Adds the contact.
- *
- * @return true, if successful
- */
- public boolean addContact() {
- String firstName = this.firstNameProperty.get();
- String lastName = this.lastNameProperty.get();
- String phoneNumber = this.phoneNumberProperty.get();
- Contact newContact = new Contact(firstName, lastName, phoneNumber);
- int prevSize = this.contacts.size();
- this.contacts.put(phoneNumber, newContact);
- if (this.contacts.size() > prevSize) {
- this.firstNameProperty.set("");
- this.lastNameProperty.set("");
- this.phoneNumberProperty.set("");
- this.contactsProperty.set(FXCollections.observableArrayList(this.contacts.values()));
- return true;
- } else {
- return false;
- }
- }
- /**
- * Updates contact.
- *
- * @return true, if successful
- */
- public boolean updateContact() {
- String searchTerm = this.phoneNumberProperty.getValue();
- Contact contact = this.contacts.get(searchTerm);
- boolean itemUpdated = false;
- if (!contact.getFirstName().equalsIgnoreCase(this.firstNameProperty.getValue())) {
- if (!contact.getLastName().equalsIgnoreCase(this.lastNameProperty.getValue())) {
- this.contacts.get(searchTerm).setFirstName(this.firstNameProperty.getValue());
- this.contacts.get(searchTerm).setLastName(this.lastNameProperty.getValue());
- itemUpdated = true;
- this.contactsProperty.set(FXCollections.observableArrayList(this.contacts.values()));
- }
- }
- if (itemUpdated == false) {
- if (!contact.getFirstName().equalsIgnoreCase(this.firstNameProperty.getValue())) {
- this.contacts.get(searchTerm).setFirstName(this.firstNameProperty.getValue());
- itemUpdated = true;
- this.contactsProperty.set(FXCollections.observableArrayList(this.contacts.values()));
- } else if (!contact.getLastName().equalsIgnoreCase(this.lastNameProperty.getValue())) {
- this.contacts.get(searchTerm).setLastName(this.lastNameProperty.getValue());
- itemUpdated = true;
- this.contactsProperty.set(FXCollections.observableArrayList(this.contacts.values()));
- }
- }
- return itemUpdated;
- }
- /**
- * Search for a contact.
- *
- * @return true, if contact is contained in the collection.
- */
- public boolean searchForContact() {
- String searchTerm = "";
- Contact contact = null;
- boolean searchBoolean = false;
- if (this.firstNameProperty.getValue().isEmpty() && this.lastNameProperty.getValue().isEmpty()) {
- searchTerm = this.phoneNumberProperty.getValue();
- contact = this.contacts.get(searchTerm);
- if (contact != null) {
- this.firstNameProperty.set(contact.getFirstName());
- this.lastNameProperty.set(contact.getLastName());
- searchBoolean = true;
- }
- } else if (this.phoneNumberProperty.getValue().isEmpty() && this.lastNameProperty.getValue().isEmpty()) {
- searchTerm = this.firstNameProperty.getValue();
- for (Contact currContact : this.contacts.values()) {
- if (currContact.getFirstName().equalsIgnoreCase(searchTerm)) {
- this.lastNameProperty.set(currContact.getLastName());
- this.phoneNumberProperty.set(currContact.getPhoneNumber());
- searchBoolean = true;
- }
- }
- } else if (this.phoneNumberProperty.getValue().isEmpty() && this.firstNameProperty.getValue().isEmpty()) {
- searchTerm = this.lastNameProperty.getValue();
- for (Contact currContact : this.contacts.values()) {
- if (currContact.getLastName().equalsIgnoreCase(searchTerm)) {
- this.firstNameProperty.set(currContact.getFirstName());
- this.phoneNumberProperty.set(currContact.getPhoneNumber());
- searchBoolean = true;
- }
- }
- }
- return searchBoolean;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement