[Solved] Duplicate mobile number exception with Java

Duplicate mobile number exception: Write a java program to find the duplicate mobile number using the exception handling mechanism.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.

Create a Class called ContactDetail with the following private attributes.

AttributesDatatype
mobileString
alternateMobileString
landLineString
emailString
addressString

Include getters and setters.
Include default and parameterized constructors.
Format for a parameterized constructor is ContactDetail(String mobile, String alternateMobile,String landLine, String email, String address)

Override the toString() method to display the Contact details as specified.

Create a class called ContactDetailBO with following methods

Method Description
static void validate(String mobile,String alternateMobile)This method throws DuplicateMobileNumber exception
if the mobile and alternateMobile are the same.

Create a driver class called Main. In the Main method, obtain inputs from the user. Validate the mobile and alternateMobile and display the ContactDetail if no exception occurs else handle the exception.

Pass the exception message as “Mobile number and alternate mobile number are same“. If mobile and alternateMobile are the same.

Input and Output format:
Refer to sample Input and Output for formatting specifications.

Note: All text in bold corresponds to the input and rest corresponds to the output.

Sample Input and Output 1:

Enter the contact details
9874563210,9874563210,0447896541,johndoe@abc.in,22nd street kk nagar chennai
DuplicateMobileNumberException: Mobile number and alternate mobile number are same

Sample Input and Output 2:

Enter the contact details
9874563210,9876543211,0447896541,johndoe@abc.in,22nd lane RR nagar kovai
Mobile:9874563210
Alternate mobile:9876543211
LandLine:0447896541
Email:johndoe@abc.in
Address:22nd lane RR nagar kovai

Solution


import java.util.Scanner;
import java.io.*;
//sauravhathi
class ContactDetailBO {
public static void validate(String mobile, String alternateMobile) throws DuplicateMobileNumberException {
    if (mobile.equals(alternateMobile)) {
        throw new DuplicateMobileNumberException();
    }
}
}
//sauravhathi
class ContactDetail {
private String mobile;
private String alternateMobile;
private String landLine;
private String email;
private String address;

public ContactDetail() {
    this.mobile = "";
    this.alternateMobile = "";
    this.landLine = "";
    this.email = "";
    this.address = "";
}
//sauravhathi
public ContactDetail(String mobile, String alternateMobile, String landLine, String email, String address) {
    this.mobile = mobile;
    this.alternateMobile = alternateMobile;
    this.landLine = landLine;
    this.email = email;
    this.address = address;
}

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
        this.mobile=mobile;
}
//sauravhathi
    public String getAlternateMobile() {
        return alternateMobile;
    }

    public void setAlternateMobile(String alternateMobile) {
        this.alternateMobile=alternateMobile;
    }

    public String getLandLine() {
        return landLine;
    }

    public void setLandLine(String landLine) {
        this.landLine=landLine;
    }

    public String getEmail() {
        return email;
    }
    //sauravhathi

    public void setEmail(String email) {
        this.email=email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address=address;
    }

    public String toString() {
        return "Mobile: " + mobile + "\nAlternate mobile: " + alternateMobile + "\nLandLine: " + landLine + "\nEmail: " + email + "\nAddress: " + address;
    }
}
//sauravhathi

class DuplicateMobileNumberException extends Exception {
    public DuplicateMobileNumberException() {
        //sauravhathi
        super("Mobile number and alternate mobile number are same");
    }
}

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the contact details");
        String s = sc.nextLine();
        String[] arr = s.split(",");
        ContactDetail cd = new ContactDetail(arr[0], arr[1], arr[2], arr[3], arr[4]);
        try {
            //sauravhathi
            ContactDetailBO.validate(arr[0], arr[1]);
            System.out.println(cd);
        } catch (DuplicateMobileNumberException e) {
            System.out.println("DuplicateMobileNumberException: " + e.getMessage());
        }
        //sauravhathi
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *