Create a class named as Customerunder package :package2, which contains following private variables/ attributes,
Member Field name
Type
id
Long
name
String
gender
Character (M/F)
String
contact Number
String
Mark all the attributes as private
Add a default constructor and a parameterized constructor to take in all attributes.
Create a separate class Main under package :package2, within it's main() method, make 2 Customer objects and then compare between these Customers.
Two members are considered same if they have same email and contactNumber. Implement the logic in the appropriate function. (override equals method in class Customer)
Sample Input:
Customer1:
ld: 45
Name: John
Gender: M
Email: john@a.com contact number: +997-4854-7485965123
Customer2:
ld: 12
Name: Marc
Gender: M
Email: marc@a.com
Contact number: +997-4854-7485965123
Sample Output:
Customer 1 and Customer 2 are different
import java.util.*;
class Customer {
private long id;
private String name;
private Character gender;
private String email;
private String contactNumber;
public Customer() {
}
public Customer(long id, String name, Character gender, String email, String contactNumber) {
this.id = id;
this.name = name;
this.gender = gender;
this.email = email;
this.contactNumber = contactNumber;
}
public String toString() {
return "ID: " + id + "\n" + "Name: " + name + "\n" + "Gender: " + gender + "\n" + "Email: " + email + "\n"
+ "Contact number: " + contactNumber;
}
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the gender
*/
public Character getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(Character gender) {
this.gender = gender;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the contactNumber
*/
public String getContactNumber() {
return contactNumber;
}
/**
* @param contactNumber the contactNumber to set
*/
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
@Override
public boolean equals(Object obj) {
Customer c = (Customer) obj;
return (c.contactNumber.compareTo(this.contactNumber) == 0 && c.email.compareTo(this.email) == 0);
}
}
class App {
static Scanner keyboard = new Scanner(System.in);
static Customer createCustomer(int i) {
System.out.println("Customer" + i + ":");
System.out.print("ID: ");
long id = keyboard.nextLong();
keyboard.nextLine();
System.out.print("Name: ");
String name = keyboard.nextLine();
System.out.print("Gender: ");
Character gender = keyboard.next().charAt(0);
keyboard.nextLine();
System.out.print("Email: ");
String email = keyboard.nextLine();
System.out.print("Contact number: ");
String contactNumber = keyboard.nextLine();
return new Customer(id, name, gender, email, contactNumber);
}
public static void main(String[] args) {
Customer c1 = createCustomer(1);
Customer c2 = createCustomer(2);
if (c1.equals(c2)) {
System.out.println("Customer 1 and Customer 2 are same");
} else {
System.out.println("Customer 1 and Customer 2 are different");
}
keyboard.close();
}
}
Comments
Leave a comment