Hi I need help with this program. I am trying to get it to store the person data into an array list and read from a file text but have not been able to figure out how to due it.

This are the instructions for the program:
Write an AddressBook class to search the address or the phone of your present friends and to also add new friends to your address book
Then write a Friend class. To do this, you can extend a Person class, which contains only the names of a person. Then you can extend the Person class to a Friend class to hold the additional data fields for the address and phone numbers. It would be also be best to create an Address class and then add an Address object data field to the Friend class.
The application should start by reading an input file ( 4 or 5 lines of data regarding friends is enough). Each line in the input file should have data about the full name of a friend, his/her full address and his/her phone numbers.
Once you read a complete record/line from an input file, create a Friend object for that record. Then choose and use one of the Java Collection classes discussed in Chapter 22 ( e.g. LinkedList, ArrayList) to create a list of Friend objects ( do not use an array for the list – you will lose 30 points).
Once you finish reading all the records from the input file, display all the Friend objects using the collection object.

//AddressBook class

public class AddressBook extends JFrame {

        //creates buttons
        JButton btAdd = new JButton("ADD");
        JButton btFirst = new JButton("First");
        JButton btLast = new JButton("Last");
        JButton btNext = new JButton("Next");
        JButton btPrevious = new JButton("Previous");
        JButton btSearch = new JButton("Search");
        JButton btExit = new JButton ("Exit");
        JButton btClear = new JButton ("Clear");
        JButton btUpdate = new JButton ("Update");

        //creates text fields
        JTextField Last = new JTextField(20);
        JTextField First = new JTextField(20);
        JTextField Street = new JTextField(20);
        JTextField City = new JTextField(10);
        JTextField Zip = new JTextField(10);
        JTextField State = new JTextField(10);
        JTextField homePhone = new JTextField(15);
        JTextField cellPhone = new JTextField(15);

        //creates objects of classes
        Person names = new Person();

    public AddressBook() {


        //creates labels
        JLabel lastName = new JLabel("Last Name:");
        JLabel firstName = new JLabel("First Name:");
        JLabel streetAdd = new JLabel("Street:");
        JLabel city = new JLabel("City:");
        JLabel zipCode = new JLabel("Zip Code:");
        JLabel state = new JLabel("State:");
        JLabel HomePhone = new JLabel("Home Phone:");
        JLabel CellPhone = new JLabel("Cell Phone:");

        //creates panels
        JPanel panel1 = new JPanel(new GridLayout(8,0,0,5));
        JPanel panel2 = new JPanel(new GridLayout(8,0,5,0));
        JPanel panel3 = new JPanel(new GridLayout(0,2,0,0));
        JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
        JPanel panel5 = new JPanel(new GridLayout(2,0));

        //add elements to panel 1
        panel1.add(Last);
        panel1.add(First);
        panel1.add(Street);
        panel1.add(City);
        panel1.add(State);
        panel1.add(Zip);
        panel1.add(homePhone);
        panel1.add(cellPhone);

        //add elements to panel2
        panel2.add(lastName);
        panel2.add(firstName);
        panel2.add(streetAdd);
        panel2.add(city);
        panel2.add(state);
        panel2.add(zipCode);
        panel2.add(HomePhone);
        panel2.add(CellPhone);

        // creates and set titled border
        Font font = new Font("Verdana",Font.ITALIC,11);
        Border lineBorder = new LineBorder(Color.GRAY,1);

        panel3.setBorder( new TitledBorder(lineBorder, "Contact Information", TitledBorder.LEFT,
        TitledBorder.DEFAULT_POSITION, font, Color.BLUE));

        panel4.setBorder(new TitledBorder(lineBorder, "Action Buttons", TitledBorder.LEFT,
        TitledBorder.DEFAULT_POSITION, font, Color.BLUE));

        //add panels to panel 3
        panel3.add(panel2);
        panel3.add(panel1);

        //add buttons to panel 4
        panel4.add(btAdd);
        panel4.add(btFirst);
        panel4.add(btNext);
        panel4.add(btPrevious);
        panel4.add(btLast);
        panel4.add(btUpdate);
        panel4.add(btSearch);
        panel4.add(btClear);
        panel4.add(btExit);

        //add panels together
        panel5.add(panel3);
        panel5.add(panel4);

        //add panels to the frame
        setLayout(new GridLayout());
        add(panel5);

        //adds action to exit button
        btExit.addActionListener(new ActionListener(){//register the listener
            public void actionPerformed(ActionEvent e){//handles the button action
                 System.exit(0);//closes the program
            }
            });

        //adds action to clear button
        btClear.addActionListener(new ActionListener(){//register the listener
            public void actionPerformed(ActionEvent e){//handles the button action
                 Last.setText("");
                 First.setText("");
                 Street.setText("");
                 City.setText("");
                 Zip.setText("");
                 State.setText("");
                 homePhone.setText("");
                 cellPhone.setText("");
            }
            });
    }

    public static void main(String[] args) {

        AddressBook frame = new AddressBook();
        frame.setTitle("Address Book");
        frame.setSize(475,400);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

//Person class

public class Person {

    private String LastName;
    private String FirstName;

    public Person(){

    }
    public Person(String LastName, String FirstName){
        this.LastName = LastName;
        this.FirstName = FirstName;

    }

    public String getLastName(){
        return LastName;
    }

    public String getFirstName(){
        return FirstName;
    }

    public void setLastName(String LastName){
        this.LastName = LastName;
    }

    public void setFirstName (String FirstName){
        this.FirstName = FirstName;
    }
    public String toString(){
        return this.FirstName + " "+ this.LastName;
    }


}

//Friend class

public class Friend extends Person{

    private String Address;
    private String City;
    private String State;
    private int Zip;
    private int HomePhone;
    private int CellPhone;

    public Friend(){
    }

    public Friend (String Address, String City, String State, int Zip,int HomePhone, int CellPhone ){
        this.Address = Address;
        this.City = City;
        this.State = State;
        this.Zip = Zip;
        this.HomePhone = HomePhone;
        this.CellPhone = CellPhone;
    }

    public String getAddress(){
        return Address;
    }

    public String getCity(){
        return City;
    }

    public String getState(){
        return State;
    }

    public int getZip(){
        return Zip;
    }

    public int getHomePhone(){
        return HomePhone;
    }

    public int getCellPhone (){
        return CellPhone;
    }

    public void setAddress(String Address){
        this.Address = Address;
    }

    public void setCity (String City){
        this.City = City;
    }

    public void setState (String State){
        this.State = State;
    }

    public void setZip (int Zip){
        this.Zip = Zip;
    }

    public void setHomePhone(int HomePhone){
        this.HomePhone = HomePhone;
    }

    public void setCellPhone (int CellPhone){
        this.CellPhone = CellPhone;
    }
    public String toString(){
        return this.Address + " "+ this.City +" " + this.State +" " + this.Zip +" "+ this.HomePhone + " " + this.CellPhone;

}
}

Recommended Answers

All 5 Replies

read from a file text

Where in the code are you trying to read from a file? For simple text files the Scanner class could do it.

store the person data into an array list

Where is the arraylist for the person data?

this is my update person class, not sure if I am correct. I am very confused with the arraylist and how to implemented using GUI.

public class Person {

    ArrayList<String>LastName;
    ArrayList<String> FirstName;

    public Person(){
        LastName=new ArrayList<String>();
        FirstName=new ArrayList<String>();

    }
    public void insertLastName(String l){
        LastName.add(l);
    }

    public void insertFirstName(String f){

        FirstName.add(f);
    }

An array list is like a dynamic array. It allows you to add lots of elements to it without having to worry about filling it up.

Your usage of arraylist in the Person class is unusual. A person normally only has a single first and last name so there is no need for an arraylist to hold that.
There could be more than one Person object used in the class, so somewhere there could be an arraylist to hold all the Person objects. Your assignment says:

create a list of Friend objects

Thanks for you clarification.I am so lost. I will keep working in this.

The assignment says:

The application should start by reading an input file

and

Once you read a complete record/line from an input file, create a Friend object

and add that object to the arraylist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.