-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactList.java
More file actions
129 lines (129 loc) · 4.09 KB
/
ContactList.java
File metadata and controls
129 lines (129 loc) · 4.09 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
/**
* One object of this class represents a list of contacts
* each with their own information. //RS
*/
import java.util.*;
import java.io.*;
public class ContactList implements Serializable, Comparator<Contact>{
private Scanner scanner = new Scanner(System.in); //RS
private List<Contact> contactList = new ArrayList<Contact>(); //JJ
/**
* Adds a contact to the array of contacts from user input.
*/
public void addContact() { //JJ and RS
String firstName = "", lastName = "", street = "",
email = "", phone = "", notes = "";
System.out.print("First name: ");
firstName = scanner.nextLine();
System.out.print("Last name: ");
lastName = scanner.nextLine();
System.out.print("Street address: ");
street = scanner.nextLine();
System.out.print("E-mail address: ");
email = scanner.nextLine();
System.out.print("Phone number: ");
phone = scanner.nextLine();
System.out.print("Notes: ");
notes = scanner.nextLine();
if (lastName.length() == 0) {
System.out.println("Last name is needed, try again");
}
else {
Contact contact = new Contact(firstName, lastName, street, email,
phone, notes);
contactList.add(contact);
System.out.println("Contact added successfully!");
}
}
/**
* Prints the entire array of contacts in alphabetical order
* according to last name.
*/
public void viewContacts() { //RS and JJ
Collections.sort(contactList, new ContactList());
if (contactList.size() == 0) {
System.out.println("No contacts exist");
}
else {
for (int index = 0; index < contactList.size(); index++) {
System.out.print(contactList.get(index));
}
}
}
/**
* Compares contact objects from the array of contacts.
*/
public int compare(Contact person1, Contact person2) { //WW
return person1.getLastName().compareTo(person2.getLastName());
}
/**
* Prints the contacts with the specified last name
* if they exist.
*/
public void findContact(){ //WW, RS and JJ
System.out.print("Last name: ");
String search = scanner.nextLine();
int noContact = 0;
if (contactList.size() != 0){
for (Contact contact : contactList) {
if(contact.getLastName() != null &&
contact.getLastName().contains(search)) {
System.out.print(contact);
++noContact;
}
}
}
else{
System.out.print("No contacts are listed on the file.");
}
if(noContact<1){
System.out.println("\nNo contact was found with this last name.");
}
}
/**
* Saves the contact list object to disk.
*/
public void saveToDisk(String fileName) throws IOException{ //JJ and RS
File outFile = new File(fileName);
FileOutputStream outFileStream;
ObjectOutputStream outObject;
try {
outFileStream = new FileOutputStream (outFile);
outObject = new ObjectOutputStream (outFileStream);
outObject.writeObject(contactList);
outFileStream.close();
outObject.close();
System.out.println("The list of contacts has been saved successfully!");
}
catch (IOException ioe) {
System.out.println ("Error writing objects to the file: "
+ ioe.getMessage());
}
}
/**
* Loads the contact list object from an existing file.
*/
public void readFromDisk(String fileName) throws IOException{ //JJ and RS
FileInputStream inFileStream;
ObjectInputStream inObject;
try {
File inFile = new File(fileName);
if(inFile.exists()){
System.out.println("The file " + fileName +
" has been found and loaded successfully!");
inFileStream = new FileInputStream (inFile);
inObject = new ObjectInputStream(inFileStream);
contactList = (List<Contact>) inObject.readObject();
inFileStream.close();
inObject.close();
}
else {
System.out.println("No file exists, please create a new contact list");
}
}
catch (ClassNotFoundException cnfe) {
System.out.println ("Error in casting to ContactList: "
+ cnfe);
}
}
}