-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobilePhone.java
70 lines (59 loc) · 1.94 KB
/
MobilePhone.java
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
package com.JHTuhin;
import java.util.ArrayList;
public class MobilePhone {
private String myNumber;
private ArrayList<Contact> myContacts;
public MobilePhone(String myNumber) {
this.myNumber = myNumber;
this.myContacts = new ArrayList<Contact>();
}
public boolean addNewContact(Contact contact){
if(findContact(contact.getName())>=0){
return false;
}
myContacts.add(contact);
return true;
}
public boolean updateContact(Contact oldContact, Contact newContact){
int foundPosition=findContact(oldContact);
if((foundPosition<0)||(findContact(newContact.getName())!=-1)){
return false;
}
this.myContacts.set(foundPosition,newContact);
return true;
}
public boolean removeContact(Contact contact){
int foundPosition=findContact(contact);
if(foundPosition<0){
return false;
}
this.myContacts.remove(contact);
return true;
}
public Contact queryContact(String name){
int foundPosition=findContact(name);
if(foundPosition>=0){
return this.myContacts.get(foundPosition);
}
return null;
}
public void printContacts() {
System.out.println("Contact List");
for(int i=0; i<this.myContacts.size(); i++){
System.out.println((i+1)+". "+myContacts.get(i).getName()
+" -> "+myContacts.get(i).getPhoneNumber());
}
}
private int findContact(Contact contact){
return myContacts.indexOf(contact);
}
public int findContact(String contactName){
for(int i=0;i<this.myContacts.size();i++){
Contact contact=this.myContacts.get(i);
if(contact.getName().equals(contactName)){
return i;
}
}
return -1;
}
}