利用Java实现的一个简易的账号注册类

分为两个类,一个类是用户即人,第二个类实现了相关接口如账号密码邮箱的简短验证,代码如下:


import javax.xml.stream.events.Characters;
import java.util.Arrays;
import java.util.Scanner;

public class test02 {
    public static void main(String[] args) {

        Person person = new Person();
        Load.UseName(person);
        Load.PassWord(person);
        Load.MailBox(person);

        System.out.println(person.toString());

    }
}

class Person {
    private String username;
    private String password;
    private String mailbox;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMailbox() {
        return mailbox;
    }

    public void setMailbox(String mailbox) {
        this.mailbox = mailbox;
    }

    @Override
    public String toString() {
        return "Person{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", mailbox='" + mailbox + '\'' +
                '}';
    }
}

class Load{

    public static boolean UseName(Person person){
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你的用户名:");
            person.setUsername(scanner.next());
            int i = person.getUsername().length();
            if (!(i <= 4 && i >= 2)) {
                throw new RuntimeException("你的账户名输入格式有误! 用户名长度应该为2 或 3 或 4");
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            UseName(person);
        }

        return true;
    }

    public static boolean PassWord(Person person) {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你的密码: ");
            person.setPassword(scanner.next());
            char[] chars = person.getPassword().toCharArray();


            for (int j = 0; j < chars.length; j++) {
                if (!(Character.isDigit(chars[j]) && chars.length == 6)) {
                    throw new RuntimeException("你的密码格式有误!密码应为全数字且长度为6 ");
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            PassWord(person);
        }
        return true;
    }

    public static boolean MailBox(Person person) {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你的邮箱: ");
            person.setMailbox(scanner.next());
            char[] chars = person.getMailbox().toCharArray();
            int count = 0;
            int count01 = 0;

            for (int j = 0; j < chars.length; j++) {
                if(Character.isWhitespace(chars[j])){
                    throw new RuntimeException("邮箱不能存在空格 !");
                }

                if(chars[j] == '@'){
                    count01++;
                    if(count01 > 1){
                        throw new RuntimeException("邮箱只允许存在一个 ’@‘ ");
                    }
                }

                if(chars[j] == '.' ){
                    count++;
                    if(count > 1){
                        throw new RuntimeException("邮箱只允许存在一个 ’.‘ ");
                    }
                    if(chars[j-1] != '@'){
                        throw new RuntimeException("请正确输入邮箱格式,检查@是否存在于’.‘之前");
                    }
                }
            }

            if(count01 != 1 && count !=1){
                throw new RuntimeException("请输入正确的邮箱格式!!");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            MailBox(person);
        }
        return true;
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值