Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public class Person implements Comparable<Person>{
  2.  
  3. private String id;
  4. private String name;
  5. private int credits;
  6.  
  7. public Person(String i, String n, int c){
  8. id = i;
  9. name = n;
  10. credits = c;
  11. }
  12.  
  13. public String personInfo(){
  14. return String.format("%-5s %12s %5d",id,name,credits);
  15. }
  16.  
  17. public int compareTo(Person that) {
  18. return id.compareTo(that.id);
  19. }
  20.  
  21. public static void main(String[]args){
  22. System.out.println(new Person("A01","juan garcia",7).personInfo());
  23. }
  24. }`
  25.  
  26.  
  27. public class Persons extends ArrayList<Person> {
  28.  
  29. public void addPerson(String id, String name, int credits){
  30. super.add(new Person(id,name,credits));
  31. }
  32.  
  33. public void sortById(){
  34. Collections.sort(this);
  35. }
  36.  
  37. public void displayPersons() {
  38. for (Person p : this)
  39. System.out.println(p.personInfo());
  40. }
  41. }
  42.  
  43.  
  44. public class PersonsDemo {
  45. public static void main(String[] args){
  46.  
  47. Persons persons = new Persons();
  48. persons.addPerson("A05","Tran Quan",7);
  49. persons.addPerson("A03","Nguyen An",7);
  50. persons.addPerson("A01","Troung Phung",5);
  51. persons.addPerson("A04","Phan Thi Lam",2);
  52. persons.addPerson("A02","Do Trung Ton",5);
  53.  
  54. System.out.println("PERSONS UNSORTED");
  55. persons.displayPersons();
  56.  
  57. System.out.println("\nPERSONS SORTED");
  58. persons.sortById();
  59. persons.displayPersons();
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement