if items are added into an arraylist would another arraylist be required for grouping items or would a hashmap or something else be required i.e. title, firstname, last name etc saved indvidually but therefore grouped to equal person 1 and so on ? the reason for grouping is to edit the grouped items from the arraylist individually or all.

Edited 5 Years Ago by andyhunter: n/a

You can use

Collections.sort(yourArrayList);

But there is some work you need to do. You have to implement Comparable interface in your class. There will be a method called compareTo (Object o) which you need to override. Here you are free to code how you should sort (using first name or last name or title), such that it should return an integer. Your sort will depend on whether you return a -ve, +ve or zero integer.

Any other clarification required, just post.

In OOAD grouping of related attributes (e.g. FirstName, LastName, Title,..) is achieved by creating a class (say Person) and adding "FirstName, LastName, Title" as attributes/member-variables to the class. Now each instance of this class would have all the attributes.
What you then do is create an ArrayList of Person.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/** name of the project which is scrubshospitalapp
 *
 */
package scrubshospitalapp;
/** required to get input from the keyboard
 *
 */

import java.util.Scanner;

/**
 *
 * @author hun10009554
 */
/** class called person
 *
 *
 */
//start of overall module
public class Person {
/** declaring private variables that are all type String for title, surname,
 * gender, contactnum, dob
 *
 */
    private String title;
    private String forename;
    private String surname;
    private String gender;
    private String contactNum;
    private String dob;

/** declare and initialise inputdetails from the scanner to get user input
 * from keyboard
 *
 */
    Scanner inputDetails = new Scanner (System.in);

/** creating default person
 * 
 */
    public Person(){
        
    }


    /**Method to display output to screen i.e. title, forename, surname, gender,
     * contact number, dob which inputs the revelant information in i.e. title,
     * forename, surname, gender, contact number, dob which equals already
     * declared and intial variables
     */
    public void createPerson(){

        System.out.println("Please enter your title ?");
        title = inputDetails.nextLine();
        this.valTitle();
        
        System.out.println("Please enter your forename ?");
        forename = inputDetails.nextLine();
        this.valForename();
        
        System.out.println("Please enter your surname ?");
        surname = inputDetails.nextLine();
        this.valSurname();
        
        System.out.println("Please enter your gender ?");
        gender = inputDetails.nextLine();
        this.valGender();
        
        System.out.println("Please enter your contact number ?");
        contactNum = inputDetails.nextLine();
        this.valContactNum();
        
        System.out.println("Please enter your date of birth ?");
        dob = inputDetails.nextLine();
        this.valDob();
    }
    
