我必须为学校项目制作一本地址簿。我得到了基本功能,但后来我们被问到以下内容。
"使用get和set方法,创建比较方法compareNames(name1,name2),比较字符串name1和name2的名字,中间名和姓。假设name1和name2遵循以下格式:“FirstName M. LastName”。"
我有一个Data类:
public class Data {
private String firstName = null;
private String middleName = null;
private String lastName = null;
private String homeAddress = null;
private String businessPhone = null;
private String homePhone = null;
private String cellPhone = null;
private String skypeId = null;
private String facebookId = null;
private String personalWebSite = null;
public Data(String firstName, String middleName, String lastName, String homeAddress,
String businessPhone, String homePhone, String cellPhone, String skypeId,
String facebookId, String personalWebSite) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.businessPhone = businessPhone;
this.homePhone = homePhone;
this.cellPhone = cellPhone;
this.skypeId = skypeId;
this.facebookId = facebookId;
this.personalWebSite = personalWebSite;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getmiddleName() {
return middleName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String gethomeAddress() {
return homeAddress;
}
public void sethomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getbusinessPhone() {
return businessPhone;
}
public void setbusinessPhone(String businessPhone) {
this.businessPhone = businessPhone;
}
public String gethomePhone() {
return homePhone;
}
public void sethomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getcellPhone() {
return cellPhone;
}
public void setcellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
public String getskypeId() {
return skypeId;
}
public void setskypeId(String skypeId) {
this.skypeId = skypeId;
}
public String getfacebookId() {
return facebookId;
}
public void setfacebookId(String facebookId) {
this.facebookId = facebookId;
}
public String toString() {
return String.format(firstName+" "+middleName+" "+lastName+" "+homeAddress+" "
+" "+businessPhone+" "+homePhone+" "+cellPhone+" "+skypeId+" "
+facebookId+" "+personalWebSite);
}
}
和一个AddressBook类:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class AddressBook {
private static List<Data> contact = new ArrayList<Data>();
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
Scanner sc = new Scanner(System.in);
int menu;
String choice;
String name1;
String name2;
String name3;
System.out.println(" 0. Exit. ");
System.out.println(" 1. Add contact. ");
System.out.println(" 2. Edit contact. ");
System.out.println(" 3. Compare contact ");
System.out.println(" 4. Outprint contact. ");
System.out.println(" 5. Outprint all contacts. ");
System.out.println(" 6. Delete contact. ");
menu = sc.nextInt();
while (menu != 0) {
switch (menu) {
case 1:
while (menu != 2) {
System.out.println("Enter First Name: ");
String firstName = sc.next();
System.out.println("Enter Middle Name: ");
String middleName = sc.next();
System.out.println("Enter Last Name: ");
String lastName = sc.next();
System.out.println("Enter Home Address: ");
String homeAddress = sc.next();
System.out.println("Enter Business Phone: ");
String businessPhone = sc.next();
System.out.println("Enter Home Phone: ");
String homePhone = sc.next();
System.out.println("Enter Cell Phone: ");
String cellPhone = sc.next();
System.out.println("Enter Skype ID: ");
String skypeId = sc.next();
System.out.println("Enter Facebook ID: ");
String facebookId = sc.next();
System.out.println("Enter Personal Web Site: ");
String personalWebSite = sc.next();
contact.add(new Data(firstName, middleName, lastName,
homeAddress, businessPhone, homePhone, cellPhone,
skypeId, facebookId, personalWebSite));// Creating a
// new
// object
// and
// adding it
// to list
System.out
.println("Would you like to add someone else? 1: Yes, 2: No");
menu = sc.nextInt();
}
break;
case 2:
System.out
.println("Enter First Name of contact that you would like to edit: ");
choice = sc.next();
addressBook.deleteByFirstName(choice);
System.out.println("Enter First Name: ");
String firstName = sc.next();
System.out.println("Enter Middle Name: ");
String middleName = sc.next();
System.out.println("Enter Last Name: ");
String lastName = sc.next();
System.out.println("Enter Home Address: ");
String homeAddress = sc.next();
System.out.println("Enter Business Phone: ");
String businessPhone = sc.next();
System.out.println("Enter Home Phone: ");
String homePhone = sc.next();
System.out.println("Enter Cell Phone: ");
String cellPhone = sc.next();
System.out.println("Enter Skype ID: ");
String skypeId = sc.next();
System.out.println("Enter Facebook ID: ");
String facebookId = sc.next();
System.out.println("Enter Personal Web Site: ");
String personalWebSite = sc.next();
contact.add(new Data(firstName, middleName, lastName,
homeAddress, businessPhone, homePhone, cellPhone,
skypeId, facebookId, personalWebSite));
break;
case 3:
System.out
.println("Enter First Name of the first contact that you would like to compare: ");
name1 = sc.next();
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
Data temp = iterator.next();
System.out
.println("Enter First Name of the first contact that you would like to compare: ");
name2 = sc.next();
for (Iterator<Data> iterator2 = contact.iterator(); iterator2.hasNext();) {
Data temp2 = iterator2.next();
System.out.println(temp);
System.out.println(temp2);
if (name1.compareTo(name2) == 0) {
System.out.println("The strings are equal.");
} else if (name1.compareTo(name2) < 0) {
System.out.println("name2 follows name1");
} else {
System.out.println("name1 follows name2");
}
return;
}
System.out.println("No contact with first name " + name1
+ " and/or " + name2 + " was found.");
}
break;
case 4:
System.out
.println("Enter First Name of contact that you would like to print: ");
choice = sc.next();
addressBook.searchByFirstName(choice);
break;
case 5:
System.out.println("This is a list of every contact");
System.out.println(addressBook.contact);
break;
case 6:
System.out
.println("Enter First Name of contact that you would like to delete: ");
choice = sc.next();
addressBook.deleteByFirstName(choice);
break;
}
System.out.println(" 0. Exit. ");
System.out.println(" 1. Add contact. ");
System.out.println(" 2. Edit contact. ");
System.out.println(" 3. Compare contact ");
System.out.println(" 4. Outprint contact. ");
System.out.println(" 5. Outprint all contacts. ");
System.out.println(" 6. Delete contact. ");
menu = sc.nextInt();
}
System.out.println("Have a nice day!");
}
private void addData(String firstName, String middleName, String lastName,
String homeAddress, String businessPhone, String homePhone,
String cellPhone, String skypeId, String facebookId,
String personalWebSite) {
Data person = new Data(firstName, middleName, lastName, homeAddress,
businessPhone, homePhone, cellPhone, skypeId, facebookId,
personalWebSite);
contact.add(person);
}
private void searchByFirstName(String firstName) {
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
Data temp = iterator.next();
if (temp.getFirstName().equalsIgnoreCase(firstName)) {
System.out.println(temp);
return;
}
}
System.out.println("No contact with first name " + firstName
+ " was found.");
}
private void deleteByFirstName(String firstName) {
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
Data temp = iterator.next();
// Add null checks for proper error handling.
if (temp.getFirstName().equalsIgnoreCase(firstName)) {
iterator.remove();
return;
}
}
System.out.println("No contact with first name " + firstName
+ " was found.");
}
}
答案 0 :(得分:0)
只需从Data
实例解析指定格式的名称,并以您应该做的任何方式比较这两个字符串。
public String formatForComparison(Data data){
String result = "";
result += data.getFirstName() + " ";
result += data.getMiddleName().charAt(0) + ". ";
result += data.getLastName();
return result;
}
注意:只有在给出至少一个字符的第二个名称总是时,这才有效。