-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
152 lines (143 loc) · 5.22 KB
/
Copy pathBook.java
File metadata and controls
152 lines (143 loc) · 5.22 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
package phonebookProject;
import java.io.IOException;
import java.util.Scanner;
public class Book {
protected static Person[] pBook = new Person[0];
protected Scanner in = new Scanner(System.in);
//Case 1
public Person[] addContact(Person person) {
Person[] temp = new Person[pBook.length + 1];
for (int i = 0; i < pBook.length; i++) {
temp[i] = pBook[i];
}
temp[temp.length - 1] = person;
return pBook = temp;
}
//Case 2
public static Person findByFirstName() throws IOException {
System.out.print("Enter Contact's First Name: ");
Scanner in = new Scanner (System.in);
String firstNameFound = in.nextLine();
for (int i = 0; i <= pBook.length - 1; i++) {
if (pBook[i].getFirstName().equalsIgnoreCase(firstNameFound)) {
return pBook[i];
}
}
throw new IOException("Contact Does Not Exist");
}
public static Person findByLastName() throws IOException {
System.out.print("Enter Contact's Last Name: ");
Scanner in = new Scanner (System.in);
String lastNameFound = in.nextLine();
for (int i = 0; i <=pBook.length - 1; i++) {
if (pBook[i].getLastName().equalsIgnoreCase(lastNameFound)) {
return pBook[i];
}
}
throw new IOException("Contact Does Not Exist");
}
public static Person findByFullName() throws IOException {
System.out.print("Enter the Contact's full Name: ");
Scanner in = new Scanner (System.in);
String firstNameFound = in.nextLine();
System.out.print("Enter Contact's Last Name: ");
String lastNameFound = in.nextLine();
for(Person person : pBook) {
if(person.getFirstName().equals(firstNameFound) && person.getLastName().equals(lastNameFound)) {
return person;
}
}
throw new IOException("Contact Does Not Exist");
}
public static Person findByPhoneNumber() throws IOException {
System.out.print("Enter Contact's Phone Number: ");
Scanner in = new Scanner (System.in);
String phoneNumberFound = in.nextLine();
for(Person person : pBook) {
if(person.getPhoneNumber().equals(phoneNumberFound)) {
return person;
}
}
throw new IOException("Contact Does Not Exist");
}
public static Person findByCity() throws IOException {
System.out.print("Enter Contact's City: ");
Scanner in = new Scanner (System.in);
String cityFound = in.nextLine();
for(Person person : pBook) {
if(person.getCity().equals(cityFound)) {
return person;
}
}
throw new IOException("Contact Does Not Exist");
}
public static Person findByState() throws IOException {
System.out.print("Enter Contact's State: ");
Scanner in = new Scanner (System.in);
String stateFound = in.nextLine();
for(Person person : pBook) {
if(person.getState().equals(stateFound)) {
return person;
}
}
throw new IOException("Contact Does Not Exist");
}
//Case 3
public static Person replaceByPhoneNumber() throws IOException {
System.out.print("Enter Contact's Phone Number: ");
Scanner in = new Scanner (System.in);
String replacePhoneNumber = in.nextLine();
for(int i = 0; i < pBook.length; i++) {
if(pBook[i].getPhoneNumber().equals(replacePhoneNumber)) {
return updateContact(i);
}
}
throw new IOException("Contact Does Not Exist");
}
public static Person updateContact(int index){
Scanner in = new Scanner (System.in);
System.out.print("Enter Contact's First Name: ");
String firstName = in.nextLine();
System.out.print("Enter Contact's Last Name: ");
String lastName = in.nextLine();
System.out.print("Enter Contact's Phone Number: ");
String phoneNumber = in.nextLine();
System.out.print("Enter Contact's email: ");
String email = in.nextLine();
System.out.print("Enter Contact's Street Number: ");
String streetNum = in.nextLine();
System.out.print("Enter Contact's Street Name: ");
String streetName = in.nextLine();
System.out.print("Enter Contact's City: ");
String city = in.nextLine();
System.out.print("Enter Contact's State: ");
String state = in.nextLine();
Address address1 = new Address(streetNum, streetName, city, state);
Person person1 = new Person(firstName, lastName, phoneNumber, email, address1);
pBook[index] = person1;
return pBook[index];
}
//Case 4
public static boolean deleteByPhoneNumber() throws ArrayIndexOutOfBoundsException {
System.out.print("Enter Contact's Phone Number: ");
Scanner in = new Scanner (System.in);
String deletePhoneNumber = in.nextLine();
Person[] temp = new Person[0];
if (pBook.length > 0) {
temp = new Person[pBook.length - 1];
}
for(int i = 0; i < pBook.length; i++) {
if(!pBook[i].getPhoneNumber().equals(deletePhoneNumber)) {
temp[i] = pBook[i];
}
}
pBook = temp;
return true;
}
//Case 5
public static void printAllContacts() {
for(Person person : pBook) {
System.out.println(person.toString());
}
}
}