Answer the question
In order to leave comments, you need to log in
HOW to sort the contents of the book bodies using the Comparable interface?
package com.m46academy.telbook;
import com.m46academy.telbook.database.Database;
import com.m46academy.telbook.database.implementation.SimpleFileDatabase;
import com.m46academy.telbook.exception.*;
import com.m46academy.telbook.model.Person;
import com.m46academy.telbook.model.Searchable;
import com.m46academy.telbook.model.personalinfo.PersonalInfo;
import java.io.IOException;
public class TelBook{
private String owner;
private Person[] persons = new Person[10];
private int largestIndex = 0;
private Database database = new SimpleFileDatabase();
// public Person[] getPersons() {
// return persons;
// }
public int length() {
return largestIndex;
}
public void add(Person person) throws IOException, EmptyPersonException, TelBookFullException {
if (largestIndex < 10 && largestIndex >= 0) {
persons[largestIndex] = person;
database.createPerson(person, largestIndex);
largestIndex++;
} else if (person == null || (person.getPersonalInfos() == null && person.getContacts() == null)) {
throw new EmptyPersonException();
} else if (largestIndex >= 10) {
throw new TelBookFullException();
}
}
//STEP 12 - START
public void replacePerson(int index, Person person) throws ArrayIndexOutOfBoundsException, EmptyPersonException, PersonNotFoundException, CannotSavePersonException {
if (index <= 0 || index >= 10) {
ArrayIndexOutOfBoundsException exception = new ArrayIndexOutOfBoundsException();
throw exception;
} else if (person == null || (person.getContacts() == null && person.getPersonalInfos() == null)) {
EmptyPersonException exception = new EmptyPersonException();
throw exception;
}
Person oldPerson = persons[index];
persons[index] = person;
try {
database.updatePerson(person, index);
} catch (PersonNotFoundException e) {
throw e;
} catch (IOException e) {
persons[index] = oldPerson;
throw new CannotSavePersonException();
}
}
//STEP 12 - END
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public void loadData() throws IOException, InputValidationException {
Person[] personsFromReadAll = database.readAll();
largestIndex = personsFromReadAll.length;
this.persons = new Person[10];
for (int i = 0; i < largestIndex; i++) {
this.persons[i] = personsFromReadAll[i];
}
}
public Person getPerson(int index) throws ArrayIndexOutOfBoundsException {
if (index < 0 || index > largestIndex - 1) {
throw new ArrayIndexOutOfBoundsException();
}
return persons[index];
}
//STEP 12 - START
public void delete(int index) throws ArrayIndexOutOfBoundsException, PersonNotFoundException, CannotDeletePersonException {
database.deletePerson(index);
persons[index] = null;
}
//STEP 12 - END
//STEP 13 - START
public Person[] search(String keyword) {
Person[] tmpFoundPersons = new Person[persons.length];
int lastFoundPersonsIndex = 0;
for (int i=0; i<persons.length; i++) {
if (persons[i]!=null) {
Searchable searchable = persons[i];
if (searchable.search(keyword)) {
tmpFoundPersons[lastFoundPersonsIndex] = persons[i];
lastFoundPersonsIndex++;
}
}
}
Person[] foundPersons = new Person[lastFoundPersonsIndex];
for (int i=0; i<foundPersons.length; i++) {
foundPersons[i] = tmpFoundPersons[i];
}
return foundPersons;
}
//STEP 13 - END
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question