In: Computer Science
java NetBeans
Class Entry:
Class PhoneBook
Class TestPhoneBook
Hello,
Please find the below code :
public class Entry implements Comparable<Entry> {
   private String name;
   private String phoneNumber;
   private String address;
   public Entry(String name, String phoneNumber,
String address) {
       super();
       this.name = name;
       this.phoneNumber =
phoneNumber;
       this.address = address;
   }
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((address
== null) ? 0 : address.hashCode());
       result = prime * result + ((name ==
null) ? 0 : name.hashCode());
       result = prime * result +
((phoneNumber == null) ? 0 : phoneNumber.hashCode());
       return result;
   }
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return
true;
       if (obj == null)
           return
false;
       if (getClass() !=
obj.getClass())
           return
false;
       Entry other = (Entry) obj;
       if (address == null) {
           if
(other.address != null)
          
    return false;
       } else if
(!address.equals(other.address))
           return
false;
       if (name == null) {
           if (other.name
!= null)
          
    return false;
       } else if
(!name.equals(other.name))
           return
false;
       if (phoneNumber == null) {
           if
(other.phoneNumber != null)
          
    return false;
       } else if
(!phoneNumber.equals(other.phoneNumber))
           return
false;
       return true;
   }
   @Override
   public String toString() {
       return "Entry [name=" + name + ",
phoneNumber=" + phoneNumber + ", 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 int compareTo(Entry o) {
       return
(Integer.valueOf(this.phoneNumber) <
Integer.valueOf(o.phoneNumber)) ? -1
          
    : (Integer.valueOf(this.phoneNumber) >
Integer.valueOf(o.phoneNumber)) ? 1 : 0;
}
}
import java.util.ArrayList;
import java.util.List;
public class Phonebook {
   private String city;
   private String type;
   public List<Entry> entryList;
   public Phonebook(String city, String type) {
       super();
       this.city = city;
       this.type = type;
       entryList = new
ArrayList<Entry>();
   }
   public void addEntryToPhoneBook(Entry entry)
{
       entryList.add(entry);
   }
   @Override
   public String toString() {
       return "Phonebook [entryList=" +
entryList + "]";
   }
   public List<Entry> displayAll() {
       return entryList;
   }
   /*
   * checkEntryByNumber
   */
   public Entry checkEntryByNumber(String number) {
       for (int index = 0; index <
entryList.size(); index++) {
           if
(entryList.get(index).getPhoneNumber() == number) {
          
    return entryList.get(index);
           }
       }
       System.out.println("Phone number
does not exists");
       // Returning null if Phone number
doesn't exists
       return null;
   }
   /*
   * checkEntrisByName
   */
   public List<Entry> checkEntrisByName(String
name) {
       List<Entry> list = new
ArrayList<Entry>();
       for (int index = 0; index <
entryList.size(); index++) {
           if
(entryList.get(index).getName().startsWith(name)) {
          
    list.add(entryList.get(index));
           }
       }
       return list;
   }
   /*
   * removeEntryFromPhoneBook
   */
   public void removeEntryFromPhoneBook(String number)
{
       for (int index = 0; index <
entryList.size(); index++) {
           if
(entryList.get(index).getPhoneNumber() == number) {
          
    System.out.println("Removed the entry : " +
entryList.get(index).getName());
          
    entryList.remove(index);
           }
       }
   }
}
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestPhonebook {
   public static void main(String[] args) {
       // Create a phone book and
initialize its attributes.
Phonebook book = new Phonebook("Kolkata", "Unknown");
// Add 5 entries to the phone book list using the method addEntryToPhoneBook()
       Entry e1 = new Entry("Abdite",
"1234567891", "C/O abc");
       Entry e2 = new Entry("Jibran",
"6783644", "C/O weew");
       Entry e3 = new Entry("Abdlice",
"876574", "C/O wewe");
       Entry e4 = new Entry("Smith",
"9275454", "C/O wewewe");
       Entry e5 = new Entry("John",
"846333", "C/O weweew");
      
book.addEntryToPhoneBook(e1);
       book.addEntryToPhoneBook(e2);
       book.addEntryToPhoneBook(e3);
       book.addEntryToPhoneBook(e4);
       book.addEntryToPhoneBook(e5);
// Display all entries in the phone book.
       List<Entry> list =
book.displayAll();
       for (int index = 0; index <
list.size(); index++) {
          
System.out.println(list.get(index).toString());
       }
System.out.println("===================================");
      
System.out.println(book.checkEntryByNumber("1234567891"));
      
System.out.println(book.checkEntryByNumber("123"));
System.out.println("===================================");
       // Display all entries starting
with the letters "Abd"
       List<Entry> list1 =
book.checkEntrisByName("Abd");
       System.out.println(list1);
System.out.println("===================================");
       // Remove a specific entry from
the phone book. Display the proper message if it
       // was removed , or if it did not
exist.
      
book.removeEntryFromPhoneBook("1234567891");
System.out.println("===================================");
       // Sort and display the entries
of the phone book
      
Collections.sort(book.displayAll());
      
System.out.println(book.displayAll());
   }
}
Test Result:

Code ScreenShots:



Please
let me know if you any questions regarding this.
Thanks