Complete the Person class: For setEmail, setFirstName, and setSurname: if the me
ID: 3812299 • Letter: C
Question
Complete the Person class:
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 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:
Calling the printContactDetails method on the object p should print the following:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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 socialMediaAccounts;
/**
* Create a Person with the first name and surname given by
* the parameters.
*/
public Person(String firstName, String surname) {
this.firstName = firstName;
this.surname = surname;
mobile = null;
email = null;
this.socialMediaAccounts = new 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
}
}
Explanation / Answer
package com.chegg;
public class SocialMediaAccount {
private String userID;
private String websiteName;
private String websiteURL;
private int activityLevel;
public SocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) {
super();
this.userID = userID;
this.websiteName = websiteName;
this.websiteURL = websiteURL;
this.activityLevel = activityLevel;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getWebsiteName() {
return websiteName;
}
public void setWebsiteName(String websiteName) {
this.websiteName = websiteName;
}
public String getWebsiteURL() {
return websiteURL;
}
public void setWebsiteURL(String websiteURL) {
this.websiteURL = websiteURL;
}
public int getActivityLevel() {
return activityLevel;
}
public void setActivityLevel(int activityLevel) {
this.activityLevel = activityLevel;
}
}
package com.chegg;
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<SocialMediaAccount> socialMediaAccounts;
/**
* Create a Person with the first name and surname given by
* the parameters.
*/
public Person(String firstName, String surname) {
this.firstName = firstName;
this.surname = surname;
mobile = null;
email = null;
this.socialMediaAccounts = new ArrayList<SocialMediaAccount>();
}
/**
* @return the person's first name
*/
public String getFirstName() {
return this.firstName;
}
/**
* Set the person's first name
* unless the parameter is an empty string.
*/
public void setFirstName(String firstName) {
if(!firstName.equals("")){
this.firstName=firstName;
}
}
/**
* Return the person's surname
*/
public String getSurname() {
return this.surname;
}
/**
* Set the person's surname
* unless the parameter is an empty string.
*/
public void setSurname(String surname) {
if(!surname.equals("")){
this.surname = surname;
}
}
/**
* Return the person's mobile phone number
*/
public String getMobile() {
return this.mobile;
}
/**
* Set the person's mobile phone number
*/
public void setMobile(String mobile) {
boolean isValid = true;
for(char ch:mobile.toCharArray()){
if(!Character.isDigit(ch)){
isValid = false;
}
}
if(isValid)
this.mobile = mobile;
}
/**
* Return the person's email address
*/
public String getEmail() {
return this.email;
}
/**
* Set the person's email address
*/
public void setEmail(String email) {
if(!email.equals("")){
this.email = email;
}
}
/**
* Create a new SocialMediaAccount object, and add it to the socialMediaAccounts list.
*/
public void addSocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) {
SocialMediaAccount socialMediaAccount = new SocialMediaAccount(userID, websiteName, websiteURL, activityLevel);
socialMediaAccounts.add(socialMediaAccount);
}
/**
* 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) {
String userId = null;
for(SocialMediaAccount acccount:socialMediaAccounts){
if(acccount.getWebsiteName().equals(websiteName)){
userId = acccount.getUserID();
break;
}
}
return userId;
}
/** Print the person's contact details in the format given in the
* project specifications.
*/
public void printContactDetails() {
System.out.println("name: "+getFirstName()+" "+getSurname());
System.out.println("mobile: "+getMobile());
System.out.println("email: "+getEmail());
for(SocialMediaAccount acccount:socialMediaAccounts){
System.out.println(acccount.getWebsiteName()+","+acccount.getUserID()+","+acccount.getWebsiteURL());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.