main需要按照用户输入的contacts,来获取多少次用户的信息(姓名,电话和邮箱);privatestaticContact[]readContacts(){Contact[]result;intnumContacts;Stringname,phone,email;Sys...
main需要按照用户输入的contacts,来获取多少次用户的信息(姓名,电话和邮箱);
private static Contact[] readContacts() {
Contact[] result;
int numContacts;
String name, phone, email;
System.out.println();
System.out.print("How many contacts do you have? ");
numContacts = KBD.nextInt();
KBD.nextLine();
System.out.println();
// create the array
result = new Contact[numContacts];
// Let the user know what they need to do
System.out.println("For each contact, enter their name.");
System.out.println("Optionally add a phone or email.");
// ACTIVITY 1: read in all the contacts' information
System.out.println();
System.out.print("Enter a contact's name: ");
System.out.print("Enter the contact's phone number (optional): ");
System.out.print("Enter the contact's e-mail address (optional): ");
// return the result
return result;
}
下面是class:
public Contact(String reqName, String reqPhone, String reqEmail) {
if ("".equals(reqName)) {
throw new IllegalArgumentException(
"Cannot create a contact with no name!");
}
name = reqName;
phone = reqPhone;
email = reqEmail;
}
// ----- getters --------------------------------------------------- //
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
// ----- setters --------------------------------------------------- //
public void setName(String req) {
name = req;
}
public void setPhone(String req) {
phone = req;
}
public void setEmail(String req) {
email = req;
}
展开