-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
389 lines (349 loc) · 15.6 KB
/
Copy pathMain.java
File metadata and controls
389 lines (349 loc) · 15.6 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;
import javax.swing.*;
import java.awt.Desktop;
/*
* This main class starts the address book program by calling the book class constructor
* and creating a book object
*/
public class Main implements ActionListener {
JFrame mainFrame = new JFrame(); //creates a frame for the board
JFrame inputFrame = new JFrame(); //creates the frame for the input window
JFrame errorFrame = new JFrame(); //creates the frame for the error window
JPanel titlePanel = new JPanel(); //creates the title panel
JPanel infoTitlePanel = new JPanel(); //creates the title panel for info frame
JPanel buttonPanel = new JPanel(); //creates the button panel for info frame
JPanel infoPanel = new JPanel(); //creates the info panel
JLabel titleTextField = new JLabel(); //creates the title text field
JLabel infoTitleTextField = new JLabel(); //creates the title text field
JLabel firstNameLabel = new JLabel(); //creates the first name Label
JLabel lastNameLabel = new JLabel(); //creates the phone number Label
JLabel streetLabel = new JLabel(); //creates the street Label
JLabel cityLabel = new JLabel(); //creates the city Label
JLabel stateLabel = new JLabel(); //creates the state Label
JLabel zipcodeLabel = new JLabel(); //creates the zipcode Label
JLabel phoneNumberLabel = new JLabel(); //creates the phone number Label
JLabel errorMessage = new JLabel(); //creates the phone number Label
JTextField firstNameTextField = new JTextField(""); //creates the first name text field
JTextField lastNameTextField = new JTextField(""); //creates the phone number text field
JTextField streetTextField = new JTextField(""); //creates the street text field
JTextField cityTextField = new JTextField(""); //creates the city text field
JTextField stateTextField = new JTextField(""); //creates the state text field
JTextField zipcodeTextField = new JTextField(""); //creates the zipcode text field
JTextField phoneNumberTextField = new JTextField(""); //creates the phone number text field
JButton addButton = new JButton(); //creates a add button
JButton updateButton = new JButton(); //creates a update button
JButton deleteButton = new JButton(); //creates a delete button
JButton enterButton = new JButton(); //creates an enter button
JButton exitButton = new JButton(); //creates an exit button
JButton logButton = new JButton(); //creates an log button
JTextPane addressList = new JTextPane(); //creates a box with list of addresses
Book addressBook; //create book object
//Variable hold values entered in by user
private String message; //return message from addressbook class method calls
private String firstName; //user entered first name
private String lastName; //user entered last name
private String street; //user entered street name
private String city; //user entered city name
private String state; //user entered state name
private String zipcode; //user entered zipcode
private String phoneNumber; //user entered phone number
//Constructor creates the object to start the program and create the windows
public Main() {
addressBook = new Book(); //creates a book object
buildMainFrame(); //builds the main frame
}
//method builds the manin frame
public void buildMainFrame() {
//build frame
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(600, 250);
mainFrame.setLayout(new BorderLayout());
//build title panel
titlePanel.setLayout(new BorderLayout());
//build title text Field
titleTextField.setForeground(new Color(20, 153, 242));
titleTextField.setFont(new Font("Serif", Font.BOLD, 20));
titleTextField.setHorizontalAlignment(JLabel.CENTER);
titleTextField.setText("Address Book");
//add tile field to the title panel
titlePanel.add(titleTextField, BorderLayout.CENTER);
//add tile panel to mainFrame
mainFrame.add(titlePanel, BorderLayout.NORTH);
//build text area for list of addresses and add to main frame
addressList.setFont(new Font("Serif", Font.BOLD, 12));
JScrollPane scrollPane = new JScrollPane(addressList);
addressList.setEditable(false);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
mainFrame.add(scrollPane, BorderLayout.CENTER);
//add current addresses to the text box
addressList.setText(addressBook.toString());
//build buttons panel
buttonPanel.setLayout(new GridLayout(1,4));
mainFrame.add(buttonPanel, BorderLayout.SOUTH);
//build add button
addButton.setFont(new Font("Serif", Font.BOLD, 12));
addButton.setText("ADD NEW CONTACT");
addButton.setFocusable(false);
addButton.addActionListener(this);
buttonPanel.add(addButton);
//build update button
updateButton.setFont(new Font("Serif", Font.BOLD, 12));
updateButton.setText("UPDATE CONTACT");
updateButton.setFocusable(false);
updateButton.addActionListener(this);
updateButton.setMaximumSize(new Dimension(30, 50));
buttonPanel.add(updateButton);
//build delete button
deleteButton.setFont(new Font("Serif", Font.BOLD, 12));
deleteButton.setText("DELETE CONTACT");
deleteButton.setFocusable(false);
deleteButton.addActionListener(this);
buttonPanel.add(deleteButton);
//build log button
logButton.setFont(new Font("Serif", Font.BOLD, 12));
logButton.setText("DISPLAY LOG");
logButton.setFocusable(false);
logButton.addActionListener(this);
buttonPanel.add(logButton);
//make the main frame visible
mainFrame.setVisible(true);
}
//method builds the input frame
public void buildInfoFrame(String frameTitle, String buttonLabel) {
//build frame
inputFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
inputFrame.setSize(500,300);
inputFrame.setLayout(new BorderLayout());
//build title panel
infoTitlePanel.setLayout(new BorderLayout());
//build title text Field
infoTitleTextField.setForeground(new Color(20, 153, 242));
infoTitleTextField.setFont(new Font("Serif", Font.BOLD, 25));
infoTitleTextField.setHorizontalAlignment(JLabel.CENTER);
infoTitleTextField.setText(frameTitle);
//add tile field to the title panel
infoTitlePanel.add(infoTitleTextField, BorderLayout.CENTER);
//add tile panel to mainFrame
inputFrame.add(infoTitlePanel, BorderLayout.NORTH);
//builds a layout panel for info text fields;
infoPanel.setLayout(new GridLayout(7,2));
inputFrame.add(infoPanel, BorderLayout.CENTER);
//build first name label and text fields
firstNameLabel.setText("First Name: ");
firstNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
firstNameTextField.addActionListener(this);
firstNameTextField.setText("");
infoPanel.add(firstNameLabel, 0);
infoPanel.add(firstNameTextField, 1);
//build phone number label and text fields and add to panel
lastNameLabel.setText("Last Name: ");
lastNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
lastNameTextField.addActionListener(this);
lastNameTextField.setText("");
infoPanel.add(lastNameLabel, 2);
infoPanel.add(lastNameTextField, 3);
//build street label and text fields and add to panel
streetLabel.setText("Street: ");
streetLabel.setHorizontalAlignment(SwingConstants.RIGHT);
streetTextField.addActionListener(this);
streetTextField.setText("");
infoPanel.add(streetLabel, 4);
infoPanel.add(streetTextField, 5);
//build city label and text fields and add to panel
cityLabel.setText("city: ");
cityLabel.setHorizontalAlignment(SwingConstants.RIGHT);
cityTextField.addActionListener(this);
cityTextField.setText("");
infoPanel.add(cityLabel, 6);
infoPanel.add(cityTextField, 7);
//build state label and text fields and add to panel
stateLabel.setText("State (abbreviation): ");
stateLabel.setHorizontalAlignment(SwingConstants.RIGHT);
stateTextField.addActionListener(this);
stateTextField.setText("");
infoPanel.add(stateLabel, 8);
infoPanel.add(stateTextField, 9);
//build zipcode label and text fields and add to panel
zipcodeLabel.setText("ZIP Code: ");
zipcodeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
zipcodeTextField.addActionListener(this);
zipcodeTextField.setText("");
infoPanel.add(zipcodeLabel, 10);
infoPanel.add(zipcodeTextField, 11);
//build phone number label and text fields and add to panel
phoneNumberLabel.setText("Phone Number (No dashs or Spaces): ");
phoneNumberLabel.setHorizontalAlignment(SwingConstants.RIGHT);
phoneNumberTextField.addActionListener(this);
phoneNumberTextField.setText("");
infoPanel.add(phoneNumberLabel, 12);
infoPanel.add(phoneNumberTextField, 13);
//build function button
enterButton.setFont(new Font("Serif", Font.BOLD, 12));
enterButton.setText(buttonLabel);
enterButton.setFocusable(false);
enterButton.addActionListener(this);
inputFrame.add(enterButton, BorderLayout.SOUTH);
//make the main frame visible
inputFrame.setVisible(true);
}
public void buildErrorFrame(String Message) {
//build frame
errorFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
errorFrame.setSize(500,150);
errorFrame.setLayout(new BorderLayout());
//build title text Field
errorMessage.setForeground(new Color(0, 0, 0));
errorMessage.setFont(new Font("Serif", Font.BOLD, 14));
errorMessage.setHorizontalAlignment(JLabel.CENTER);
errorMessage.setText(Message);
//add tile field to the title panel
errorFrame.add(errorMessage, BorderLayout.CENTER);
//build function button
exitButton.setFont(new Font("Serif", Font.BOLD, 16));
exitButton.setText("Exit");
exitButton.setFocusable(false);
exitButton.addActionListener(this);
errorFrame.add(exitButton, BorderLayout.SOUTH);
//make the main frame visible
errorFrame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//build the frame with add button is clicked
if(e.getSource() == addButton) {
buildInfoFrame("Enter New Contact Information", "ADD CONTACT");
}
//build the frame with update button is clicked
if(e.getSource() == updateButton) {
buildInfoFrame("Enter Current Contact Information", "NEXT");
}
//build the frame with delete button is clicked
if(e.getSource() == deleteButton) {
buildInfoFrame("Enter Contact Information To Delete", "DELETE CONTACT");
}
//open text file with log button is clicked
if(e.getSource() == logButton) {
try { //try/catch block to get file
File file = new File("ChangeRecord.txt"); //create file object
Desktop desktop = Desktop.getDesktop(); //create desktop object to open file
if(file.exists()) { //checks file exists or not
desktop.open(file); //opens the file
}
else { //error message if file not found
buildErrorFrame("No Log File Available");
}
}
catch(Exception e1) {
e1.printStackTrace();
}
}
//colse window when exit button is presses
if(e.getSource() == exitButton) {
errorFrame.dispose();
}
//perform actions based on label given to the enter button
if(e.getSource() == enterButton) {
String message; //local field to hold return message
String firstName = firstNameTextField.getText(); //local field to hold user entered first name
String lastName = lastNameTextField.getText(); //local field to hold user entered last name
String street = streetTextField.getText(); //local field to hold user entered street name
String city = cityTextField.getText(); //local field to hold user entered city name
String state = stateTextField.getText(); //local field to hold user entered state name
String zipcode = zipcodeTextField.getText(); //local field to hold user entered zipcode name
String phoneNumber = phoneNumberTextField.getText(); //local field to hold user entered phoneNumber name
//adds contact when button is clicked
if(enterButton.getText() == "ADD CONTACT") {
//checks tat values are not null in text fields
if(firstName.equals("") || lastName.equals("") || city.equals("") || state.equals("") || street.equals("") ||
zipcode.equals("") || phoneNumber.equals("")) {
message = addressBook.addInfo(null, null, null, null, null, null, null);
buildErrorFrame(message); //error message is displayed if values are null
}
//calls add info method to add contact info to book
else {
message = addressBook.addInfo(firstName, lastName, street, city, state, zipcode, phoneNumber);
addressList.setText(addressBook.toString());
//closes window once contact is added
if(message == null) {
inputFrame.dispose();
}
//shows error message if incorrect values entered
else {
buildErrorFrame(message);
}
}
}
if(enterButton.getText() == "DELETE CONTACT") {
//checks tat values are not null in text fields
if(firstName.equals("") || lastName.equals("") || city.equals("") || state.equals("") || street.equals("") ||
zipcode.equals("") || phoneNumber.equals("")) {
message = addressBook.deleteInfo(null, null, null, null, null, null, null);
buildErrorFrame(message);
}
//calls add info method to delete contact info to book
else {
message = addressBook.deleteInfo(firstName, lastName, street, city, state, zipcode, phoneNumber);
addressList.setText(addressBook.toString());
//closes window once contact is deleted
if(message == null) {
inputFrame.dispose();
}
//shows error message if incorrect values entered
else {
buildErrorFrame(message);
}
}
}
if(enterButton.getText() == "NEXT") {
//checks tat values are not null in text fields
if(firstName.equals("") || lastName.equals("") || city.equals("") || state.equals("") || street.equals("") ||
zipcode.equals("") || phoneNumber.equals("")) {
message = addressBook.checkExists(null, null, null, null, null, null, null);
buildErrorFrame(message);
}
//calls add info method to delete contact info to book
else {
message = addressBook.checkExists(firstName, lastName, street, city, state, zipcode, phoneNumber);
//closes window once contact is added and sets values for use in updating contacts
if(message == null) {
this.firstName = firstName; //holds first name to be updated
this.lastName = lastName; //holds last name to be updated
this.street = street; //holds street to be updated
this.city = city; //holds city to be updated
this.state = state; //holds state name to be updated
this.zipcode = zipcode; //holds zipcode to be updated
this.phoneNumber = phoneNumber; //holds phone number to be updated
inputFrame.dispose(); //closes the window
buildInfoFrame("Enter Updated Contact Information", "UPDATE CONTACT"); //builds new window to get updated info
return; //exits function after closing window
}
//shows error message if incorrect values entered
else {
buildErrorFrame(message);
}
}
}
if(enterButton.getText() == "UPDATE CONTACT") {
//calls the update method in the book class to update info
message = addressBook.updateInfo(this.firstName, this.lastName, this.street, this.city, this.state, this.zipcode, this.phoneNumber, firstName,
lastName, street, city, state, zipcode, phoneNumber);
addressList.setText(addressBook.toString());
//closes window once contact is updated
if(message == null) {
inputFrame.dispose();
}
//shows error message if incorrect values entered
else {
buildErrorFrame(message);
}
}
}
}
//Main method is used to run the program by creating object and calling constructor of main class
public static void main(String[] args) {
Main newBook = new Main();
}
}