In this post, we will learn Check for Existence of Customer – equals method Program in java Programming language.
Question:
Uninor, one of the mobile network operators offers free network for the customers. Before making use of this offer the customer should get enrolled with this network. One customer can get enrolled only once.
Now, Uninor needs an application to check whether the customer who enrolls already exists or not. Develop a program to implement the concept by taking the input for two customers and compare if they are same.
Create a public class Customer with the private attributes:
String name
String panno
String emailid
int salary
Include appropriate public getters and setters.
Write a 4 argument constructor with arguments – name, panno, emailid and salary.
Note :
- If the emailid and panno are same, then customer is an existing customer.
- Override the equals method to check for the existence of customer.
Create a class Main with the main method. In the main method get the input for 2 customers as shown in the sample input. Compare the 2 objects using the overridden equals method and print the output as “Both the objects are equal.”, if the objects are equal. Else, display “Both the objects are not equal.”
Refer the samples given, to read and display the data.
Sample Input 1:
Enter the name:
John
Enter the panno:
abcde1234R
Enter the emailid:
john@gmail.com
Enter the salary:
20000
Enter the name:
Tom
Enter the panno:
abcde1234R
Enter the emailid:
john@gmail.com
Enter the salary:
30000
Sample Output 1:
Both the objects are equal.
Sample Input 2:
Enter the name:
John
Enter the panno:
abcde1234R
Enter the emailid:
john@gmail.com
Enter the salary:
20000
Enter the name:
Tom
Enter the panno:
abcde1234R
Enter the emailid:
tom@gmail.com
Enter the salary:
30000
Sample Output 2:
Both the objects are not equal.
CODE:–
Main.java
import java.util.Scanner; public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the name:"); String a=sc.nextLine(); System.out.println("Enter the panno:"); sc.next(); String b=sc.nextLine(); System.out.println("Enter the emailid"); sc.next(); String e=sc.nextLine(); System.out.println("Enter the salary:"); int c=sc.nextInt(); System.out.println("Enter the name:"); sc.nextLine(); String d=sc.nextLine(); System.out.println("Enter the panno:"); sc.next(); String f=sc.nextLine(); System.out.println("Enter the emailid:"); sc.next(); String g=sc.nextLine(); System.out.println("Enter the salary:"); int h=sc.nextInt(); Customer a1=new Customer(a,b,e,c); Customer a2=new Customer(d,f,g,h); boolean a3=true; a3=a1.equals(a2); if(a3==true) System.out.println("Both the objects are equal."); else System.out.println("Both the objects are not equal."); } }
public class Customer { private String name; private String panno; private String emailid; private int salary; public void setName(String a) { name=a; } public void setPanno(String b) { panno=b; } public void setEmailid(String c) { emailid=c; } public void setSalary(int d) { salary=d; } public String getName() { return name; } public String getPanno() { return panno; } public String getEmailid() { return emailid; } public int getSalary() { return salary; } public Customer() { name=""; panno=""; emailid=""; salary=0; } public Customer(String a,String b,String c,int d) { this.name=a; this.panno=b; this.emailid=c; this.salary=d; } public boolean equals(Object o) { Customer e=(Customer) o; if(e.name.equals(this.name)&& e.panno.equals(this.panno) && e.emailid.equals(this.emailid) && e.salary==this.salary) return true; else return false; } }