Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. public class Student implements Comparable{
  2. private String fullName;
  3. private Date registrationDate;
  4.  
  5. public Person(String fullName){
  6. this.fullName = fullName;
  7. }
  8.  
  9. public String getFullName() {
  10. return fullName;
  11. }
  12.  
  13. public void setFullName(String fullName) {
  14. this.fullName = fullName;
  15. }
  16.  
  17. public Date getRegistrationDate() {
  18. return registrationDate;
  19. }
  20. public void setRegistrationDate(Date registrationDate) {
  21. this.registrationDate = registrationDate;
  22. }
  23.  
  24. @Override
  25. public int compareTo(Object obj) {
  26. Person person = (Person) obj;
  27.  
  28. if(person instanceof Staff){
  29. return 1;
  30. }else if(person instanceof Student){
  31. return -1;
  32. }
  33. else if(getRegistrationDate().before(person.getRegistrationDate())){
  34. return 1;
  35. }else if(getRegistrationDate().after(person.getRegistrationDate())){
  36. return -1;
  37. }
  38. return 0;
  39. }
  40. }
  41.  
  42. public class Staff extends Person{
  43. public Staff(String fullName){
  44. this.fullName = fullName;
  45. }
  46. }
  47.  
  48. public class Student extends Member{
  49. public Student(String fullName){
  50. this.fullName = fullName;
  51. }
  52. }
  53.  
  54. public class School {
  55.  
  56. public static void main(String args[]) {
  57.  
  58. //list of students
  59. Student student1 = new Student("John Kent");
  60. Date dateStudent1Joined = new GregorianCalendar(2014, Calendar.JULY, 1).getTime();
  61. student1.setRegistrationDate(dateStudent1Joined);
  62.  
  63. Student student2 = new Student("Peter Tush");
  64. Date dateStudent2Joined = new GregorianCalendar(2014, Calendar.JULY, 2).getTime();
  65. student2.setRegistrationDate(dateStudent2Joined);
  66.  
  67. Student student3 = new Student("Mike Monroe");
  68. Date dateStudent3Joined = new GregorianCalendar(2014, Calendar.JULY, 3).getTime();
  69. student3.setRegistrationDate(dateStudent3Joined);
  70.  
  71. Student student4 = new Student("Tom Johnson");
  72. Date dateStudent4Joined = new GregorianCalendar(2014, Calendar.JULY, 4).getTime();
  73. student4.setRegistrationDate(dateStudent4Joined);
  74.  
  75. Student student5 = new Student("Tony Spencer");
  76. Date dateStudent5Joined = new GregorianCalendar(2014, Calendar.JULY, 5).getTime();
  77. student5.setRegistrationDate(dateStudent5Joined);
  78.  
  79. //list of staff
  80. Staff staff1 = new Staff("Luke Clint");
  81. Date dateStaff1Joined = new GregorianCalendar(2014, Calendar.JULY, 6).getTime();
  82. staff1.setRegistrationDate(dateStaff1Joined);
  83.  
  84. Staff staff2 = new Staff("Ron West");
  85. Date dateStaff2Joined = new GregorianCalendar(2014, Calendar.JULY, 7).getTime();
  86. staff2.setRegistrationDate(dateStaff2Joined);
  87.  
  88. Staff staff3 = new Staff("Jim Gary");
  89. Date dateStaff3Joined = new GregorianCalendar(2014, Calendar.JULY, 8).getTime();
  90. staff3.setRegistrationDate(dateStaff3Joined);
  91.  
  92.  
  93. //create a queue data structure to hold Persons in school
  94. PriorityQueue<Person> schoolQueue = new PriorityQueue<Person>();
  95. //add students to queue
  96. schoolQueue.offer(student1);
  97. schoolQueue.offer(student2);
  98. schoolQueue.offer(student3);
  99. schoolQueue.offer(student4);
  100. schoolQueue.offer(student5);
  101. //add staff to queue
  102. schoolQueue.offer(staff1);
  103. schoolQueue.offer(staff2);
  104. schoolQueue.offer(staff3);
  105.  
  106. //print names of people in queue
  107. for(Member member : clubQueue){
  108. String memberName = member.getFullName();
  109. System.out.println(memberName);
  110. }
  111.  
  112. }
  113. }
  114.  
  115. @Override
  116. public int compareTo(Object obj) {
  117. Person person = (Person) obj;
  118.  
  119. if(person instanceof Staff){
  120. return 1;
  121. }else if(person instanceof Student){
  122. return -1;
  123. }
  124. else if(getRegistrationDate().before(person.getRegistrationDate())){
  125. return 1;
  126. }else if(getRegistrationDate().after(person.getRegistrationDate())){
  127. return -1;
  128. }
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement