binibiningtinamoran

Contact

Nov 16th, 2019
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. public class Contact implements Comparable<Contact> {
  2.  
  3.     private String lastName;
  4.     private String firstName;
  5.     private String email;
  6.  
  7.  
  8.     public Contact(String lastName, String firstName, String email) {
  9.  
  10.         this.lastName = lastName;
  11.         this.firstName = firstName;
  12.         this.email = email;
  13.     }
  14.  
  15.  
  16.     public String getLastName() {
  17.         return lastName;
  18.     }
  19.  
  20.     public String getFirstName() {
  21.         return firstName;
  22.     }
  23.  
  24.     public String getEmail() {
  25.         return email;
  26.     }
  27.  
  28.     public void setLastName(String lastName) {
  29.         this.lastName = lastName;
  30.     }
  31.  
  32.  
  33.     public void setFirstName(String firstName) {
  34.         this.firstName = firstName;
  35.     }
  36.  
  37.     public void setEmail(String email) {
  38.         this.email = email;
  39.     }
  40.  
  41.     // Forces the natural ordering to be based on last names.
  42.     // If last names are the same, ignoring cases, then sort based on first name.
  43.     @Override
  44.     public int compareTo(Contact that) {
  45.         if (this.getLastName().compareToIgnoreCase(that.getLastName()) == 0) {
  46.             return this.getFirstName().compareToIgnoreCase(that.getFirstName());
  47.         } else {
  48.             return this.getLastName().compareToIgnoreCase(that.getLastName());
  49.         }
  50.     }
  51.  
  52.     @Override
  53.     public String toString() {
  54.         return "First Name: " + firstName + "\n" +
  55.                 "Last Name: " + this.lastName + "\n" +
  56.                 "Email: " + this.getEmail() + "\n"; // or getEmail()
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment