-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressBook.java
More file actions
148 lines (133 loc) · 4.83 KB
/
AddressBook.java
File metadata and controls
148 lines (133 loc) · 4.83 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
package com.company;
import java.util.*;
/**
* Address Book uses a vector data structure to hold multiple Address Entry objects
* in succession, allowing us to iterate through them and print them all out in succession.
* @author Michael LaRussa
* @since 2/15/2021
*/
public class AddressBook {
/**
* Address Entry List
* <p>
* A vector which is the data structure used to hold all of
* the Address Entry objects placed into the Address Book.
*/
Vector<AddressEntry> addressEntryList = new Vector<>();
static Scanner input = new Scanner(System.in);
/**
* Add Method
* Adds a new AddressEntry object to the addressEntryList
* @param addressEntry an AddressEntry object
*/
public void add(AddressEntry addressEntry)
{
addressEntryList.addElement(addressEntry);
}
/**
* List Method
* Sort everything in the Address Book, then iterate through addressEntryList and for each item call toString and print it out
*/
public String list()
{
addressEntryList.sort(null);
Iterator<AddressEntry> iterate = addressEntryList.iterator();
Integer count = 1;
String all = "";
while(iterate.hasNext())
{
all += "\nEntry Number " + count;
all += iterate.next().toString();
count++;
}
System.out.println(all);
return all;
}
/**
* This method does the bulk of the work when removing an entry, simply because I couldn't find an easy way to do it from the main class.
* This method will search using the search query string, and then ask to remove any Entries that may show.
* @param query The string passed to the function, which is assumed to be a Last Name, and will search the AddressBook for Last Name fields matching it.
*/
public void searchToRemove(String query)
{
Integer count = 0;
Integer numOfEntries = 1;
Integer size = addressEntryList.size();
int indexes[] = new int[size];
for (int i = 0; i < addressEntryList.size(); i++)
{
if(addressEntryList.get(i).getLastName().contentEquals(query))
{
indexes[count] = i;
count++;
}
}
if (count > 0)
{
System.out.println("The following " + count + " entries were found. Select the number of the one you wish to remove:");
for (int i = 0; i < count; i++)
{
System.out.println("(" + numOfEntries + ")");
System.out.println(addressEntryList.get(indexes[i]).toString());
numOfEntries++;
}
String in = input.nextLine();
for (int i = 0; i < count; i++) {
if (in.contentEquals(String.valueOf(i+1)))
{
System.out.println("Do you want to remove this Entry? Y/N");
System.out.println(addressEntryList.get(indexes[i]).toString());
String answer = input.nextLine();
if (answer.contentEquals("Y") || answer.contentEquals("y"))
{
addressEntryList.remove(indexes[i]);
}
else
{
System.out.println("Returning to the Main Menu...");
}
}
}
}
else {
System.out.println("No entries were found.");
}
}
/**
* This method will search using the search query string, and then display them.
* @param query The string passed to the function, which is assumed to be a Last Name, and will search the AddressBook for Last Name fields containing it.
*/
public void search(String query)
{
Integer count = 0;
Integer numOfEntries = 1;
Integer size = addressEntryList.size();
int indexes[] = new int[size];
//For loop, iterating through our entire list of Address Entries
for (int i = 0; i < addressEntryList.size(); i++)
{
//If the lastname is the search query
if(addressEntryList.get(i).getLastName().contains(query))
{
indexes[count] = i;
count++;
}
}
//If we found anything
if (count > 0)
{
//Display entries found
System.out.println("The following " + count + " entries were found.");
for (int i = 0; i < count; i++)
{
System.out.println("(" + numOfEntries + ")");
System.out.println(addressEntryList.get(indexes[i]).toString());
numOfEntries++;
}
}
//No entries were found
else {
System.out.println("No entries were found.");
}
}
}