-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBookGUI.java
More file actions
243 lines (195 loc) · 9.5 KB
/
PhoneBookGUI.java
File metadata and controls
243 lines (195 loc) · 9.5 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
/**
*
* @author Shaw Jie Yao
*/
import javax.swing.JFrame;
import javax.swing.*;
import java.util.*;
class Contact{
String name;
String contactNumber;
String age;
// Declare instance variables
Contact(String NAME, String ConNum, String AGE) {
name = NAME;
contactNumber = ConNum;
age = AGE;
}
// Constructor (accepts two parameters: name and contact number of the person)
@Override
public String toString() {
return "\n Name: " + name + ", Contact No: " + contactNumber + ", Age of Contact: " + age;
}
// public String method toString()
public String getName() {
return name;
}
// public String method getName()
public String getNumber() {
return contactNumber;
}
// public String method getNumber()
public String getAge() {
return age;
}
public boolean equalsName(String name) {
return getName().toLowerCase().equals(name.toLowerCase());
}
// public boolean method equalsName()
}
public class PhoneBookGUI {
JFrame frame;
JLabel label;
ArrayList contacts;
PhoneBookGUI() {
this.frame = new JFrame("PhoneBook Application!");
contacts = new ArrayList();
}
public static void main(String args[]) {
PhoneBookGUI press;
String select;
JFrame frame;
press = new PhoneBookGUI();
frame = new JFrame("PhoneBook Application!");
do {
select = JOptionPane.showInputDialog (frame, """
Welcome to the PhoneBook (GUI) Application [Java Ver 1.0]
What would you like to do? Press:
(P) Print all Contact Information from the PhoneBook
(A) Add a New Contact to the Phonebook
(E) Edit Contact Information
(D) Delete a Contact from the Phonebook
(S) Search Contact Information by Name
(Q) Quit the PhoneBook Application""", "PhoneBookGUI", JOptionPane.PLAIN_MESSAGE);
switch(select) {
case "P" -> press.printAllContacts();
case "p" -> press.printAllContacts();
case "A" -> press.addContact();
case "a" -> press.addContact();
case "E" -> press.editContact();
case "e" -> press.editContact();
case "D" -> press.deleteContact();
case "d" -> press.deleteContact();
case "S" -> press.searchContact();
case "s" -> press.searchContact();
case "Q" -> {
return;
}
case "q" -> {
return;
}
default -> JOptionPane.showMessageDialog(null, "You pressed an invalid key, (P, A, E, D, S, Q) only!");
}
}
while(!select.equals("Q") && !select.equals("q"));
}
public void addContact(){
String NAME, ConNum, age;
Contact pb;
boolean successful;
NAME = JOptionPane.showInputDialog(frame, "Enter Contact Name: ", "addContact", JOptionPane.PLAIN_MESSAGE);
ConNum = JOptionPane.showInputDialog(frame, "Enter Contact Number: ", "addContact", JOptionPane.PLAIN_MESSAGE);
age = JOptionPane.showInputDialog(frame, "Enter Age of Contact: ", "addContact", JOptionPane.PLAIN_MESSAGE);
pb = new Contact(NAME, ConNum, age);
successful = contacts.add(pb);
if(successful){
JOptionPane.showMessageDialog(frame, "Contact is successfully added.", "addContact", JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(frame, "Phonebook is full!", "addContact", JOptionPane.WARNING_MESSAGE);
}
}
public void editContact() {
int c, check = 0;
String name;
name = JOptionPane.showInputDialog(frame, "Enter Contact Name to be [Updated]: ", "editContact", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(frame, "Contact is now being searched for in the Database... ", "editContact", JOptionPane.INFORMATION_MESSAGE);
for(c = 0; c < contacts.size(); c++){
Contact pb = (Contact)contacts.get(c);
if(name.equals(pb.name)){
check = 1;
pb.name = JOptionPane.showInputDialog(frame, "Enter the New Contact Name: ", "editContact", JOptionPane.PLAIN_MESSAGE);
pb.contactNumber = JOptionPane.showInputDialog(frame, "Enter the New Contact Number: ", "editContact", JOptionPane.PLAIN_MESSAGE);
pb.age = JOptionPane.showInputDialog(frame, "Enter the New Age of Contact: ", "editContact", JOptionPane.PLAIN_MESSAGE);
contacts.set(c, pb);
JOptionPane.showMessageDialog(frame, "The Contact has been successfully updated!", "editContact", JOptionPane.INFORMATION_MESSAGE);
}
}
if(check == 0){
JOptionPane.showMessageDialog(frame, "Sorry, the Contact does not exist in the Database.", "editContact", JOptionPane.WARNING_MESSAGE);
}
}
public void deleteContact() {
int c, select, check = 0;
String name;
name = JOptionPane.showInputDialog(frame, "Enter Contact Name:", "deleteContact", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(frame,"Contact is now being searched for in the Database... ", "deleteContact", JOptionPane.INFORMATION_MESSAGE);
OUTER:
for(c = 0; c < contacts.size(); c++) {
Contact pb = (Contact)contacts.get(c);
if(pb.name != null && name.equals(pb.name)) {
check = 1;
select = JOptionPane.showConfirmDialog(frame,"Are you sure you want to proceed in deleting this contact? [Note: Records may not be restored.]", "deleteContact", JOptionPane.YES_NO_OPTION);
switch(select) {
case JOptionPane.NO_OPTION -> {
JOptionPane.showMessageDialog(frame, "Aaah! Why did you change your mind?", "deleteContact", JOptionPane.QUESTION_MESSAGE);
break OUTER;
}
case JOptionPane.YES_OPTION -> {
contacts.remove(c);
JOptionPane.showMessageDialog(frame, "Contact Info was successfully deleted from the PhoneBook.", "deleteContact", JOptionPane.INFORMATION_MESSAGE);
break OUTER;
}
default -> JOptionPane.showMessageDialog(frame, "It seems you have made an invalid choice, please try again.", "deleteContact", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(check == 0){
JOptionPane.showMessageDialog(frame, "Sorry, the Contact does not exist in the Database.", "deleteContact", JOptionPane.WARNING_MESSAGE);
}
}
public void printAllContacts() {
int pac, p, count = 0;
String list, select;
list = "";
for(pac = 0; pac < contacts.size();) {
for(p = 0; count < contacts.size() && p < 10;) {
if(contacts.listIterator() != null) {
list = list + contacts.toString();
p++;
count++;
}
pac++;
}
if(contacts.size() <= count) {
break;
}
}
if(count == 0){
JOptionPane.showMessageDialog(frame, "You haven't saved any contacts yet.", "printAllContacts", JOptionPane.WARNING_MESSAGE);
}
else{
JOptionPane.showMessageDialog(frame, contacts.toString() + "\n", "printAllContacts", JOptionPane.PLAIN_MESSAGE);
}
do{
select = JOptionPane.showInputDialog(frame, "Press (G) to go back to the Main Menu...", "printAllContacts", JOptionPane.PLAIN_MESSAGE);
}
while (!select.equals("G") && !select.equals("g"));
}
public void searchContact() {
int c, contactname = 0;
String name;
name = JOptionPane.showInputDialog(frame, "Enter Contact Name: ", "searchContact", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(frame, "Contact is now being searched for in the Database... ", "searchContact", JOptionPane.INFORMATION_MESSAGE);
for(c = 0; c < contacts.size(); c++){
Contact pb = (Contact)contacts.get(c);
if(name.equals(pb.name)){
contactname = 1;
JOptionPane.showMessageDialog(frame, pb.toString(), "searchContact", JOptionPane.PLAIN_MESSAGE);
}
}
if(contactname == 0){
JOptionPane.showMessageDialog(frame, "Sorry, the Contact does not exist in the Database.", "searchContact", JOptionPane.WARNING_MESSAGE);
}
}
}