Question: 1. Complete the Person class: Add error-checking code to the accessor and mutator methods, as follows: - For setEmail, setFirstName, and setSurname: if the method
1. Complete the Person class:
Add error-checking code to the accessor and mutator methods, as follows:
- For setEmail, setFirstName, and setSurname: if the method is passed an empty string, it should do nothing; but otherwise it should set the appropriate field.
- For setMobile: if the method is passed a valid mobile phone number, it should set the appropriate field; otherwise it should do nothing. A string is a valid mobile phone number if every character in it is a digit from 0 to 9.
Hints:
- To convert a string so you can process it in a for-each loop you can use the String.toCharArray method. You do not need to know any array operations for this task.
- To test whether a character is a digit, you can use the Character.isDigit method.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Complete the addSocialMediaAccount and getSocialMediaID methods. They should behave as described in the Javadoc comments for those methods.
Complete the printContactDetails method:
The method should print, in order, 3 lines showing the name, mobile and email of the person, prefaced by the strings "name: ", "mobile: " and "email: ", respectively. The name of a person is their first name and surname, separated by a space.
For each of the social media accounts the person has, the method should print one line containing the website name, the userID, and the website URL, separated by commas.
For example, the following code will construct a Person object, and add details to it:
Person p = new Person("Diana", "Prince");
p.setMobile("0409670123");
p.setEmail("diana.prince@inscom.mil");
p.addSocialMediaAccount("@dianap", "Twitter", "http://twitter.com/", 50);
p.addSocialMediaAccount("diana.prince", "Facebook", "http://facebook.com/", 90);
Calling the printContactDetails method on the object p should print the following:
name: Diana Prince
mobile: 0409670123
email: diana.prince@inscom.mil
Twitter,@dianap,http://twitter.com/
Facebook,diana.prince,http://facebook.com/
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList; /** * Person details including contacts and social media accounts * */ public class Person { private String firstName; private String surname; private String mobile; private String email; private ArrayList
/** * @return the person's first name */ public String getFirstName() { return ""; // replace this line with your own code } /** * Set the person's first name * unless the parameter is an empty string. */ public void setFirstName(String firstName) { // replace this line with your own code } /** * Return the person's surname */ public String getSurname() { return ""; // replace this line with your own code } /** * Set the person's surname * unless the parameter is an empty string. */ public void setSurname(String surname) { // replace this line with your own code } /** * Return the person's mobile phone number */ public String getMobile() { return ""; // replace this line with your own code } /** * Set the person's mobile phone number */ public void setMobile(String mobile) { this.mobile = mobile; } /** * Return the person's email address */ public String getEmail() { return ""; // replace this line with your own code } /** * Set the person's email address */ public void setEmail(String email) { // replace this line with your own code } /** * Create a new SocialMediaAccount object, and add it to the socialMediaAccounts list. */ public void addSocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) { // replace this line with your own code } /** * Search the socialMediaAccounts list for an account on the website specified by the websiteName * parameter, and return the userID for that account. If no such account can be found, return * null. */ public String getSocialMediaID(String websiteName) { return ""; // replace this line with your own code }
/** Print the person's contact details in the format given in the * project specifications. */ public void printContactDetails() { // replace this line with your own code }
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. Complete the AddressBook class:
Create a constructor for the class, which initializes the contacts field to an empty list.
Complete the addPerson method, which adds a Person object to the contacts list, unless they have the same surname and first name as a person already in the list in which case, the method should not add them, but should instead print the message could not add person.
Complete the findPerson method, which searches for a person in the contacts list using their first name and surname. If a Person object with the specified first name and surname is found in the list, the method should return it; if not, the method should return null.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList; /** * Manage an AddressBook of Person contacts * */ public class AddressBook {
private ArrayList
// replace this line with your code for a constructor
/** Add the person p to the "contacts" list, unless they have the same * surname and first name as a person already in the list, in which case * do not add them, but instead print the error message "could not add person". * */ public void addPerson(Person p) { // replace this line with your code }
/** Search for a person in the "contacts" list by first name and surname, * and return the relevant Person object if one matches, or otherwise return null. */ public Person findPerson(String firstName, String surname) { return null; // replace this line with your code } /** * Find the most social person in the address book. */ public Person findMostSocial() { return null; //replace this line with your code (if any) here } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
