2015-12-26 92 views
-2

這不是家庭作業,是我在書中遇到的練習。 構建一個名爲Name的類,它表示一個person的名稱。該類應該具有表示名字,姓氏和父名的字段。 類應該有以下方法:Java字符串排序

public Name (String fn,String f_n,String ln) 
    /* initializes the fields of an object with the values fn,f_n and m. 
     fn means first name 
     ln means last name 
     f_n means fathersname btw. */ 

public String getNormalOrder(); //returns the name of the person in the normal order : first name,fathers name,last name. 

public String getReverseOrder(); //returns the name of the person in the reverse order : last name,fathers name,first name. 

public boolean compare (String fn,String f_n,String ln); // Returns true if the first name is the same with fn,fathers name is the same with f_n, last name with ln.If the opposite happens it returns false. 

建立了一個名爲測試名程序,測試類名字的方法。

我的解決方案

public class Name { 
    String fn; 
    String f_n; 
    String ln; 

    public Name(String initialfn, String initialf_n, String initialln) { 
     fn = initialfn; 
     f_n = initialf_n; 
     ln = initialln; 
    } 

    public String getNormalOrder() { 
     return fn + " " + f_n + 
      " " + ln; 
    } 

    public String getReverseOrder() { 
     return ln + ", " + f_n + 
      " " + fn + " "; 
    } 
} 

如何第三種方法被比較?我該如何測試課程?

回答

1

對於靈活的解決方案:

public enum NameMember { 
    FIRSTNAME, SECONDNAME, FATHERSNAME; 
} 

Name類:

public class Name { 
    private final String firstName; 
    private final String secondName; 
    private final String fathersName; 

    public Name(String firstName, String secondName, String fathersName) { 
     this.firstName = firstName; 
     this.secondName = secondName; 
     this.fathersName = fathersName; 
    } 

    public String getName(NameMember member1, NameMember member2, NameMember member3) { 
     StringBuilder sb = new StringBuilder(); 
     return sb.append(getMember(member1)).append(" ") 
       .append(getMember(member2)).append(" ") 
       .append(getMember(member3)).toString(); 
    } 

    public String getMember(NameMember member) { 
     switch (member) { 
      case FIRSTNAME: 
       return firstName; 
      case SECONDNAME: 
       return secondName; 
      case FATHERSNAME: 
       return fathersName; 
      default: 
       return null; 
     } 
    } 

    @Override 
    public String toString() { 
     return getName(NameMember.FIRSTNAME, NameMember.SECONDNAME, NameMember.FATHERSNAME); 
    } 
} 

NameComparator(柔性)類:

import java.util.Comparator; 

public class NameComparator implements Comparator<Name> { 

    private NameMember nameMember; 

    public NameComparator(NameMember nameMember) { 
     this.nameMember = nameMember; 
    } 

    @Override 
    public int compare(Name name1, Name name2) { 
     return name1.getMember(nameMember).compareTo(name2.getMember(nameMember)); 
    } 
} 

和主類(測試驅動器):

public static void main(String args[]) { 
    List<Name> names = new ArrayList<>(); 
    names.add(new Name("Alice", "Burda", "Christophe")); 
    names.add(new Name("Ben", "Ashton", "Caine")); 
    names.add(new Name("Chane", "Bagwell", "Alex")); 
    names.add(new Name("Ann", "Clinton", "Brad")); 

    System.out.println("NAMES ORDERED BY FIRST NAME:"); 
    Collections.sort(names, new NameComparator(NameMember.FIRSTNAME)); 
    printNames(names); 

    System.out.println("\nNAMES ORDERED BY SECOND NAME:"); 
    Collections.sort(names, new NameComparator(NameMember.SECONDNAME)); 
    printNames(names); 

    System.out.println("\nNAMES ORDERED BY FATHERSNAME:"); 
    Collections.sort(names, new NameComparator(NameMember.FATHERSNAME)); 
    printNames(names); 
} 

private static void printNames(Collection<Name> names) { 
    names.stream().forEach(System.out::println); 
}