Java Overriding Equals Method and Compare Objects for Equality

Java Overriding Equals Method and Compare Objects for Equality:

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 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) { = name; = id;
        this.balance = balance;

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

        return false;

CustomerManagementApp Class With Input/Output Format:



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(;

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


            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 =;
                    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);

                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");
                        try {
                                System.out.println("\nDuplicate customer\n");
                                System.out.println("\n" + cms[search_id_1].getName() + ", " + cms[search_id_2].getName());
                        }catch (Exception e){
                            System.out.println("Customer doesn't exist");

                case 0:


    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. ");

Leave a Reply

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

You are commenting using your 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