-1

i have gotten the assignment to make a phonebook with 3 classes, the driver that runs it all, phonebook,and a person class.

the problem i was having was i couldn't make the Collection.sort(telbook.personen) get to work as how i have it in my code, what i want to know is what do i have to add or replace to make it sort the arraylist as i have it now as a function that i can run by myself to check if it did sort, but that didn't work.

driver class:

        /**
 * Created by ricardo on 2/26/2015.
 */

import java.util.*;

public class Driver {
    Phonebook telbook = new Phonebook();
    Scanner scan = new Scanner(System.in);
    String newLine = System.getProperty("line.separator");
    String[] Commands = {"/addperson - add a person to my beautiful program",
            "/listpersons - for full list of persons",
            "/removeperson - to remove a made person",
            "/sortlist - sorts the phonebook (alphabetically)"};
    private boolean running;
    private boolean startmessage = false;

public static void main(String[] args) {

    Driver n = new Driver();
    n.run();

}

public void run() {
    running = true;
    startProgram();
}

public void startProgram() {
    while (running) {
        if (!startmessage) {
            System.out.println("Type /commands for all available commands.");
            startmessage = true;
        }
        String entered = scan.nextLine();
        if (entered.equals("/commands")) {
            for (int i = 0; i < Commands.length; i++)
                System.out.println(Commands[i]);
        } else if (entered.equals("/addperson")) {
            addPerson();
        } else if (entered.equals("/listpersons")) {
            listPersons();
        } else if (entered.equals("/removeperson")) {
            removePerson();
        } else if (entered.equals("/sortlist")) {
            sortList();
        } else {
            System.out.println("Command not available. Type /commands for full list of commands");
        }
    }
}

public void addPerson() {
    System.out.println("Fill in your name");
    String addname = scan.nextLine();
    System.out.println("Fill in your adress");
    String addadress = scan.nextLine();
    System.out.println("Fill in your city");
    String addcity = scan.nextLine();
    System.out.println("Fill in your phonenumber");
    String addphonenumber = scan.nextLine();
    System.out.println("Your data has been saved!");
    Person addperson = new Person(addname, addadress, addphonenumber, addcity);
    telbook.personen.add(addperson);
    //sortList();


}

public void removePerson() {
    listPersons();
    System.out.println("Insert the ID of the person to be removed");
    int ID = Integer.parseInt(scan.nextLine());
    if (ID > telbook.personen.size()) {
        System.out.println("There is no person with this ID, please select a different ID");
        removePerson();

    } else {
        telbook.personen.remove(ID);
        System.out.println("Person with the ID of " + ID + " has been removed");
    }
}

public void listPersons() {
    int ID = 0;
    if (telbook.personen.isEmpty()) {
        System.out.println("There is no person added yet. type /addperson to do so");
    }
    for (int i = 0; i < telbook.personen.size(); i++) {
        System.out.println("ID:" + ID + newLine + " name: " + telbook.personen.get(i).name + newLine + " adress: " + telbook.personen.get(i).adress + newLine + " city: " + telbook.personen.get(i).city + newLine + " phonenumber: " + telbook.personen.get(i).phonenumber);
        ID++;


    }


}

public void sortList() {
    Collections.sort(telbook.personen);
}

}

phonebook class:

        /**
 * Created by ricardo on 2/26/2015.
 */

    import java.util.*;

    public class Phonebook {
        ArrayList<Person> personen = new ArrayList<Person>();


    }

person class:

       /**
 * Created by ricardo on 2/26/2015.
 */
public class Person {
    String name, adress, phonenumber, city;

    public Person(String name, String adress, String phonenumber, String city) {
        this.name = name;
        this.adress = adress;
        this.city = city;
        this.phonenumber = phonenumber;
    }

//    public String getCity() { return city; }
//
//    public void setCity(String city) {
//        this.city = city;
//    }
//
//    public String getName() {
//        return name;
//    }
//
//    public void setName(String name) {
//        this.name = name;
//    }
//
//    public String getAdress() {
//        return adress;
//    }
//
//    public void setAdress(String adress) {
//        this.adress = adress;
//    }
//
//    public String getPhonenumber() {
//        return phonenumber;
//    }
//
//    public void setPhonenumber(String phonenumber) {
//        this.phonenumber = phonenumber;
//    }
}
  • An assignment (i.e. homework) implicates self-dependent work. Please, do not litter SO with such specific and useless questions – Andremoniy Mar 04 '15 at 09:45
  • And this is why school is next to useless. What real-world scenario would limit you to 3 classes? School seems to bury creativity somewhere deep down scorched earth. Anyway: http://stackoverflow.com/questions/1206073/sorting-a-collection-of-objects – Stephan Bijzitter Mar 04 '15 at 09:46
  • +ricardo spek you should delete this question once you got the answer. – Vishnudev K Mar 04 '15 at 09:52

1 Answers1

0

You should make your Person class implement the Comparable interface, and specifically tell how one should compare two Person objects.

An alternative is to implement a Comparator, and use Collections.sort(arrayList,comparator)

amit
  • 175,853
  • 27
  • 231
  • 333
  • I'm not the downvoter, but I'm afraid it is because you are encouraging such stupid questions by your clever answers. – Andremoniy Mar 04 '15 at 09:49
  • 1
    @Andremoniy (1) There are no "stupid questions", there is nothing wrong in not being a java expert, that is not familiar with `Comparator` (the question does need some tweaks however). (2) I see no harm in telling a person what he SHOULD do, without giving him the code. It encourages learning. – amit Mar 04 '15 at 09:50
  • But I think (as the teacher with 7 years of teaching experience) that such assigments each student have to do self-dependently. Furthemore such questions are deeply useless for other readers of SO because it is very specifically. – Andremoniy Mar 04 '15 at 09:52
  • that's the reason im asking it, it is specific and therefore no other already asked questions helped – ricardo spek Mar 04 '15 at 09:56