題目及解答:策略模式+泛型(對Person類按姓名、id、年齡正序、倒序排序)
題目要求:
加入有若干個Person類物件存在一個List當中,對它們進行排序,分別按照名字、年齡、id號進行排序(要有正序和倒序兩種方式)。加入年齡或者姓名重複,按照id號的正序進行排序。要求使用策略模式和泛型進行。
程式如下:
1.Person類定義
package strategy.demo;
import java.util.*;
public class Person
{
private String name;
private int age;
private int id;
public Person(int id, String name, int age)
{
this.id = id;
this.name = name;
this.age = age;
}
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return this.id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
public String say()
{
return "學號:"+this.id +", 姓名:"+this.name+ ", 年齡:"+this.age;
}
}
2.按照姓名排序
①正序
package strategy.demo;
import java.util.*;
public class NameUpSort implements Strategy
{
public void sort(List<Person> list)
{
Collections.sort(list, new MyComparator6());
}
}
class MyComparator6 implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
if((p2.getName()).equals(p1.getName()))
{
return p1.getId() - p2.getId();
}
return (p1.getName()).compareTo(p2.getName());
}
}
②倒序
package strategy.demo;
import java.util.*;
public class NameDownSort implements Strategy
{
public void sort(List<Person> list)
{
Collections.sort(list, new MyComparator5());
}
}
class MyComparator5 implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
if((p2.getName()).equals(p1.getName()))
{
return p1.getId() - p2.getId();
}
return (p2.getName()).compareTo(p1.getName());
}
}
3.按照id排序
①正序
package strategy.demo;
import java.util.*;
public class IdUpSort implements Strategy
{
public void sort(List<Person> list)
{
Collections.sort(list, new MyComparator4());
}
}
class MyComparator4 implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
return p1.getId() - p2.getId();
}
}
②倒序
package strategy.demo;import java.util.*;
public class IdDownSort implements Strategy
{
public void sort(List<Person> list)
{
Collections.sort(list, new MyComparator3());
}
}
class MyComparator3 implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
return p2.getId() - p1.getId();
}
}
4.按照年齡排序
①正序
package strategy.demo;
import java.util.*;
public class AgeUpSort implements Strategy
{
public void sort(List<Person> list)
{
Collections.sort(list, new MyComparator2());
}
}
class MyComparator2 implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
if(p2.getAge() == p1.getAge())
{
return p1.getId() - p2.getId();
}
return p1.getAge() - p2.getAge();
}
}
②倒序
package strategy.demo;
import java.util.*;
public class AgeDownSort implements Strategy
{
public void sort(List<Person> list)
{
Collections.sort(list, new MyComparator1());
}
}
class MyComparator1 implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
if(p2.getAge() == p1.getAge())
{
return p1.getId() - p2.getId();
}
return p2.getAge() - p1.getAge();
}
}
5.Strategy介面定義
package strategy.demo;
import java.util.*;
interface Strategy
{
public void sort(List<Person> list);
}
6.Client類定義
package strategy.demo;
import java.util.*;
public class Client
{
public static void main(String[] args)
{
List<Person> ls = new ArrayList<Person>();
ls.add(new Person(1, "zhangsan", 20));
ls.add(new Person(2, "lisi", 21));
ls.add(new Person(3, "wangwu", 25));
ls.add(new Person(4, "zhaoliu", 26));
ls.add(new Person(5, "chenqi", 28));
ls.add(new Person(6, "dongba", 10));
ls.add(new Person(7, "zhansan", 15));
ls.add(new Person(8, "liujiu", 26));
System.out.println("-----------按姓名升序排序-----------");
Strategy str1 = new NameUpSort();
str1.sort(ls);
Client.print(ls);
System.out.println("-----------按姓名降序排序-----------");
Strategy str2 = new NameDownSort();
str2.sort(ls);
Client.print(ls);
System.out.println("-----------按id升序排序-----------");
Strategy str3 = new IdUpSort();
str3.sort(ls);
Client.print(ls);
System.out.println("-----------按id降序排序-----------");
Strategy str4 = new IdDownSort();
str4.sort(ls);
Client.print(ls);
System.out.println("-----------按年齡升序排序-----------");
Strategy str5 = new AgeUpSort();
str5.sort(ls);
Client.print(ls);
System.out.println("-----------按年齡降序排序-----------");
Strategy str6 = new AgeDownSort();
str6.sort(ls);
Client.print(ls);
}
public static void print(List<Person> ls)
{
for(int i = 0; i< ls.size(); i++)
{
Person p = ls.get(i);
System.out.println(p.say());
}
}
}
執行時:java strategy.demo.Client
結果: