-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListPhoneBook.java
More file actions
132 lines (117 loc) · 4.02 KB
/
ListPhoneBook.java
File metadata and controls
132 lines (117 loc) · 4.02 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
import java.util.LinkedList;
import java.util.ListIterator;
/**
* Simple list implementation of the PhoneBook interface.
* @author Christine Zarges
* @version 1.0, 26th October 2017
*/
public class ListPhoneBook implements PhoneBook {
/**
* Nested class that stores contact information.
*/
private class Contact {
private int phoneNumber;
private String name;
public Contact(int phoneNumber,String name) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
private LinkedList<Contact> phoneBook;
/**
* A default constructor. Creates a List.
*/
public ListPhoneBook() {
phoneBook = new LinkedList<Contact>();
}
/**
* Method to look up the owner of phone number.
*
* @param phoneNumber the phone number whose owner is to be returned
* @return the name of the owner, or null if the number does not exist
*/
@Override
public String search(int phoneNumber) {
// search for phone number
for (Contact c : phoneBook) {
if (c.getPhoneNumber() == phoneNumber) {
return c.getName();
}
}
return null;
}
/**
* Method to add a new contact with phone number and name to the phone book. If the phone number already exists,
* the query is ignored. Otherwise the new contact is added. Returns a boolean indicating if adding the contact
* was successful.
*
* @param phoneNumber the phone number of the new contact
* @param name the name in the new contact
* @return true if the entry was added, false if the phone number already exists
*/
@Override
public boolean add(int phoneNumber, String name) {
// search for phone number
for (Contact c : phoneBook) {
if (c.getPhoneNumber() == phoneNumber) {
return false;
}
}
// add new contact if phone number does not exist
Contact newContact = new Contact(phoneNumber, name);
phoneBook.add(newContact);
return true;
}
/**
* Method to update the name associated with a given phone number. If the phone number does not exist, the
* query is ignored. Otherwise, the associated name is updated and the old name returned.
*
* @param phoneNumber the phone number of the contact to be updated
* @param name the new name of the contact
* @return the old name if the entry was updated, null if the phone number does not exist
*/
@Override
public String update(int phoneNumber, String name) {
// search for phone number
for (Contact c : phoneBook) {
if (c.getPhoneNumber() == phoneNumber) {
// update if found
String oldName = c.getName();
c.setName(name);
return oldName;
}
}
return null;
}
/**
* Method to remove a contact with a given phone number.
*
* @param phoneNumber the phone number of the contact to be deleted
* @return the name of the associated person, or null if the name does not exist
*/
@Override
public String remove(int phoneNumber) {
// search for phone number
ListIterator<Contact> iterator = phoneBook.listIterator();
while(iterator.hasNext()){
Contact c = iterator.next();
if(c.getPhoneNumber() == phoneNumber){
iterator.remove();
return c.getName();
}
}
return null;
}
}