Compare People
<< Gift Interface | OtherProjectsTrailIndex | Plinko >>
How can arrange people? by name? by zip code? By how may TV's they have? you can implement Comparable or Comparator interfaces to have it any way you want it! This is a simple example... Click here to see another example
Person.java
public class Person implements Comparable //the compareTo() method will tell how to compare people { private String first,last,birth,zip; private int pets; //Number of pets in their household public Person(String f, String l, String b, String z, int p){ first=f; last=l; birth=b; zip=z; pets=p; } public String toString(){ return first+" "+last+" "+birth+" "+zip+" "+pets; } public String getFirst(){ return first;} public String getLast(){ return last;} public String getBirth(){ return birth;} public String getZip(){ return zip;} public int getPets(){ return pets;} /** * here we implement the Comparable Interface * so people are compared based upon how many Pets they have */ public int compareTo(Object other) { return this.pets - ((Person)other).getPets(); } }
BirthComporator.java
import java.util.Comparator; public class BirthComparator implements Comparator<Person> { public int compare(Person p1, Person p2) { return p1.getBirth().compareTo(p2.getBirth()); } }
ComparePeople.java
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class ComparePeople { private static Person[] folks; /** * @param args */ public static void main(String[] args) { folks=new Person[3]; folks[2] = new Person("Chad", "Adams", "19981225", "90210",2); folks[1] = new Person("Arty", "Zim", "20151112", "85150",3); folks[0] = new Person("Brad", "Zim", "20020317", "91011",1); Arrays.sort(folks); Scanner kb = new Scanner(System.in); String command=""; while (!command.equals("Q")){ doCommand(command); System.out.print("Type a command: F)irst, L)ast, B)irthday, Z)ip P)ets Q)uit: "); command=kb.nextLine().substring(0,1).toUpperCase(); } } public static void doCommand(String cmd){ if(cmd.equals("F")){ System.out.println("Sorted by First Name:"); // use a first Name comparator than you make }else if(cmd.equals("L")){ System.out.println("Sorted by Last Name:"); //use a Last, then first name comparator tha you make }else if(cmd.equals("B")){ System.out.println("Sorted by Birth date:"); Arrays.sort(folks,new BirthComparator()); // Use a comparator provided }else if(cmd.equals("Z")){ System.out.println("Sorted by Zip Code:"); // use a zipcode comparator that you make }else if(cmd.equals("P")){ System.out.println("Sorted by number of Pets in Household:"); Arrays.sort(folks); // Use the Person class's compareTo method } for(Person p:folks) System.out.println(p); } }
Now you try
- Make the rest of the Comparators so that the
ComparePeople
application works properly (Make the default compare first by last name, then by first name, the way alphabetizing is usually done). - Add to the
Person
class aString
instance field calledjob
, and add necessary code to test a sort byjob