ADoyle4

Directory.java

Apr 4th, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. class Directory{//This class contains my main methods
  2. public static void main(String [] args){
  3. Population p = new Population();
  4. p.get();
  5. p.put();
  6. }
  7. }
  8. class Population{//this class contains the array of type Person
  9.  
  10. private int numPeople=0;
  11. private final int maxNumNames = 1000;
  12. private Person[] register = new Person[maxNumNames];
  13.  
  14. void get(){//this enters the information from the console in to the array of people
  15. while (!Console.EndOfFile()) {
  16. register[numPeople] = new Person();
  17. register[numPeople].getPerson();
  18. numPeople++;
  19. }
  20. sort();
  21. }
  22.  
  23. void sort() { // sorts the arrays alphabetically
  24.  
  25. int lft = 0;
  26. while (lft<numPeople) {
  27. int min = lft; int i = lft+1;
  28. while (i<numPeople){
  29. if (register[i].compare(register[min])) min = i;
  30. i++;
  31. }
  32. Person temp = register[lft];
  33. register[lft] = register[min]; register[min] = temp;
  34. lft++;
  35. }
  36. }
  37.  
  38. void put(){//this is printing out the array elements that have been filled
  39. for(int i = 0; i<numPeople; i++){
  40. register[i].putPerson();
  41. }
  42.  
  43.  
  44. }
  45.  
  46. class Person{
  47. private String forename;
  48. private String surname;
  49. private String number;
  50.  
  51.  
  52. void getPerson(){//method to enter in one person
  53.  
  54. forename = Console.readToken();
  55. surname = Console.readToken();
  56. number = Console.readToken();
  57. }
  58.  
  59. void putPerson() { // formatted print method
  60.  
  61. System.out.printf("%-10s%-10s%-10s\n", surname + " ", forename+ " ", number);
  62. }
  63.  
  64. boolean compare(Person p){// compares array objects with each other as part of sort method
  65. return (surname.compareTo(p.surname)<0 || surname.equals(p.surname) && forename.compareTo(p.forename)<0);
  66. }
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment