In: Computer Science
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String), phoneNumber (String), and address (String). 2. Implement the initialization constructor . 3. Implement the setters and getters for all attributes. 4. Implement the toString() method to display all attributes. 5. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. 6. Implement the compareTo (Entry other) method that returns 0 if the two entries have the same number, -1 if this.number is smaller than other.number, and + 1 if this.number is > other.number Class PhoneBook 1. Implement the class PhoneBook that has a city (String), type (String), entryList (an Array of Entry). Assume the array size = 10 2. Implement the constructors to initialize the city and type. 3. Implement the method addEntryToPhoneBook(Entry e) that adds an entry to the phone book. 4. Implement the method toString() that displays all attributes and all enties information of the phone book. 5. Implement the method displayAll() that displays all entries in the phone book. 6. Implement the method checkEntryByNumber(String number) that takes a number and returns the entry with that number if it exists. 7. Implement the method checkEntrisByName(String name) that takes a name, and returns an array of the entries that start with this name or letters. 8. Implement the method removeEntryFromPhoneBook (String number) that removes an entry from the array of entires in the phone book. Class TestPhoneBook 1. Create a phone book and initialize its attributes. 2. Add 5 entries to the phone book list using the method addEntryToPhoneBook() 3. Display all entries in the phone book. 4. Display the entry information for a given phone number if it exists. Give the appropriate message if it does not exist. 5. Display all entries starting with the letters "Abd" 6. Remove a specific entry from the phone book. Display the proper message if it was removed , or if it did not exist. 7. Sort and display the entries of the phone book (Bonus). Java NetBeans.
package phonebook;
public class Entry {
String name;
String phoneNumber;
String address;
public Entry(String name, String phoneNumber, String address) {
super();
this.name = name;
this.phoneNumber = phoneNumber;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Entry [name=" + name + ", phoneNumber=" + phoneNumber + ", address=" + address + "]";
}
public boolean equals(Entry e){
return e.name.equals(this.name) && e.address.equals(this.address) && e.phoneNumber.equals(this.phoneNumber);
}
public int compareTo(Entry e){
if(this.phoneNumber.compareTo(e.phoneNumber)==0)
return 0;
else if(this.phoneNumber.compareTo(e.phoneNumber)==-1)
return -1;
return 1;
}
}
phonebook class
package phonebook;
import java.util.ArrayList;
public class PhoneBook {
String city;
String type;
Entry entryList[]=new Entry[10];
int ind=0;
public PhoneBook(String city, String type) {
super();
this.city = city;
this.type = type;
}
public void addEntryToPhoneBook(Entry e){
if((ind)<entryList.length){
entryList[ind++]=e;
}
else{
System.out.println("Phone book is full");
}
}
@Override
public String toString() {
return "PhoneBook [city=" + city + ", type=" + type + "]\n"+
"Entries"+entryList.toString();
}
public void displayAll(){
for(Entry e:entryList){
if(e!=null)
System.out.println(e);
}
}
public Entry checkEntryByNumber(String number){
for(Entry e:entryList){
if(e.phoneNumber.equals(number))
return e;
}
return null;
}
public Object[] checkEntrisByName(String name) {
ArrayList<Entry> res=new ArrayList<>();
for(Entry e:entryList){
if(e!=null && e.name.startsWith(name))
res.add(e);
}
if(!res.isEmpty()){
return res.toArray();
}
return null;
}
public boolean removeEntryFromPhoneBook(String number){
for(int i=0;i<entryList.length-1;i++){
if(entryList[i].phoneNumber.equals(number)){
for(int j=i+1;j<entryList.length-1;j++){
entryList[j-1]=entryList[j];
}
entryList[entryList.length-1]=null;
return true;
}
}
return false;
}
}
TestPhoneBook class
package phonebook;
import java.util.Collections;
public class TestPhoneBook {
public static void main(String[] args) {
PhoneBook p=new PhoneBook("NYC","Work");
p.addEntryToPhoneBook(new Entry("AbdJhon","12345","CA"));
p.addEntryToPhoneBook(new Entry("Abd","46722","Huston"));
p.addEntryToPhoneBook(new Entry("JJ","23904","DC"));
p.addEntryToPhoneBook(new Entry("Aron","896745","Pasedina"));
p.addEntryToPhoneBook(new Entry("Reid","34566","Hills"));
p.displayAll();
Entry e=p.checkEntryByNumber("12345");
System.out.println("Entry with given Number");
if(e==null){
System.out.println("Entry with this number does not exists");
}
else{
System.out.println(e);
}
System.out.println("Entry with given String");
Object ar[]=p.checkEntrisByName("Abd");
if(ar!=null){
for(Object en:ar){
System.out.println(en);
}
}
else{
System.out.println("Entry with this name does not exists");
}
boolean isRemoved=p.removeEntryFromPhoneBook("46722");
if(isRemoved){
System.out.println("Entry with this number removed");
}
else{
System.out.println("Entry with this number does not exists");
}
p.displayAll();
}
}
output
Entry [name=AbdJhon, phoneNumber=12345, address=CA]
Entry [name=Abd, phoneNumber=46722, address=Huston]
Entry [name=JJ, phoneNumber=23904, address=DC]
Entry [name=Aron, phoneNumber=896745, address=Pasedina]
Entry [name=Reid, phoneNumber=34566, address=Hills]
Entry with given Number
Entry [name=AbdJhon, phoneNumber=12345, address=CA]
Entry with given String
Entry [name=AbdJhon, phoneNumber=12345, address=CA]
Entry [name=Abd, phoneNumber=46722, address=Huston]
Entry with this number removed
Entry [name=AbdJhon, phoneNumber=12345, address=CA]
Entry [name=JJ, phoneNumber=23904, address=DC]
Entry [name=Aron, phoneNumber=896745, address=Pasedina]
Entry [name=Reid, phoneNumber=34566, address=Hills]