-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressBookApplication.java
More file actions
196 lines (161 loc) · 6 KB
/
AddressBookApplication.java
File metadata and controls
196 lines (161 loc) · 6 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
package com.company;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.*;
/**
* Address Book Application is a program that can store Addresses of
* multiple people inside of it, either read from a file or user input.
* <p>
* @author Michael LaRussa
* @since 2/15/2021
*/
class AddressBookApplication {
/**
* This is just like any other main method, it runs the program.
* Currently, it is being used to test the init method.
* @param args
*/
/*
public static void main(String args[]) {
//simply invokes static methods of the Menu class
/* Exercise 1 stuff
Menu.prompt_FirstName();
Menu.prompt_LastName();
Menu.prompt_Street();
Menu.prompt_City();
Menu.prompt_State();
Menu.prompt_Zip();
Menu.prompt_Telephone();
Menu.prompt_Email();
AddressBook addressBook = new AddressBook();
init("AddressInputDataFile.txt");
//initAddressBookExercise(addressBook);
*/
/*AddressBook mainAddressBook = new AddressBook();
boolean working = true;
while(working == true)
{
String in = Menu.mainMenu();
if (in.contentEquals("a"))
{
readFromFile(mainAddressBook);
}
else if (in.contentEquals("b"))
{
addNewEntry(mainAddressBook);
}
else if (in.contentEquals("c"))
{
removeEntry(mainAddressBook);
}
else if (in.contentEquals("d"))
{
search(mainAddressBook);
}
else if (in.contentEquals("e"))
{
mainAddressBook.list();
}
else if (in.contentEquals("f"))
{
System.out.println("Goodbye!");
working = false;
}
else
{
System.out.println("That is an invalid input, please try again.");
}
}
}
*/
/**
* Creates 2 instances of Address Entry, and adds them to ab, then calls ab.list().
* @param ab An AddressBook Object passed to the function to add new Address Entries to it.
*/
public static void initAddressBookExercise(AddressBook ab)
{
AddressEntry x = new AddressEntry("Michael", "LaRussa", "Calaveras Expy.", "Milpitas", "California", 95035, "555-123-4567", "Example@gmail.com");
AddressEntry y = new AddressEntry("Nathan", "LaRussa", "Calaveras Expy.", "Milpitas", "California", 95035, "555-123-4567", "Example2@gmail.com");
ab.add(x);
ab.add(y);
ab.list();
}
/**
* Reads a text file, parses what is in it, and then places the values
* into new Address Entries, which are then placed into a new AddressBook.
* <p>
* Currently, this method will error out if the text file is not perfectly
* formatted, so I may need to tweak this.
* @param filename The name of the file that will be read.
*/
public static void init(String filename, AddressBook addressBookFile){
try{
FileReader input = new FileReader(filename);
Scanner inFile = new Scanner(input);
Integer loop = 0;
Object[] arr = new Object[8];
while(inFile.hasNextLine())
{
arr[loop] = inFile.nextLine();
loop++;
if (loop == 8) {
addressBookFile.add(new AddressEntry((String)arr[0], (String)arr[1], (String)arr[2], (String)arr[3], (String)arr[4], Integer.parseInt((String)arr[5]), (String)arr[6], (String)arr[7]));
loop = 0;
}
}
inFile.close();
addressBookFile.list();
}
catch (FileNotFoundException e){
System.out.println("An error occurred reading the file.");
}
}
static Scanner input = new Scanner(System.in);
/**
* Asks for a filename, and then gives it to our init function.
* @param ab An AddressBook object so that we don't have to create one here.
*/
public static void readFromFile(AddressBook ab) {
System.out.println("Enter filename:");
String in = input.nextLine();
init(in, ab);
}
/**
* Asks for all needed fields for a new, and then gives it to our init function.
* @param ab An AddressBook object so that we don't have to create one here.
*/
public static void addNewEntry(AddressBook ab) {
AddressEntry ae = new AddressEntry();
ae.setFirstName(Menu.prompt_FirstName());
ae.setLastName(Menu.prompt_LastName());
ae.setStreet(Menu.prompt_Street());
ae.setCity(Menu.prompt_City());
ae.setState(Menu.prompt_State());
ae.setZip(Menu.prompt_Zip());
ae.setPhone(Menu.prompt_Telephone());
ae.setEmail(Menu.prompt_Email());
System.out.println("Thank you, the following contact has been added to your list: ");
System.out.println(ae.toString());
ab.add(ae);
}
/**
* A handler function so that this code need not be present in main, and passes most of its functionality to Address Book's Search to Remove method.
* @param ab An Address Book, telling us which one to remove an entry from.
*/
public static void removeEntry(AddressBook ab) {
System.out.println("Enter the Last Name of a contact to remove: ");
String in = input.nextLine();
ab.searchToRemove(in);
}
/**
* A handler function so that this code need not be present in main, and passes most of its functionality to Address Book's Search method.
* @param ab An Address Book, telling us which one to search.
*/
public static void search(AddressBook ab) {
System.out.println("Enter the beginning of a Last Name of a contact to search by: ");
String in = input.nextLine();
ab.search(in);
}
}