Java Overriding Equals Method and Compare Objects for Equality

Java Overriding Equals Method and Compare Objects for Equality:
Use: 

Where we have objects instead of primitive values to compare.
We can compare a all or certain property/attribute for equality
based on our custom equals method logic.
Other things that can be also learned from this Code:

1. Array of objects (Homogeneous Collection).
2. Object creation (with passing arguments to constructor).
3. Exception Handling.
4. Creating and calling a method in Main Class.
5. Use of instanceof operator.
6. Type casting objects to certain type.

Function Call:

Object1.equals(Object2);

Here,

Object1 is referenced by the this operator.

Object2 is the argument passed to the equals method
to compare specific properties against the caller object.

CustomerManagementSystem Class:

customer_managent_system with custom equals method
customer_managent_system with custom equals method

 

CustomerManagementSystem Class Code:

/**
 * Created by asif on 8/13/2015.
 */
public class CustomerManagementSystem {

    private String name;
    private int id;
    private double balance;

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }

    public CustomerManagementSystem(String name, int id, double balance) {
        this.name = name;
        this.id = id;
        this.balance = balance;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof CustomerManagementSystem){
            if(this.name.equals( ((CustomerManagementSystem) obj).name) && this.id == ((CustomerManagementSystem) obj).id &&  this.balance == ((CustomerManagementSystem) obj).balance ){
                return true;
            }
        }

        return false;
    }
}

CustomerManagementApp Class With Input/Output Format:


customer_managent_main_1
customer_managent_main_1
customer_managent_main_2
customer_managent_main_2

 

CustomerManagementApp Class Code:

import java.util.Scanner;

/**
 * Created by asif on 8/13/2015.
 */
public class CustomerManagementApp {
    public static void main(String [] args){

        Scanner k = new Scanner(System.in);

        CustomerManagementSystem [] cms = new CustomerManagementSystem[10];
        int i = 0;

        while(true){

            showMenu();
            int choice = k.nextInt();

            if(i > 10){
                System.out.println("System can't handle any more users");
            }

            switch (choice){
                case 1:
                    System.out.print("Enter name: ");
                    String name = k.next();
                    System.out.print("Enter id: ");
                    int id = k.nextInt();
                    System.out.print("Enter balance: ");
                    double balance = k.nextDouble();
                    cms[i] = new CustomerManagementSystem(name, id, balance);
                    ++i;
                    break;

                case 2:
                    System.out.println("\nCheck Duplicate: ");
                    System.out.print("Enter first customer number: ");
                    int search_id_1 = k.nextInt();

                    System.out.print("Enter second customer number: ");
                    int search_id_2 = k.nextInt();


                    if((search_id_1 < 1 || search_id_1 > 10) && (search_id_2 < 1 || search_id_2 > 10))
                        System.out.println("Customer doesn't exist");
                    else{
                        --search_id_1;
                        --search_id_2;
                        try {
                            if(cms[search_id_1].equals(cms[search_id_2]))
                                System.out.println("\nDuplicate customer\n");
                            else
                                System.out.println("\n" + cms[search_id_1].getName() + ", " + cms[search_id_2].getName());
                        }catch (Exception e){
                            System.out.println("Customer doesn't exist");
                        }
                    }
                    break;

                case 0:
                    return;
            }
        }

    }

    public static void showMenu(){
        System.out.println("'1' Create your customer.");
        System.out.println("'2' Check For Duplicate Customers.");
        System.out.println("'0' to exit the program. ");
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s