From: Echo_
Copy constructor for remove:
public Contact(Contact copy) {
firstName = new String(copy.getFirstName());
lastName = new String(copy.getLastName());
address = new String(copy.getAddress());
city = new String(copy.getCity());
state = new String(copy.getState());
email = new String(copy.getEmail());
phone = new String(copy.getPhone());
zip = copy.getZip();
}
Be careful when suggesting Copy Constructors. He is new so if he goes and implements copy constructors in java without knowing what is immutable, it will screw him as java only copies references. Luckily for him, your copy constructor is using primitive types and immutable structures. Would be nice to put a notice for other new guys reading.
@OP, I do not know if re-sizing is required when importing or if the teacher expects you to wipe out the current contacts and replace them with the imported ones. Thus I implemented re-sizing anyway. Is there any reason that the teacher assigned you such a task without teaching you the basics?
Add these imports to the addressbook class:
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
Add these to your Address book class:
private int EmptyIndex() {
for (int I = 0; I < Contacts.length; ++I) {
if (Contacts[I] == null) {
return I;
}
}
return -1;
}
private void Resize(int NewSize) {
Contact[] ContactArray = new Contact[NewSize];
for (int I = 0; I < Contacts.length; ++I) {
ContactArray[I] = Contacts[I];
}
Contacts = null;
Contacts = ContactArray;
}
public boolean importContacts(String inputFileName) {
try {
List<String> Info = Files.readAllLines(Paths.get(inputFileName), StandardCharsets.UTF_8);
int Index = EmptyIndex();
if (Info.size() >= Contacts.length || Index == -1) {
if (Index == -1 || Info.size() == Contacts.length) {
Resize(Contacts.length + Info.size());
} else {
Resize(Contacts.length + (Info.size() - Contacts.length));
}
}
for (int I = 0; I < Info.size(); ++I) {
String[] Parameters = Info.get(I).split(",");
if (Parameters != null) {
int NewIndex = EmptyIndex();
if (NewIndex != -1) {
for (int J = 0; J < Parameters.length; ++J) {
Parameters[J] = Parameters[J].trim();
}
int Zip = Parameters[5] == null ? 0 : Integer.valueOf(Parameters[5]);
Contacts[NewIndex] = new Contact(Parameters[0], Parameters[1], Parameters[2], Parameters[3], Parameters[4], Parameters[7], Parameters[6], Zip);
}
}
}
for (int I = 0; I < Contacts.length; ++I) {
System.out.println(Contacts[I].toString());
}
} catch (Exception e) {
return false;
}
return true;
}
public boolean export(String outputFileName) {
if (Contacts.length > 0) {
String Info = "";
for (int I = 0; I < Contacts.length; ++I) {
if (Contacts[I] != null) {
Info += Contacts[I].GetFirstName() + ", " + Contacts[I].GetLastName() + ", " + Contacts[I].GetAddress()
+ ", " + Contacts[I].GetCity() + ", " + Contacts[I].GetState() + ", " + Contacts[I].GetZip()
+ ", " + Contacts[I].GetPhone() + ", " + Contacts[I].GetEmail() + "\r\n";
}
}
if (!Info.isEmpty()) {
try {
if (Files.exists(Paths.get(outputFileName), LinkOption.NOFOLLOW_LINKS)) {
System.out.println("The file already exists.. Would you like to overwrite it? yes/no");
BufferedReader Buffer = new BufferedReader(new InputStreamReader(System.in));
String Answer = Buffer.readLine().trim();
if (Answer.toLowerCase().equals("no")) {
return false;
}
}
Files.write(Paths.get(outputFileName), Info.getBytes(), StandardOpenOption.CREATE);
System.out.println("Overwritten");
return true;
} catch (Exception e) {
return false;
}
}
}
return false;
}
Now I tend to write code without comments and sometimes it can get quite complex. I don't know if it is complex to you or not but if it is, drop a line below and either myself or someone would explain it to you.