-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContact Application
More file actions
229 lines (186 loc) · 5.23 KB
/
Copy pathContact Application
File metadata and controls
229 lines (186 loc) · 5.23 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
package com.alekha.modular;
import java.util.ArrayList;
public class Contacts {
private String name;
private String phoneNumber;
private String email;
public Contacts(String name,String phoneNumber,String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String getEmail() {
return email;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public static Contacts createContact(String name,String phoneNumber,String email) {
return new Contacts(name,phoneNumber,email);
}
public static Contacts updateContact(String name, String phoneNumber,String email) {
return new Contacts(name,phoneNumber,email);
}
}
package com.alekha.modular;
import java.util.ArrayList;
import java.util.Iterator;
public class MobilePhone {
ArrayList<Contacts> myContacts = new ArrayList<Contacts>();
/*
* public MobilePhone() { this.myContacts = new ArrayList<Contacts>(); }
*/
public boolean addNewContact(Contacts contact) {
if(findContact(contact.getName()) >= 0) {
System.out.println("Name is already added");
}
else if(findPhone(contact.getPhoneNumber())>= 0) {
System.out.println("PhoneNumber is already added");
}
myContacts.add(contact);
return true;
}
public boolean updateContact(Contacts oldContact , Contacts newContact) {
int foundPosition = findContact(oldContact);
if(foundPosition < 0) {
return false;
}
this.myContacts.set(foundPosition, newContact);
System.out.println(newContact.getName());
return true;
}
public boolean removeContact(Contacts contact) {
int foundPosition = findContact(contact);
if(foundPosition < 0) {
return false;
}
this.myContacts.remove(foundPosition);
return true;
}
private int findContact(Contacts contact) {
return this.myContacts.indexOf(contact);
}
private int findContact(String contactName) {
for(int i =0; i < myContacts.size(); i++) {
Contacts contacts = this.myContacts.get(i);
if(contacts.getName().equals(contactName)) {
return i;
}
}
return -1;
}
private int findPhone(String contactPhoneNumber) {
for(int i =0; i < myContacts.size(); i++) {
Contacts contacts = this.myContacts.get(i);
if(contacts.getPhoneNumber().equals(contactPhoneNumber)) {
return i;
}
}
return -1;
}
/*
* public String queryContact(Contacts contact) { if(findContact(contact) >= 0)
* { return contact.getName(); } return null; }
*/
public Contacts checkContact(String name) {
int position = findContact(name);
if(position >=0) {
return this.myContacts.get(position);
}
return null;
}
public void displayContacts() {
System.out.println("Contact list");
Iterator<Contacts> itr = myContacts.iterator();
while(itr.hasNext()) {
Contacts b = itr.next();
System.out.println(b.getName());
System.out.println(b.getPhoneNumber());
System.out.println(b.getEmail());
System.out.println(" ");
}
}
}
package com.alekha.modular;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static MobilePhone mp = new MobilePhone();
static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int choice =0;
do {
System.out.println("_____Contact Details______");
System.out.println("1: Add a new contact");
System.out.println("2: Update contact");
System.out.println("3: Remove the contact");
System.out.println("4: Display the contact");
choice = Integer.parseInt(sc.readLine());
switch (choice) {
case 1:
addNewContact();
break;
case 2:
updateContact();
break;
case 3:
removeContact();
break;
case 4:
mp.displayContacts();
break;
default:
System.out.println("None of these");
}
}
while(choice !=5);
}
private static void addNewContact() throws IOException {
String name = sc.readLine();
String phone = sc.readLine();
String email = sc.readLine();
Contacts newContact = Contacts.createContact(name, phone, email);
if (mp.addNewContact(newContact)) {
System.out.println("name = "+ name + " "+ " phone = " + phone + " "+ " Email = "+ email);
} else {
System.out.println("Can't add, already on file");
}
}
private static void updateContact() throws IOException {
String name = sc.readLine();
Contacts extContact = mp.checkContact(name);
if(extContact == null)
{
System.out.println("Cannot found contact");
return;
}
String newName = sc.readLine();
String newNumber = sc.readLine();
String email = sc.readLine();
Contacts newContact = Contacts.updateContact(newName, newNumber,email);
if(mp.updateContact(extContact, newContact))
{
System.out.println("Successfully updated");
}else {
System.out.println("Error Updating Contact");
}
}
private static void removeContact() throws IOException {
String name = sc.readLine();
Contacts exContact = mp.checkContact(name);
if(exContact == null)
{
System.out.println("Cannot found contact");
return;
}
if(mp.removeContact(exContact)) {
System.out.println("Successfully deleted");
}else {
System.out.println("Error Deleting Contact");
}
}
}