-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBookDirectory.java
More file actions
183 lines (153 loc) · 4.5 KB
/
PhoneBookDirectory.java
File metadata and controls
183 lines (153 loc) · 4.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
package HW7Exercise2;
// Exercise2
import java.util.Scanner;
public class PhoneBookDirectory {
private PhoneBookEntry[] myObject;
private int count;
private Scanner input;
public PhoneBookDirectory() {
myObject = new PhoneBookEntry[6];
count = 0;
input = new Scanner(System.in);
}
// Method that returns 0 if the entry is full
public int full() {
if (count == myObject.length) {
return 0;
} else {
return 1;
}
}
// Method that finds an entry using its first name and last name
private PhoneBookEntry findEntry(String firstName, String lastName) {
for (int i = 0; i < count; i++) {
if (myObject[i].getFirstName().equalsIgnoreCase(firstName) &&
myObject[i].getLastName().equalsIgnoreCase(lastName)) {
return myObject[i];
}
}
return null;
}
// method 1: Add an entry to the phonebook
public int addEntry(PhoneBookEntry entry) {
// check if the array is full
if(full() == 0) {
System.out.println("Inventory is full. Cannot add add more entries.");
return 0; // Array is full
}
myObject[count++] = entry;
return 1; // Successfully added
}
// method 2: Print all phonebook entries
public void printEntry() {
if(count == 0) {
System.out.println("No entry in phonebook.");
return;
}
for(int i=0;i<myObject.length;i++) {
if (myObject[i] != null) {
myObject[i].printBookEntry();
System.out.println("--------------------");
}
}
}
// method 3: Search for an entry by Phone Number (Linear Search)
public int LinearSearchByPhoneNumber(String PhoneNumber) {
for(int i=0;i<myObject.length;i++) {
if(myObject[i] != null && myObject[i].getPhoneNumber().equals(PhoneNumber) ){
//return 1 if found
return 1;
}
}
//return 0 if not found
return 0;
}
// method 4: Search for an entry by id (Binary Search)
public PhoneBookEntry SearchbyIdBinarySearch(int id) {
SortbyID();
int low=0;
int high=myObject.length-1;
while(low<=high) {
int mid=(low+high)/2;
// Skip null entries in the binary search
if (myObject[mid] == null) {
high--;
continue;
}
if(myObject[mid].getId() == id) {
return myObject[mid];
}else if(myObject[mid].getId() < id) {
low = mid + 1;
}else {
high = mid - 1;
}
}
return new PhoneBookEntry();
}
// Code implemented based on concepts from Chapter Eight: Arrays
// (Professor Bari's slides)
// method 5: Sort Phone Book Entries by id (selection sort)
public void SortbyID() {
for(int i=0;i<count - 1;i++) {
int minIndex = i;
for(int j=i+1;j<count;j++) {
if(myObject[j].getId() < myObject[minIndex].getId()) {
minIndex = j;
}
}
if(minIndex != i) {
PhoneBookEntry temp = myObject[minIndex];
myObject[minIndex] = myObject[i];
myObject[i] = temp;
}
}
}
// method 6: Edit an entry
public int Edit(String firstName, String lastName) {
if (count == 0) {
// If no entries in phonebook
return 0;
}
System.out.print("Enter the last name of the entry you want to edit: ");
String LNameE = input.nextLine();
System.out.print("Enter the first name: ");
String FNameE = input.nextLine();
PhoneBookEntry entry = findEntry(FNameE,LNameE);
// returns 0 if the entry of the given userName and lastName do not exist
if (entry == null) {
return 0;
}
System.out.println("Enter new id: ");
int idE = input.nextInt();
entry.setId(idE);
input.nextLine();
System.out.println("Enter new email: ");
String emailE = input.nextLine();
entry.setEmail(emailE);
System.out.println("Enter new zip Code: ");
String zCodeE = input.nextLine();
entry.setZipCode(zCodeE);
System.out.println("Enter new phone number: ");
String pNE = input.nextLine();
entry.setPhoneNumber(pNE);
System.out.println("Entry updated successfully!");
// return a 1 if the object was found and edited
return 1;
}
// method 7: Delete an Entry of a given id
public int DeleteEntry(int id) {
for (int i = 0; i < count; i++) {
if (myObject[i] != null && myObject[i].getId() == id) {
// Reset all attributes of the entry to default values
myObject[i].setId(-1);
myObject[i].setFirstName("xxx");
myObject[i].setLastName(null);
myObject[i].setEmail(null);
myObject[i].setZipCode(null);
myObject[i].setPhoneNumber(null);
return 1; // return 1 if the Entry is deleted successfully
}
}
return 0; // return 0 if not (not found)
}
}