Write a Java GUI application that will keep track of a user's contact list. The application must contain the contact mobile number, work number and email address
Q.3.1
On the form, create a list box that will allow the user to select a contact name, which is populated from a text file. Also create a search button that when clicked will display the mobile number, work number and email address.
Create a sequential file (contacts.txt) that contains data for the following fields
Q.3.2
The contact name;
The contact mobile number,
The contact work number:
The contact email address
Q.33
Load the data from the contacts txt file and populate the list bax with the contact names Include a feature to inform the user on the amount of contacts
Sample Screenshot:
Contacts Application
Select Contact
Joe Bloggs
Jeff Jones
Mobile
072 157 1545
Work
021 5555555
@gmail.com
Number of contacts: 2
SEARCH
import java.io.*;
import java.awt.Component;
import javax.swing.*;
import java.util.Collections;
import java.util.ArrayList;
public class Main extends JFrame {
private static ArrayList<Peoples> people = new ArrayList<>();
JPanel inp = new JPanel(),
MPan = new JPanel(),
Span = new JPanel(),
searchPanel = new JPanel(),
editPanel = new JPanel(),
currentPanel = MPan;
JLabel Lab[] = { new JLabel("Name: "),
new JLabel("Street address: "),
new JLabel("Email address: "),
new JLabel("Phone number: "),
new JLabel("Date of birth: ") };
JTextField inputFields[] = new JTextField[5],
searchOption = new JButton("Find people"),
Nam = new JTextField("Enter the name and telephone number.");
JButton option = new JButton("Register new people"),
searchReturn = new JButton("Back to main menu"),
sortChoise = new JButton("Display all people"),
inputSubmit = new JButton("Submit"),
sortDisplay = new JButton("Back to main menu"),
JTextArea searchArea = new JTextArea();
fields = new JTextArea();
protected void init() {
setSize(900, 500);
setComponent(MPan, this, 900, 500, 0, 0, true);
setComponent(inp, this, 900, 500, 0, 0, false);
setComponent(Span, this, 900, 500, 0, 0, false);
setComponent(searchPanel, this, 900, 500, 0, 0, false);
setComponent(new JLabel("Address Book Application!"), MPan, 300, 30, 100, 30, true);
setComponent(new JLabel("Select activity you want to perform?"), MPan, 300, 30, 150, 50, true);
setComponent(option, MPan, 300, 40, 100, 100, true);
setComponent(sortChoise, MPan, 300, 40, 100, 170, true);
setComponent(searchOption, MPan, 300, 40, 100, 240, true);
setComponent(sortDisplay, Span, 300, 40, 400, 10, true);
setComponent(searchReturn, searchPanel, 300, 40, 500, 10, true);
setComponent(searchArea, searchPanel, 300, 400, 350, 100, true);
sortDisplay.addActionListener(event -> displayPanel(MPan));
searchReturn.addActionListener(event -> displayPanel(MPan));
option.addActionListener(event -> displayPanel(inp));
searchOption.addActionListener(event -> displayPanel(searchPanel));
sortChoise.addActionListener(event -> {
displayPanel(Span);
Collections.sort(people);
for (int i = 0; i < people.size(); i++) {
fields.setText(fields.getText() + "Name: " + people.get(i).getN() + "\n " + "Phone: "
+ people.get(i).getPhoneNmb() + "\n " + "Email: " + people.get(i).getEmail() + "\n "
+ "Address: " + people.get(i).getStreetAddress() + "\n " + "BirthDate: "
+ people.get(i).getBirthDate() + "\n ");
}
});
for (int i = 0, j = 20; i < 5; i++, j += 50) {
setComponent(Lab[i], inp, 100, 20, 50, j, true);
setComponent(inputFields[i] = new JTextField(), inp, 100, 20, 200, j, true);
}
setComponent(fields, Span, 200, 500, 200, 70, true);
setComponent(inputSubmit, inp, 80, 40, 450, 600, true);
inputSubmit.addActionListener(event -> {
people.add(new Peoples(inputFields[0].getText(), inputFields[1].getText(), inputFields[2].getText(),
inputFields[3].getText(), inputFields[4].getText()));
displayPanel(MPan);
});
setComponent(Nam, searchPanel, 200, 20, 350, 10, true);
Nam.addActionListener(event -> {
searchArea.removeAll();
String input = Nam.getText();
JTextArea searchArea = new JTextArea(200, 30);
boolean byName = input.matches("[A-Za-z]||\\s+");
for (Peoples p : people) {
if (input.equals(byName ? p.getN() : p.getPhoneNmb()))
searchArea.setText(searchArea.getText() + p.getN() + " " + p.getPhoneNmb());
}
});
setvisible(true);
}
protected void setComponent(JComponent comp, Component place, int dm, int lh, int kk, int yy, boolean v) {
comp.setSize(dm, lh);
comp.setLocation(kk, yy);
if (place == this)
((JFrame) place).add(comp);
else
((JComponent) place).add(comp);
comp.setLayout(null);
comp.setvisible(v);
}
public void deserializeAll() {
people.clear();
Peoples p = null;
try (FileInputStream fis = new FileInputStream("personSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {
while (ois.available() > 0) {
p = (Peoples) ois.readObject();
people.add(p);
}
} catch (IOException | ClassNotFoundException e) {
System.out.println(e.getMessage());
}
}
class Peoples implements Comparable<Peoples>, Serializable {
private String name, phoneNmb, email, streetAddress, birthDate;
Peoples(String N, String p, String e, String s, String b) {
name = N;P
phoneNmb = p;
email = e;
streetAddress = s;
birthDate = b;
serialize();
}
public String getN() {
return name;
}
public void serializeAll() {
for (Peoples p : people)
p.serialize();
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNmb() {
return phoneNmb;
}
public void setPhoneNmb(String phoneNmb) {
this.phoneNmb = phoneNmb;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public int compareTo(Peoples p) {
return name.compareTo(p.getN());
}
public String getStreetAddress() {
return streetAddress;
}
protected void serialize() {
try (FileOutputStream fs = new FileOutputStream("personSer.ser", true);
ObjectOutputStream os = new ObjectOutputStream(fs)) {
os.writeObject(this);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
protected Peoples deserialize() {
Peoples p = null;
try (FileInputStream fis = new FileInputStream("personSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {
p = (Peoples) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println(e.getMessage());
}
return p;
}
}
protected void displayPanel(JPanel jp) {
if (jp != currentPanel) {
currentPanel.setvisible(false);
currentPanel = jp;
}
jp.setvisible(true);
}
public static void main(String args[]) {
new Main().init();
}
}
Comments
Leave a comment