Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Directory{//This class contains my main methods
- public static void main(String [] args){
- Population p = new Population();
- p.get();
- p.put();
- }
- }
- class Population{//this class contains the array of type Person
- private int numPeople=0;
- private final int maxNumNames = 1000;
- private Person[] register = new Person[maxNumNames];
- void get(){//this enters the information from the console in to the array of people
- while (!Console.EndOfFile()) {
- register[numPeople] = new Person();
- register[numPeople].getPerson();
- numPeople++;
- }
- sort();
- }
- void sort() { // sorts the arrays alphabetically
- int lft = 0;
- while (lft<numPeople) {
- int min = lft; int i = lft+1;
- while (i<numPeople){
- if (register[i].compare(register[min])) min = i;
- i++;
- }
- Person temp = register[lft];
- register[lft] = register[min]; register[min] = temp;
- lft++;
- }
- }
- void put(){//this is printing out the array elements that have been filled
- for(int i = 0; i<numPeople; i++){
- register[i].putPerson();
- }
- }
- class Person{
- private String forename;
- private String surname;
- private String number;
- void getPerson(){//method to enter in one person
- forename = Console.readToken();
- surname = Console.readToken();
- number = Console.readToken();
- }
- void putPerson() { // formatted print method
- System.out.printf("%-10s%-10s%-10s\n", surname + " ", forename+ " ", number);
- }
- boolean compare(Person p){// compares array objects with each other as part of sort method
- return (surname.compareTo(p.surname)<0 || surname.equals(p.surname) && forename.compareTo(p.forename)<0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment