-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
205 lines (188 loc) · 10.1 KB
/
Book.java
File metadata and controls
205 lines (188 loc) · 10.1 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.util.*;
/*
* the book class manages all address objects that are created and allows for them to be added or removes or updated
* also address book can be saved to a file
*/
public class Book {
private List<Address> addressBook; //list of address objects to store all addresses
public List<String> changeLog; //list of any updates and deletions form the address book
//constructor is called to create a book object and initialize the address object list
Book(){
//the address list is initialized
addressBook = new ArrayList<Address>();
//change log list is initialized;
changeLog = new ArrayList<String>();
}
//addInfo method adds an address to the list and verifies the parameters passed in. return true if info was added
public String addInfo(String firstName, String lastName, String street, String city, String state, String zipcode,
String phoneNumber) throws IncorrectInfoException {
//try/catch statement used to throw error if the contact info is not correct then method returns false
try {
//if statement checks for null values, throws exception if null value is found
if(firstName != null && lastName != null && street != null && city != null && state != null && zipcode != null && phoneNumber != null) {
//checks that the state uses 2 letter abbreviation
if(state.length() == 2) {
//checks to see that the zipcode is the correct length and also a set of numbers
if(zipcode.length() == 5 &&Integer.parseInt(zipcode) <= 99999) {
//checks to see that the phone is the correct length and also a set of numbers
if(phoneNumber.length() == 10 && Long.parseLong(phoneNumber) <= 9999999999L) {
Address newAddress = new Address(firstName, lastName, street, city, state, zipcode, phoneNumber); //create address object
addressBook.add(newAddress); //add object to address book list
return null;
}
else {
throw new IncorrectInfoException(2); //throws an exception if the info is incorrect. parameter 2 = error with phone number
}
}
else {
throw new IncorrectInfoException(3); //throws an exception if the info is incorrect. parameter 3 = error with zipcode
}
}
else {
throw new IncorrectInfoException(4); //throws an exception if the info is incorrect. parameter 4 = error with state
}
}
else {
throw new IncorrectInfoException(5); //throws an exception if the info is incorrect. parameter 5 = null value given
}
}
catch (Exception e){
return e.toString(); //return false if error is thrown
}
}
//deleteInfo method deletes an address from the list after it verifies the parameters passed in. return true if info was deleted
public String deleteInfo(String firstName, String lastName, String street, String city, String state, String zipcode,
String phoneNumber) throws IncorrectInfoException{
//System.out.println(firstName + lastName + street +city + state + zipcode + phoneNumber);
//try/catch statement used to throw error if the contact info is not found then method returns false
try {
if(firstName != null && lastName != null && street != null && city != null && state != null && zipcode != null && phoneNumber != null) {
//loop iterates to find the contact to delete.
for(int count = 0; count < addressBook.size(); count++){
if(addressBook.get(count).getFirstName().equalsIgnoreCase(firstName) && addressBook.get(count).getLastName().equalsIgnoreCase(lastName) &&
addressBook.get(count).getStreet().equalsIgnoreCase(street) && addressBook.get(count).getCity().equalsIgnoreCase(city) &&
addressBook.get(count).getState().equalsIgnoreCase(state) && addressBook.get(count).getZipcode().equalsIgnoreCase(zipcode) &&
addressBook.get(count).getPhoneNumber().equalsIgnoreCase(phoneNumber)) {
recordChange(count, "Record Deleted: "); //calls method to record the change
addressBook.remove(count); //removes the contact form the device
return null;
}
else {
throw new IncorrectInfoException(1); //throws an exception if the info is not found. parameter 1 = incorrect info
}
}
}
else {
throw new IncorrectInfoException(5); //throws an exception if the info is incorrect. parameter 5 = null value given
}
}
catch (Exception e){
return e.toString();
}
return null;
}
//updateInfo method updates an address in the list after it verifies the parameters passed in. return true if info was updated
public String updateInfo(String firstName, String lastName, String street, String city, String state, String zipcode,
String phoneNumber, String newFirstName, String newLastName, String newStreet, String newCity, String newState, String newZipcode, String newPhoneNumber) throws IncorrectInfoException {
//try/catch statement used to throw error if the contact info is not found then method returns false
try {
//checks that non of the values passed in are null
if(firstName != null && lastName != null && street != null && city != null && state != null && zipcode != null && phoneNumber != null &&
newStreet != null && newCity != null && newState != null && newZipcode != null && newPhoneNumber!= null && newFirstName != null && newLastName != null) {
//checks that the state uses 2 letter abbreviation
if(newState.length() == 2) {
//checks to see that the zipcode is the correct length and also a set of numbers
if(newZipcode.length() == 5 &&Integer.parseInt(newZipcode) <= 99999) {
//checks to see that the phone is the correct length and also a set of numbers
if(newPhoneNumber.length() == 10 && Long.parseLong(newPhoneNumber) <= 9999999999L) {
//loop iterates to find the contact to delete.
for(int count = 0; count < addressBook.size(); count++){
if(addressBook.get(count).getFirstName().equalsIgnoreCase(firstName) && addressBook.get(count).getLastName().equalsIgnoreCase(lastName) &&
addressBook.get(count).getStreet().equalsIgnoreCase(street) && addressBook.get(count).getCity().equalsIgnoreCase(city) &&
addressBook.get(count).getState().equalsIgnoreCase(state) && addressBook.get(count).getZipcode().equalsIgnoreCase(zipcode) &&
addressBook.get(count).getPhoneNumber().equalsIgnoreCase(phoneNumber)) {
recordChange(count, "Record Updated: "); //calls method to record the change
addressBook.get(count).setFirstName(newFirstName); //changes the first name to a new first name
addressBook.get(count).setLastName(newLastName); //changes the last name to a new last name
addressBook.get(count).setStreet(newStreet); //changes the street name to a new street name
addressBook.get(count).setCity(newCity); //changes the city to a new city
addressBook.get(count).setState(newState); //changes the state to a new state
addressBook.get(count).setZipcode(newZipcode); //changes the zipcode to a new zipcode
addressBook.get(count).setPhoneNumber(newPhoneNumber); //changes the phone number to a new phone number
return null;
}
}
}
else {
throw new IncorrectInfoException(2); //throws an exception if the info is incorrect. parameter 2 = error with phone number
}
}
else {
throw new IncorrectInfoException(3); //throws an exception if the info is incorrect. parameter 3 = error with zipcode
}
}
else {
throw new IncorrectInfoException(4); //throws an exception if the info is incorrect. parameter 4 = error with state
}
throw new IncorrectInfoException(1); //throws an exception if the info is not found. parameter 1 = incorrect info
}
else {
throw new IncorrectInfoException(5); //throws an exception if the info is incorrect. parameter 5 = null value given
}
}
catch (Exception e){
return e.toString();
}
}
public String checkExists(String firstName, String lastName, String street, String city, String state, String zipcode,
String phoneNumber) throws IncorrectInfoException {
//try/catch statement used to throw error if the contact info is not found then method returns error
try {
if(firstName != null && lastName != null && street != null && city != null && state != null && zipcode != null && phoneNumber != null) {
//loop iterates to find the contact to delete.
for(int count = 0; count < addressBook.size(); count++){
if(addressBook.get(count).getFirstName().equalsIgnoreCase(firstName) && addressBook.get(count).getLastName().equalsIgnoreCase(lastName) &&
addressBook.get(count).getStreet().equalsIgnoreCase(street) && addressBook.get(count).getCity().equalsIgnoreCase(city) &&
addressBook.get(count).getState().equalsIgnoreCase(state) && addressBook.get(count).getZipcode().equalsIgnoreCase(zipcode) &&
addressBook.get(count).getPhoneNumber().equalsIgnoreCase(phoneNumber)) {
return null;
}
else {
throw new IncorrectInfoException(1); //throws an exception if the info is not found. parameter 1 = incorrect info
}
}
}
else {
throw new IncorrectInfoException(5); //throws an exception if the info is incorrect. parameter 5 = null value given
}
}
catch (Exception e){
return e.toString();
}
return null;
}
//this method adds the changed made to the book to the log
public void recordChange(int index, String changeType) {
changeLog.add(changeType + addressBook.get(index).toString()); //change is added to the change log
}
//returns the address book object
public List<Address> getAddressBook() {
return addressBook;
}
//toString method writes the contents of the address Book in a formated string
@Override
public String toString() {
String bookList = ""; //holds all of the addresses in a single string to return
//iterates through the address book list and adds contents to the return string
for(int count = 0; count < addressBook.size(); count++){
//removes next line from the first address and prints all addresses on a separate line
if(count == 0) {
bookList = bookList + addressBook.get(count).toString();
}
else {
bookList = bookList + "\n" + addressBook.get(count).toString();
}
}
return bookList;
}
}