Skip to content

Instantly share code, notes, and snippets.

@MLLeKander
Last active August 29, 2015 14:19
Show Gist options
  • Save MLLeKander/14039a0a3ef61626e205 to your computer and use it in GitHub Desktop.
Save MLLeKander/14039a0a3ef61626e205 to your computer and use it in GitHub Desktop.
Bikeshedding
abstract class Person implements Comparable<Person> {
protected String lastName, firstName;
public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String fullName() { return firstName+" "+lastName; }
protected abstract int rank();
public compareTo(Person o) {
int r1 = this.rank(), r2 = o.rank();
return r1 != r2 ? r1-r2 : this.fullName().compareTo(o.fullName());
}
}
class Instructor extends Person {
protected int rank() { return 5; }
}
class TeachingAssistant extends Person {
protected int rank() { return 10; }
}
class Student extends Person {
protected int rank() { return 15; }
}
abstract class Person implements Comparable<Person> {
protected String lastName, firstName;
public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String fullName() { return firstName+" "+lastName; }
public compareTo(Person o) {
if (this.getClass() == o.getClass()) {
return this.fullName().compareTo(o.fullName());
} else if (this instanceof Instructor && (o instanceof TeachingAssistant || o instanceof Student)) {
return 1;
} else if (this instanceof TeachingAssistant && o instanceof Student) {
return 1;
}
return -1;
}
}
class Instructor extends Person {
}
class TeachingAssistant extends Person {
}
class Student extends Person {
}
abstract class Person implements Comparable<Person> {
protected String lastName, firstName;
public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String fullName() { return firstName+" "+lastName; }
public compareTo(Person o) {
return this.fullName().compareTo(o);
}
}
class Instructor extends Person {
public int compareTo(Person o) {
if (o instanceof TeachingAssistant || o instanceof Student) {
return 1;
}
return super.compareTo(o);
}
}
class TeachingAssistant extends Person {
public int compareTo(Person o) {
if (o instanceof Instructor) {
return -1;
} if (o instanceof Student) {
return 1;
}
return super.compareTo(o);
}
}
class Student extends Person {
public int compareTo(Person o) {
if (o instanceof Instructor || o instanceof TeachingAssistant) {
return -1;
}
return super.compareTo(o);
}
}
abstract class Person implements Comparable<Person> {
protected String lastName, firstName;
public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String fullName() { return firstName+" "+lastName; }
public compareTo(Person o) {
return this.fullName().compareTo(o);
}
}
class Instructor extends Person {
}
class TeachingAssistant extends Person {
}
class Student extends Person {
}
class Main {
public static void main(String[] args) {
Comparator<Person> cmp = new Comparator<Person>() {
public compare(Person a, Person b) {
if (a.getClass() == b.getClass()) {
return a.compareTo(b);
} else if (a instanceof Instructor && (b instanceof TeachingAssistant || b instanceof Student)) {
return 1;
} else if (a instanceof TeachingAssistant && b instanceof Student) {
return 1;
}
return -1;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment