+1 (315) 557-6473 

Create a Program to Create Contact Service Application in Java Assignment Solution.


Instructions

Objective
Write a program to create contact service application in java language.

Requirements and Specifications

program to create contact service application in java

Source Code

CONTACT

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package com.mycompany.contacttests;

public class Contact {

private String contactId;

private String firstName;

private String lastName;

private String address;

private String phoneNumber;

public Contact(String contactId, String firstName, String lastName, String address, String phoneNumber) {

//validate inputs against requirements

boolean isValid = validateInput(contactId, 10);

if(isValid) {

this.contactId = contactId;

}

isValid = isValid && setContactFirstName(firstName);

isValid = isValid && setContactLasttName(lastName);

isValid = isValid && setAddress(address);

isValid = isValid && setPhoneNumber(phoneNumber);

if(!isValid) {

throw new IllegalArgumentException("Invalid input");

}

}

public boolean setContactFirstName(String firstName) {

boolean isValid = validateInput(firstName, 10);

if(isValid) {

this.firstName = firstName;

}

return isValid;

}

public boolean setContactLasttName(String lastName) {

boolean isValid = validateInput(lastName, 10);

if(isValid) {

this.lastName = lastName;

}

return isValid;

}

public boolean setAddress(String address) {

boolean isValid = validateInput(address, 20);

if(isValid) {

this.address = address;

}

return isValid;

}

public boolean setPhoneNumber(String phoneNumber) {

boolean IsValid = phoneNumber.matches("\\d{10}");

if(IsValid) {

this.phoneNumber = phoneNumber;

}

return IsValid;

}

public String getContactId() {

return contactId;

}

public String getFirstName() {

return firstName;

}

public String getLastName() {

return lastName;

}

public String getAddress() {

return address;

}

public String getPhoneNumber() {

return phoneNumber;

}

private boolean validateInput(String item, int length) {

return (item != null && item.length() <= length && item.length() > 0);

}

}

CONTACT SERVICE

package com.mycompany.contacttests;

/*

* To change this license header, choose License Headers in Project Properties.

;* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

import java.util.HashMap;

import java.util.Map;

public class ContactService {

private static ContactService reference = new ContactService();

private final Map contacts;

ContactService() {

this.contacts = new HashMap();

}

//Create a singleton Contact Service

public static ContactService getService() {

return reference;

}

public boolean addContact(Contact contact) {

boolean isSuccess = false;

if(!contacts.containsKey(contact.getContactId())) {

contacts.put(contact.getContactId(), contact);

isSuccess = true;

}

return isSuccess;

}

public boolean deleteContact(String contactId) {

return contacts.remove(contactId) != null;

}

public Contact getContact(String contactId) {

return contacts.get(contactId);

}

}