Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Contact implements Comparable<Contact> {
- private String lastName;
- private String firstName;
- private String email;
- public Contact(String lastName, String firstName, String email) {
- this.lastName = lastName;
- this.firstName = firstName;
- this.email = email;
- }
- public String getLastName() {
- return lastName;
- }
- public String getFirstName() {
- return firstName;
- }
- public String getEmail() {
- return email;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- // Forces the natural ordering to be based on last names.
- // If last names are the same, ignoring cases, then sort based on first name.
- @Override
- public int compareTo(Contact that) {
- if (this.getLastName().compareToIgnoreCase(that.getLastName()) == 0) {
- return this.getFirstName().compareToIgnoreCase(that.getFirstName());
- } else {
- return this.getLastName().compareToIgnoreCase(that.getLastName());
- }
- }
- @Override
- public String toString() {
- return "First Name: " + firstName + "\n" +
- "Last Name: " + this.lastName + "\n" +
- "Email: " + this.getEmail() + "\n"; // or getEmail()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment