Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an AddressBook class to search the address or the phone of your present fr

ID: 3651004 • Letter: W

Question

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 use an ArrayList to create a list of Friend objects
Once you finish reading all the records from the input file, display all the Friend objects using the collection object.
Once you read all the input, display the GUI to the user and allow him/her to view, add or to search the list. The buttons in the GUI should allow the user to perform such actions. That is, when pressed:

Explanation / Answer

Part 4:

Run this:

Program AddressBookGUI.java

================================================

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

class AddressBookGUI extends JFrame implements ActionListener
{
    private JLabel name,address,phone,searchByName,searchByPhone;
    private JTextField n,a,p,sn,sp;

    private JButton showall,add,bsn,bnp;
   
    JPanel panel1,panel2,panel3;
    AddressBook ab;

    JFrame frame;
    AddressBookGUI()
    {
        frame=new JFrame();

        ab=new AddressBook();
        frame.setTitle("Address Book");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //GUI
        name=new JLabel("Name: ");
        address=new JLabel("Address: ");
        phone=new JLabel("Phone: ");
        searchByName=new JLabel("Search By Name: ");
        searchByPhone=new JLabel("Search By Phone: ");
       
        n=new JTextField("",25);
        a=new JTextField("",25);
        p=new JTextField("",25);
        sn=new JTextField("",25);
        sp=new JTextField("",25);
       
        add=new JButton("Add");
        showall=new JButton("Show all");
        bsn=new JButton("Search");
        bnp=new JButton("Search");
       
        panel1=new JPanel();
        JPanel subp1,subp2,subp3,subp4;
        subp1=new JPanel();
        subp1.add(name);
        subp1.add(n);
        subp2=new JPanel();
        subp2.add(address);
        subp2.add(a);
        subp3=new JPanel();
        subp3.add(phone);
        subp3.add(p);
        subp4=new JPanel();
        subp4.add(add);
        subp4.add(showall);
        panel1.setLayout(new GridLayout(4,1));
        panel1.add(subp1);
        panel1.add(subp2);
        panel1.add(subp3);
        panel1.add(subp4);
       
        panel2=new JPanel();
        //GUI
        //Table
        String[] columnNames = {"Name",
                "Address",
                "Phone Number"};

        AddressBook ab1=new AddressBook();
        ab1.ReadData();

        Object data[][]=new Object[ab1.getAb().size()][3];
        int i,j;
        for(i=0;i<ab1.getAb().size();i++)
        {
            data[i][0]=ab1.getAb().get(i).getName();
            data[i][1]=ab1.getAb().get(i).getAddress();
            data[i][2]=ab1.getAb().get(i).getPhoneNumber();
        }

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(400, 200));
        table.setFillsViewportHeight(true);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        panel2.add(scrollPane);

        panel3=new JPanel();
        JPanel sp1,sp2,sp3,sp4;
        sp1=new JPanel();
        sp1.add(searchByName);
        sp1.add(sn);
        sp2=new JPanel();
        sp2.add(bsn);
        sp3=new JPanel();
        sp3.add(searchByPhone);
        sp3.add(sp);
        sp4=new JPanel();
        sp4.add(bnp);
        panel3.setLayout(new GridLayout(4,1));
        panel3.add(sp1);
        panel3.add(sp2);
        panel3.add(sp3);
        panel3.add(sp4);
       
        frame.setLayout(new GridLayout(1, 3));
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);

        frame.pack();
        addListeners();
        frame.setVisible(true);
    }
    public void addListeners()
    {
        add.addActionListener(this);
        showall.addActionListener(this);
        bsn.addActionListener(this);
        bnp.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==add)
        {
            String t1=n.getText();
            String t2=a.getText();
            String t3=p.getText();
            if(t1.equals("")||t2.equals("")||t3.equals(""))
            {
                JOptionPane.showMessageDialog(frame,"The Name/Address/Phone space is blank, enter again");
                return;
            }
            Friend f1=new Friend();
            f1.setName(t1);
            f1.setAddress(t2);
            f1.setPhoneNumber(Integer.parseInt(t3));
            ab.ReadData();
            ab.appendData(f1);
            frame.setVisible(false);
            new AddressBookGUI();
        }
        if(e.getSource()==showall)
        {
            ab.ReadData();
            frame.setVisible(false);
            new AddressBookGUI();
        }
        if(e.getSource()==bsn)
        {
            String t=sn.getText();
            if(t.equals(""))
            {
                JOptionPane.showMessageDialog(frame,"The Name space is blank, enter again");
                return;
            }
            ab.ReadData();
            ArrayList<Friend> sar=ab.searchByName(t);
            if(sar.size()==0)
            {
                JOptionPane.showMessageDialog(frame,"No records by that name");
                return;
            }
            ShowSearchResults sr=new ShowSearchResults(sar);
        }
        if(e.getSource()==bnp)
        {
            String t=sp.getText();
            if(t.equals(""))
            {
                JOptionPane.showMessageDialog(frame,"The Phone space is blank, enter again");
                return;
            }
            ab.ReadData();
            int t1=Integer.parseInt(t);
            ArrayList<Friend> sar=ab.searchByPhone(t1);
            if(sar.size()==0)
            {
                JOptionPane.showMessageDialog(frame,"No records by that Phone Number");
                return;
            }
            ShowSearchResults sr=new ShowSearchResults(sar);
        }
    }
    public static void main(String args[])
    {
        new AddressBookGUI();
    }
}

=====================================================

Screenshot:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote