-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathPhoneDirectory.java
More file actions
157 lines (137 loc) · 5.81 KB
/
Copy pathPhoneDirectory.java
File metadata and controls
157 lines (137 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
public class PhoneDirectory extends JFrame implements ActionListener {
// HashMap to store contacts
HashMap<String, Contact> contacts = new HashMap<>();
// GUI components
JTextField firstNameField, lastNameField, emailField, phoneNumberField, jobNameField, searchField;
JTextArea displayArea;
JButton addButton, deleteButton, updateButton, searchButton, showAllButton;
// Constructor to initialize the GUI
public PhoneDirectory() {
setTitle("Phone Directory");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(0, 1));
// Input fields
firstNameField = new JTextField(15);
lastNameField = new JTextField(15);
emailField = new JTextField(15);
phoneNumberField = new JTextField(15);
jobNameField = new JTextField(15);
searchField = new JTextField(15);
// Buttons
addButton = new JButton("Add Contact");
deleteButton = new JButton("Delete Contact");
updateButton = new JButton("Update Contact");
searchButton = new JButton("Search Contact");
showAllButton = new JButton("Show All Contacts");
// Display area
displayArea = new JTextArea(10, 30);
displayArea.setEditable(false);
// Panel for input fields and labels
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(0, 2));
inputPanel.add(new JLabel("First Name:"));
inputPanel.add(firstNameField);
inputPanel.add(new JLabel("Last Name:"));
inputPanel.add(lastNameField);
inputPanel.add(new JLabel("Email:"));
inputPanel.add(emailField);
inputPanel.add(new JLabel("Phone Number:"));
inputPanel.add(phoneNumberField);
inputPanel.add(new JLabel("Job Name:"));
inputPanel.add(jobNameField);
// Panel for search field
JPanel searchPanel = new JPanel();
searchPanel.add(new JLabel("Search by Phone/Name:"));
searchPanel.add(searchField);
// Panel for buttons
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(updateButton);
buttonPanel.add(searchButton);
buttonPanel.add(showAllButton);
// Add action listeners to buttons
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
searchButton.addActionListener(this);
showAllButton.addActionListener(this);
// Add components to the frame
add(inputPanel);
add(searchPanel);
add(buttonPanel);
add(new JScrollPane(displayArea));
setVisible(true);
}
// Inner class to define a Contact
class Contact {
String firstName, lastName, email, phoneNumber, jobName;
Contact(String firstName, String lastName, String email, String phoneNumber, String jobName) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phoneNumber = phoneNumber;
this.jobName = jobName;
}
@Override
public String toString() {
return firstName + " " + lastName + " - " + email + " - " + phoneNumber + " - " + jobName;
}
}
// Method to handle button actions
public void actionPerformed(ActionEvent e) {
String firstName = firstNameField.getText();
String lastName = lastNameField.getText();
String email = emailField.getText();
String phoneNumber = phoneNumberField.getText();
String jobName = jobNameField.getText();
if (e.getSource() == addButton) {
// Add contact
if (!phoneNumber.isEmpty()) {
contacts.put(phoneNumber, new Contact(firstName, lastName, email, phoneNumber, jobName));
displayArea.setText("Contact added: " + phoneNumber);
} else {
displayArea.setText("Phone number is required to add contact.");
}
} else if (e.getSource() == deleteButton) {
// Delete contact
if (!phoneNumber.isEmpty()) {
contacts.remove(phoneNumber);
displayArea.setText("Contact deleted: " + phoneNumber);
} else {
displayArea.setText("Enter phone number to delete contact.");
}
} else if (e.getSource() == updateButton) {
// Update contact
if (!phoneNumber.isEmpty() && contacts.containsKey(phoneNumber)) {
contacts.put(phoneNumber, new Contact(firstName, lastName, email, phoneNumber, jobName));
displayArea.setText("Contact updated: " + phoneNumber);
} else {
displayArea.setText("Contact not found.");
}
} else if (e.getSource() == searchButton) {
// Search contact
String searchKey = searchField.getText();
if (contacts.containsKey(searchKey)) {
displayArea.setText(contacts.get(searchKey).toString());
} else {
displayArea.setText("Contact not found.");
}
} else if (e.getSource() == showAllButton) {
// Show all contacts
StringBuilder allContacts = new StringBuilder();
for (Contact contact : contacts.values()) {
allContacts.append(contact.toString()).append("\n");
}
displayArea.setText(allContacts.toString());
}
}
public static void main(String[] args) {
new PhoneDirectory();
}
}