-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactService.java
More file actions
119 lines (111 loc) · 3.57 KB
/
ContactService.java
File metadata and controls
119 lines (111 loc) · 3.57 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
import java.util.ArrayList;
public class ContactService {
// variables for creating objects of Contact class
public static String publicId;
public String publicFirstName;
public String publicLastName;
public String publicPhone;
public String publicAddress;
// create a structure to store data of contacts
static ArrayList<Contact> contactList = new ArrayList<Contact>(0);
// generate unique Id
public static String generateUniqueId() {
// placeholder string
String uniqueId;
// start with a 10-digit number if list is empty
if (contactList.isEmpty()) {
publicId = "1000000001";
}
//gets the latest value, therefore highest, in the arraylist if it is not empty
else {
int arraySize = contactList.size();
publicId = contactList.get(arraySize - 1).getId();
}
// convert string to int for creation of next value
int tempInt = Integer.valueOf(publicId);
// add 1 to make next value unique
tempInt += 1;
// convert back to string to store
uniqueId = Integer.toString(tempInt);
return uniqueId;
}
// creation of a Contact object and adding it to the arraylist
public static void addContact(String fName, String lName, String phone, String address) {
String ID = generateUniqueId();
Contact Contact1 = new Contact(ID, fName, lName, phone, address);
contactList.add(Contact1);
}
// add a contact as a whole manually for testing
public static void addContact(Contact newContact) {
String tempId = newContact.getId();
for (int i = 0; i < contactList.size(); i++) {
if (tempId.equals(contactList.get(i).getId())) {
throw new IllegalArgumentException("Contact ID Must Be Unique");
}
}
contactList.add(newContact);
}
// updater methods for fields that can be updated
public static void updateFirstName(String uniqueId, String publicFirstName) {
for (int i = 0; i < contactList.size(); i++) {
if (uniqueId.compareTo(contactList.get(i).getId()) == 0) {
contactList.get(i).setFirstName(publicFirstName);
}
}
}
public static void updateLastName(String uniqueId, String publicLastName) {
for (int i = 0; i < contactList.size(); i++) {
if (uniqueId.compareTo(contactList.get(i).getId()) == 0) {
contactList.get(i).setLastName(publicLastName);
}
}
}
public static void updatePhoneNum(String uniqueId, String publicPhone) {
for (int i = 0; i < contactList.size(); i++) {
if (uniqueId.compareTo(contactList.get(i).getId()) == 0) {
contactList.get(i).setPhone(publicPhone);
}
}
}
public static void updateAddress(String uniqueId, String publicAddress) {
for (int i = 0; i < contactList.size(); i++) {
if (uniqueId.compareTo(contactList.get(i).getId()) == 0) {
contactList.get(i).setAddress(publicAddress);
}
}
}
// deletes a contact from the list
public static void deleteContact(String uniqueId) {
for (int i = 0; i < contactList.size(); i++) {
if (uniqueId.compareTo(contactList.get(i).getId()) == 0) {
int position = i;
// removes the contact from list thereby deleting it since they are only
// stored in the list
contactList.remove(position);
}
}
}
// search function for testing
public static int searchContact(String uniqueId) {
int result = 0;
for (int i = 0; i < contactList.size(); i++) {
if (uniqueId.compareTo(contactList.get(i).getId()) == 0) {
result = 1;
}
else {
result = 2;
}
}
return result;
}
// find index function
public static int findIndex(String uniqueId) {
int result = 0;
for (int i = 0; i < contactList.size(); i++) {
if (uniqueId.compareTo(contactList.get(i).getId()) == 0) {
result = i;
}
}
return result;
}
}