/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
import java.util.Calendar;
import java.util.ArrayList;
import java.util.Random;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
String[] names = {
"Alice",
"Bob",
"Candy",
"Danel",
"Frank",
"Grant",
"Harry",
"Iris",
"Joe",
"Kevin" };
ArrayList<Person> people = new ArrayList<Person>();
for(String name:names){
Calendar cal = Calendar.getInstance();
int birthYear = (int)Math.floor(Math.random() * 100) + 1;
cal.set(birthYear, 1 /* mouth */, 1 /* date */);
Person p = new Person(name, /* birthday = */cal, /* addr = */"");
people.add(p);
}
System.out.println(people);
Collections.sort(people);
System.out.println();
System.out.println(people);
return;
}
}
class Person implements Comparable<Person> {
public String name;
private Calendar birthday;
public String addr;
/* constructor */
public Person(String name, Calendar birthday, String addr) {
this.name = name;
this.birthday = birthday;
this.addr = addr;
}
int getAge() {
Calendar today = Calendar.getInstance();
int todayYear = today.get(Calendar.YEAR);
int birthdayYear = birthday.get(Calendar.YEAR);
return todayYear - birthdayYear;
}
public int compareTo(Person other) {
int age = this.getAge();
int otherAge = other.getAge();
if (age == otherAge)
return 0;
return (this.getAge() > other.getAge()) ? 1 : -1;
}
public String toString() {
return "Person(name="+name+", age="+getAge()+", addr="+addr+")";
}
}