    /**method to validate the title to make sure it is only contains characters
     * if title is null, numbers, not characters a-z upper or lower case or
     * just spaces then piece of code loops until correct, has to be correct
     * length i.e. greater than 2 and less than 4 in size, if not correct then
     * displays output,asks for input which title = inputDetails
     * 
     */
    public void valTitle(){
    //validate title
    while (this.title.matches("^[\\s]+$")
                                    ||this.title.matches("")
                                    ||this.title.matches("^![^M[^r]]$^![^M[^r][^s]]$")
                                    ){
                                System.out.println("Title can not"
                                        + " be empty or have spaces, must contain "
                                        + "a-z characters and has to be correct "
                                        + "length in size i.e. Mr or Miss");
                                System.out.println("Enter your title ?");
                                 title = inputDetails.nextLine();
        }
    }
     /**method to validate the forename to make sure it is only contains characters
     * if title is null, numbers, not characters a-z upper or lower case or
     * just spaces then piece of code loops until correct, must be correct length
      * in size i.e. greater than 3 and less than 10, if not correct then
     * displays output,asks for input which forename = inputDetails
     *
     */
    public void valForename(){
    while (this.forename.matches("^[\\s]+$")
                                    ||this.forename.matches("")
                                    ||this.forename.matches("^[0-9]+$")
                                    ||this.forename.matches("^[^A-Z^a-z]+$")
                                    ||this.forename.length()<3
                                    ||this.forename.length()>10
                                    ){
                                System.out.println("Forename can not"
                                        + " be empty or have spaces, must contain "
                                        + "a-z characters and must be correct "
                                        + "length in size");
                                System.out.println("Enter your forename ?");
                                 forename = inputDetails.nextLine();
        }
    }
     /**method to validate the surname to make sure it is only contains characters
     * if title is null, numbers, not characters a-z upper or lower case or
     * just spaces then piece of code loops until correct, must be correct
      * length in size i.e. greater than 4 and less than 15, if not correct then
     * displays output,asks for input which surname = inputDetails
     *
     */
    public void valSurname(){
    while (this.surname.matches("^[\\s]+$")
                                    ||this.surname.matches("")
                                    ||this.surname.matches("^[0-9]+$")
                                    ||this.surname.matches("^[^A-Z^a-z]+$")
                                    ||this.surname.length()>15
                                    ||this.surname.length()<4
                                    ){
                                System.out.println("Surname can not"
                                        + " be empty or have spaces, must contain "
                                        + "a-z characters and must be correct "
                                        + "length in size");
                                System.out.println("Enter your surname ?");
                                 surname = inputDetails.nextLine();
        }
    }
    /**method to validate the gender to make sure it is only contains characters
     * if title is null, numbers, not characters a-z upper or lower case or
     * just spaces then piece of code loops until correct, length must be less
     * than 1, if not correct then displays output,asks for input which
     * gender = inputDetails
     *
     */
    public void valGender(){
    while (this.gender.matches("^[\\s]+$")
                                    ||this.gender.matches("")
                                    ||this.gender.matches("^[^m][^f]+$")
                                    ){
                                System.out.println("Gender can not"
                                        + " be empty or have spaces, must be "
                                        + "only m or f "
                                        + "i.e. m = male and f = female");
                                System.out.println("Enter your gender ?");
                                 gender = inputDetails.nextLine();
        }
    }
    /**method to validate the contact number to make sure it is only 0-9
     * characters and not a-z characters upper or lower case and not null or
     * spaces,loop exits when valid data entered. length must be greater than 10
     * and less than 13 in size. Displays output until correct
     * data entered asking user to enter correct data and takes input from
     * contactNum = inputDetails.
     *
     */
    public void valContactNum(){
    while (this.contactNum.matches("^[^0-9]+$")
                                ||this.contactNum.matches("^[\\s]+$")
                                ||this.contactNum.matches("")
                                ||this.contactNum.length()<10
                                ||this.contactNum.length()>13
                                ){
                            System.out.println("Please enter a correct contact "
                                    + "number using 0-9 characters i.e. "
                                    + "full area code plus landline number or "
                                    + "else mobile number with no spaces");
                            System.out.println("Enter your contact number ?");
                            contactNum = inputDetails.nextLine();
        }
    }
    /**method to validate the dob to make sure it is only 0-9
     * characters and not a-z characters upper or lower case and not null or
     * spaces,loop exits when valid data entered.length must be 6 in size.
     * Displays output until correct
     * data entered asking user to enter correct data and takes input from
     * dob = inputDetails.
     *
     */
    public void valDob(){
         while (this.dob.matches("^[^1-3][^1-3^][^/][^1-3][^1-3][^/][^0-9][^0-9]+$")
                                ||this.dob.matches("^[\\s]+$")
                                ||this.dob.matches("")

                                ){
                            System.out.println("Please enter a correct date of "
                                    + "birth numbers using 0-9 characters");
                            System.out.println("Enter your date of birth ?");
                            dob = inputDetails.nextLine();
        }
    }

    
/**method to display on screen from input from keyboard i.e. title,
 * forename, surname, gender, contact number, dob
 *
 */
    public void printAll(){
        System.out.println("Title : "+this.title);
        System.out.println("Forename : "+this.forename);
        System.out.println("Surname : "+this.surname);
        System.out.println("Gender : "+this.gender);
        System.out.println("Contact Number : "+this.contactNum);
        System.out.println("Date of Birth : "+this.dob);

        
    }


public static void main (String []args){
    //unit testing creating a new person
    Person newPerson = new Person();
    //creating person
    newPerson.createPerson();
    //displaying input
    newPerson.printAll();

        


}//end of main

}//end of overall module

basically i am going to try to add the create person into an arraylist i think but how would that work with indexing as i would want i.e. person[0] for first person and then person[1] for second person and so on :S?

Edited 5 Years Ago by andyhunter: n/a

Simply add "newPerson" to your ArrayList.

List<Person> people = new ArrayList<Person>();
people.add(newPerson);

You can retrieve them with get()

people.get(0);  // returns first person in list
This article has been dead for over six months. Start a new discussion instead.