-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactManagerPro.java
More file actions
202 lines (171 loc) · 7.01 KB
/
Copy pathContactManagerPro.java
File metadata and controls
202 lines (171 loc) · 7.01 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
// Contact class following structured contact fields
class Contact implements Comparable<Contact> {
String firstName, lastName, phone, email, company, jobTitle, address, birthday, notes;
public Contact(String firstName, String lastName, String phone, String email,
String company, String jobTitle, String address, String birthday, String notes) {
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
this.email = email;
this.company = company;
this.jobTitle = jobTitle;
this.address = address;
this.birthday = birthday;
this.notes = notes;
}
@Override
public String toString() {
return firstName + " " + lastName + "\t" + phone + "\t" + email + "\t" + company +
"\t" + jobTitle + "\t" + address + "\t" + birthday + "\t" + notes;
}
@Override
public int compareTo(Contact o) {
return this.lastName.compareToIgnoreCase(o.lastName);
}
}
// File handling class
class FileHandler {
public static void writeToFile(String filename, ArrayList<Contact> contacts) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write("First Name\tLast Name\tPhone\tEmail\tCompany\tJob Title\tAddress\tBirthday\tNotes\n");
for (Contact contact : contacts) {
writer.write(contact.toString());
writer.newLine();
}
writer.close();
}
public static ArrayList<Contact> readFromFile(String filename) throws IOException {
ArrayList<Contact> contacts = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
reader.readLine(); // Skip header
while ((line = reader.readLine()) != null) {
String[] fields = line.split("\t");
if (fields.length == 9) {
contacts.add(new Contact(fields[0], fields[1], fields[2], fields[3], fields[4],
fields[5], fields[6], fields[7], fields[8]));
}
}
reader.close();
return contacts;
}
}
// GUI Application
public class ContactManagerPro extends JFrame {
private JTextField firstNameField, lastNameField, phoneField, emailField, companyField, jobTitleField,
addressField, birthdayField, notesField;
private JTextArea textArea;
private JButton addButton, saveButton, loadButton, sortButton;
private ArrayList<Contact> contacts;
private static final String FILE_NAME = "contacts.txt";
public ContactManagerPro() {
setTitle("Contact Manager Pro"); // <-- Title updated here
setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel inputPanel = new JPanel(new GridLayout(9, 2, 5, 5));
inputPanel.add(new JLabel("First Name:"));
firstNameField = new JTextField();
inputPanel.add(firstNameField);
inputPanel.add(new JLabel("Last Name:"));
lastNameField = new JTextField();
inputPanel.add(lastNameField);
inputPanel.add(new JLabel("Phone:"));
phoneField = new JTextField();
inputPanel.add(phoneField);
inputPanel.add(new JLabel("Email:"));
emailField = new JTextField();
inputPanel.add(emailField);
inputPanel.add(new JLabel("Company:"));
companyField = new JTextField();
inputPanel.add(companyField);
inputPanel.add(new JLabel("Job Title:"));
jobTitleField = new JTextField();
inputPanel.add(jobTitleField);
inputPanel.add(new JLabel("Address:"));
addressField = new JTextField();
inputPanel.add(addressField);
inputPanel.add(new JLabel("Birthday:"));
birthdayField = new JTextField();
inputPanel.add(birthdayField);
inputPanel.add(new JLabel("Notes:"));
notesField = new JTextField();
inputPanel.add(notesField);
add(inputPanel, BorderLayout.NORTH);
textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
JPanel panel = new JPanel();
addButton = new JButton("Add Contact");
saveButton = new JButton("Save Contacts");
loadButton = new JButton("Load Contacts");
sortButton = new JButton("Sort by Last Name");
panel.add(addButton);
panel.add(saveButton);
panel.add(loadButton);
panel.add(sortButton);
add(panel, BorderLayout.SOUTH);
contacts = new ArrayList<>();
addButton.addActionListener(e -> addContact());
saveButton.addActionListener(e -> saveContacts());
loadButton.addActionListener(e -> loadContacts());
sortButton.addActionListener(e -> sortContacts());
}
private void addContact() {
Contact contact = new Contact(
firstNameField.getText(), lastNameField.getText(), phoneField.getText(),
emailField.getText(), companyField.getText(), jobTitleField.getText(),
addressField.getText(), birthdayField.getText(), notesField.getText()
);
contacts.add(contact);
updateTextArea();
clearFields();
}
private void saveContacts() {
try {
FileHandler.writeToFile(FILE_NAME, contacts);
JOptionPane.showMessageDialog(this, "Contacts saved successfully!");
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error saving contacts.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void loadContacts() {
try {
contacts = FileHandler.readFromFile(FILE_NAME);
updateTextArea();
JOptionPane.showMessageDialog(this, "Contacts loaded successfully!");
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error loading contacts.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void sortContacts() {
contacts.sort(Comparator.naturalOrder()); // Sorts by last name
updateTextArea();
JOptionPane.showMessageDialog(this, "Contacts sorted successfully!");
}
private void updateTextArea() {
textArea.setText("First Name\tLast Name\tPhone\tEmail\tCompany\tJob Title\tAddress\tBirthday\tNotes\n");
for (Contact contact : contacts) {
textArea.append(contact.toString() + "\n");
}
}
private void clearFields() {
firstNameField.setText("");
lastNameField.setText("");
phoneField.setText("");
emailField.setText("");
companyField.setText("");
jobTitleField.setText("");
addressField.setText("");
birthdayField.setText("");
notesField.setText("");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ContactManagerPro().setVisible(true));
}
}