Answer to Question #200413 in Java | JSP | JSF for Maxwell

Question #200413

Create a GUI application “Address Book” which can be used to store and search the information of different people. The information can be: name, phone no, email, street address etc. Your project should have the following features:

1. A GUI where user can input the information of a particular people

2. A file where all information are stored. (You will have to append new info to the file. Otherwise the previous information will be lost ,you have to use file reader and writer method)

3. A GUI in which the user will be able to see all the information, sorted by name. The information must come from the file where you stored user information. (Hint: use JTextArea to show all user info)

4. A GUI where the user will be able to search a person using his name or phone no. The search result should show the info of all the matched person

5. A GUI where the user will be able to edit the information of a specific person. 


1
Expert's answer
2021-05-31T12:49:09-0400
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class GUI extends JFrame {
    private ArrayList<Person> persons;
    private JList<Object> list;
    private String fileName = "people.txt";

    public GUI() {
        persons = new ArrayList<>();
        readFile(fileName);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("Add", addPanel());
        tabbedPane.add("Show|Search", showOrSearchPanel());
        tabbedPane.add("Edit", editPanel());
        add(tabbedPane);
        setSize(400, 300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public JPanel addPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        String[] labelNames = {"Name", "Phone Number", "Email", "Street Address"};
        JTextField[] textFields = new JTextField[labelNames.length];
        JLabel[] labels = new JLabel[labelNames.length];
        for (int i = 0; i < labelNames.length; i++) {
            textFields[i] = new JTextField();
            labels[i] = new JLabel(labelNames[i]);
            panel.add(labels[i]);
            panel.add(textFields[i]);
        }
        JButton addButton = new JButton("Add");
        addButton.addActionListener(e -> {
            for (JTextField textField : textFields) {
                if (textField.getText().length() == 0) {
                    return;
                }
            }
            persons.add(new Person(textFields[0].getText(), textFields[1].getText(),
                    textFields[2].getText(), textFields[3].getText()));
            list.setListData(persons.toArray());
            writeFile(fileName);
        });
        panel.add(addButton);
        return panel;
    }

    public JPanel showOrSearchPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        JPanel searchPanel = new JPanel();
        searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.Y_AXIS));
        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(false);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(380, 200));
        JLabel nameLabel = new JLabel("Name");
        JTextField nameField = new JTextField();
        JLabel phoneLabel = new JLabel("Phone Number");
        JTextField phoneField = new JTextField();
        JButton showButton = new JButton("Show All");
        showButton.addActionListener(e -> {
            textArea.setText("");
            persons.sort(Person::compareTo);
            for (Person person : persons) {
                textArea.append(person + "\n");
            }
        });
        JButton nameButton = new JButton("Search by name");
        nameButton.addActionListener(e -> {
            if (nameField.getText().length() > 0) {
                textArea.setText("");
                for (Person person : persons) {
                    if (person.getName().equals(nameField.getText())) {
                        textArea.append(person + "\n");
                    }
                }
            }
        });
        JButton phoneButton = new JButton("Search by phone no.");
        phoneButton.addActionListener(e -> {
            if (phoneField.getText().length() > 0) {
                textArea.setText("");
                for (Person person : persons) {
                    if (person.getName().equals(phoneField.getText())) {
                        textArea.append(person + "\n");
                    }
                }
            }
        });

        searchPanel.add(nameLabel);
        searchPanel.add(nameField);
        searchPanel.add(phoneLabel);
        searchPanel.add(phoneField);

        searchPanel.add(showButton);
        searchPanel.add(nameButton);
        searchPanel.add(phoneButton);
        panel.add(scrollPane);
        panel.add(searchPanel);
        return panel;
    }

    public JPanel editPanel() {
        String[] labelNames = {"Name", "Phone Number", "Email", "Street Address"};
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        JPanel dataPanel = new JPanel();
        dataPanel.setLayout(new BoxLayout(dataPanel, BoxLayout.Y_AXIS));
        JTextField[] textFields = new JTextField[labelNames.length];
        JLabel[] labels = new JLabel[labelNames.length];
        list = new JList<>();
        list.setListData(persons.toArray());
        list.addListSelectionListener(e -> {
            if (list.getSelectedIndex() != -1) {
                textFields[0].setText(persons.get(list.getSelectedIndex()).getName());
                textFields[1].setText(persons.get(list.getSelectedIndex()).getPhoneNumber());
                textFields[2].setText(persons.get(list.getSelectedIndex()).getEmail());
                textFields[3].setText(persons.get(list.getSelectedIndex()).getStreetAddress());
            }
        });
        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setPreferredSize(new Dimension(380, 200));
        for (int i = 0; i < labelNames.length; i++) {
            textFields[i] = new JTextField();
            labels[i] = new JLabel(labelNames[i]);
            dataPanel.add(labels[i]);
            dataPanel.add(textFields[i]);
        }
        JButton editButton = new JButton("Edit");
        editButton.addActionListener(e -> {
            if (list.getSelectedIndex() != -1) {
                for (JTextField textField : textFields) {
                    if (textField.getText().length() == 0) {
                        return;
                    }
                }
                persons.get(list.getSelectedIndex()).setName(textFields[0].getText());
                persons.get(list.getSelectedIndex()).setPhoneNumber(textFields[1].getText());
                persons.get(list.getSelectedIndex()).setEmail(textFields[2].getText());
                persons.get(list.getSelectedIndex()).setStreetAddress(textFields[3].getText());
                list.setListData(persons.toArray());
                writeFile(fileName);
            }
        });
        dataPanel.add(editButton);
        panel.add(scrollPane);
        panel.add(dataPanel);
        return panel;
    }

    public void readFile(String fileName) {
        try (Scanner in = new Scanner(new File(fileName))) {
            String[] data;
            while (in.hasNextLine()) {
                data = in.nextLine().split(";");
                persons.add(new Person(data[0], data[1], data[2], data[3]));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void writeFile(String fileName) {
        try (PrintWriter writer = new PrintWriter(fileName)) {
            for (Person person : persons) {
                writer.println(person.getName() + ";" + person.getPhoneNumber() + ";"
                        + person.getEmail() + ";" + person.getStreetAddress());
                writer.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new GUI();
    }
}


public class Person implements Comparable<Person> {
    private String name;
    private String phoneNumber;
    private String email;
    private String streetAddress;

    public Person(String name, String phoneNumber, String email, String streetAddress) {
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.email = email;
        this.streetAddress = streetAddress;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getStreetAddress() {
        return streetAddress;
    }

    public void setStreetAddress(String streetAddress) {
        this.streetAddress = streetAddress;
    }

    @Override
    public int compareTo(Person person) {
        return name.compareTo(person.name);
    }

    @Override
    public String toString() {
        return name + ", PhoneNumber= " + phoneNumber +
                ", Email= " + email + ", StreetAddress= " + streetAddress;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS