Duplicate mobile number exception

 ContactDetail.JAVA:

public class ContactDetail {
    private String mobileNumber;
	private	String alternateMobileNumber;
	private	String landline;
	private	String email;
	private String address;
	
	public ContactDetail(String mobileNumber, String alternateMobileNumber, String landline, String email, String address) {
		this.mobileNumber = mobileNumber;
		this.alternateMobileNumber = alternateMobileNumber;
		this.landline = landline;
		this.email = email;
		this.address = address;
	}
	public ContactDetail() {
	}
	public String getMobileNumber() {
		return mobileNumber;
	}
	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}
	public String getAlternateMobileNumber() {
		return alternateMobileNumber;
	}
	public void setAlternateMobileNumber(String alternateMobileNumber) {
		this.alternateMobileNumber = alternateMobileNumber;
	}
	public String getLandline() {
		return landline;
	}
	public void setLandline(String landline) {
		this.landline = landline;
	}
	public String getEmail() {
		return email;
	}
	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:"+getMobileNumber()+"\n"+
    	                "Alternate mobile:"+getAlternateMobileNumber()+"\n" +
    	                "LandLine:"+getLandline()+"\n" +
    	                "Email:"+getEmail()+"\n" +
    	                "Address:"+getAddress();
    	    }

    	
    }



MAIN.JAVA:

import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the contact details");
        String st = br.readLine();
        String[] str = st.split(",");

        ContactDetail c = new ContactDetail(str[0],str[1],str[2],str[3],str[4]);
        c.setMobileNumber(str[0]);
        c.setAlternateMobileNumber(str[1]);
        c.setLandline(str[2]);
        c.setEmail(str[3]);
        c.setAddress(str[4]);
        try {
            ContactDetailBO.validate(str[0], str[1]);
            System.out.println(c.toString());
        } catch (DuplicateMobileNumberException e) {
            System.out.println(e);
        }

    }
}


ContactDetailBO.JAVA:

public class ContactDetailBO
{
 static void validate(String mobileNumber,String alternateMobileNumber) throws DuplicateMobileNumberException {
     if(Long.parseLong(mobileNumber)==Long.parseLong(alternateMobileNumber)){
         throw new DuplicateMobileNumberException();
     }
 }
}



Duplicatemobilenumber.JAVA:

public class DuplicateMobileNumberException extends Throwable{
     public String getMessage(){
       return "Mobile number and alternate mobile number are same";

    }
}

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.