利用自定义的异常验证邮箱合法性(不使用正则)

不用正则表达式,验证邮箱合法性
a、本地验证---验证的是邮箱与密码的格式 --邮箱: 
        1、要有@ . 2、@ . 前后不能为空 3、@要在 . 的前面 
        4、@前面的长度至少是10,包含数字,字母,且必须有大写字母   
 b:网络验证---验证的是邮箱与密码与服务器存储的是否相同
public class UserEmail {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 用户
        System.out.println("请输入邮箱号");
        String user = sc.nextLine();
        Email mail = new Email(user);

        try {

            mail.emilHeFa();
            System.out.println("邮箱正确");

            // 密码
            System.out.println("请输入密码");
            String pw = sc.next();// 密码不能有空格
            PassWord passWord = new PassWord(pw);

            try {

                passWord.passWord();
                System.out.println("密码正确");

            } catch (SeverPassWordException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (NUmException e) {
            e.printStackTrace();
        } catch (WeikongException e) {
            e.printStackTrace();
        } catch (OrderException e) {
            e.printStackTrace();
        } catch (LengthException e) {
            e.printStackTrace();
        } catch (SeverUserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

class NUmException extends Exception { // 第一种异常:没有 @ 。

    public NUmException() {
        super();
    }

    public NUmException(String message) {
        super(message);
    }

}

class WeikongException extends Exception { // 第二种 @ 。 前后不能为空

    public WeikongException() {
        super();
    }

    public WeikongException(String message) {
        super(message);
    }

}

class OrderException extends Exception { // 第三种 @在。的前面

    public OrderException() {
        super();
    }

    public OrderException(String message) {
        super(message);
    }

}

class LengthException extends Exception { // 第四种 @前面的长度至少是10,包含数字,字母,且必须有大写字母

    public LengthException() {
        super();
    }

    public LengthException(String message) {
        super(message);
    }

}

class SeverUserException extends Exception { // 服务器用户校验

    public SeverUserException() {
        super();
    }

    public SeverUserException(String message) {
        super(message);
    }

}

class SeverPassWordException extends Exception { // 服务器密码校验

    public SeverPassWordException() {
        super();
    }

    public SeverPassWordException(String message) {
        super(message);
    }

}

class Email {
    private String mail;

    public Email(String mail) {
        this.mail = mail;
    }

    public String getMail() { // get set 方法
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public void emilHeFa() throws NUmException, WeikongException, OrderException, LengthException, SeverUserException {

        // 第一种异常:没有 @ 。
        if (!(mail.contains("@") && mail.contains("."))) {
            throw new NUmException("没有 @   .");
        }

        // 第二种 @ 。 前后不能为空
        if (mail.contains(" ") ||  mail.endsWith(".")  || !shunXu(mail)) {
            throw new WeikongException("@和.前后不能为空");
        }

        // 第三种 @在。的前面
        if (mail.indexOf("@") > mail.indexOf(".")) {
            throw new OrderException("格式不正确");
        }

        // 第四种 @前面的长度至少是10,必须有大写字母 包含数字,字母
        if (mail.indexOf("@") < 10 && !(bigLetter(mail))
                || ((numLetter(mail) && mail.indexOf("@") < 10 && !(bigLetter(mail))))) { // 保证全是大写也可以
            throw new LengthException("@前面的长度至少是10,包含数字,字母且必须有大写字母");
        }

        // 假装和服务器用户匹配
        UAP c = new UAP();
        String us = c.user; 
        if (!(mail.equals(us))) {
            throw new SeverUserException("用户名错误");
        }
        System.out.println("用户正确");

    }
    //判断@ 和 。 之间为空?
    public boolean shunXu(String user) {

            if ((mail.indexOf(".") - mail.indexOf("@") >1)) {
                return true;
            }

        return false;

    }

    // 包含数字,字母
    public boolean numLetter(String user) {

        for (int i = 0; i < user.length(); i++) {

            char a = user.charAt(i); // 返回指定索引的值
            if (a >= 97 && a <= 122 || a >= 0 && a <= 9) { // 有小写字母或数字 有返回true 没有则false

                return true;

            }
        }

        return false;

    }

    // 必须有大写字母
    public boolean bigLetter(String user) {

        for (int i = 0; i < user.length(); i++) { // 遍历
            char a = user.charAt(i);

            if (a >= 65 && a <= 90) { // 有大写字母返回true 没有则false

                return true;
            }
        }
        return false;
    }
}

class PassWord { // 与服务器密码匹配
    private String pw;

    public PassWord(String pw) {
        this.pw = pw;
    }

    public String getPw() {
        return pw;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }

    public void passWord() throws SeverPassWordException { // 假装去服务器验证密码
        UAP c = new UAP();
        String mi = c.pw;
        if (!(pw.equals(mi))) {
            throw new SeverPassWordException("用户密码不正确");
        }
        System.out.println("密码通过");
    }

}

class UAP { // 假装一个服务器
    public static String user = "Q123456789@163.com";
    public static String pw = "1234567";

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值