• (自定义异常)完成某个计费系统的用户登录和注册模块


    自定义异常小练习,登录和注册

    /**
     * @Author Liu Lei
     * @Date 2020/8/31 16:01
     * @Version 1.0
     * @description
     * 登陆异常
     */
    public class IllegalLoginException extends Exception {
        public IllegalLoginException() {
        }
    
        public IllegalLoginException(String message) {
            super(message);
        }
    }
    /**
     * @Author Liu Lei
     * @Date 2020/8/31 16:02
     * @Version 1.0
     * @description
     * 注册异常
     */
    public class IllegalRegisterException extends Exception {
    
        public IllegalRegisterException() {
        }
    
        public IllegalRegisterException(String message) {
            super(message);
        }
    }
    import java.util.Scanner;
    
    /**
     * @Author Liu Lei
     * @Date 2020/8/31 17:13
     * @Version 1.0
     * @description
     */
    public class TestUser {
    
        public static void main(String[] args){
            UserViewImpl uv = new UserViewImpl();
            do{
                Scanner sc = new Scanner(System.in);
                System.out.println("请选择执行的操作:
    1、登陆
    2、注册
    3 退出");
                int i = sc.nextInt();
                if(i==1){
                    uv.login();
                }
                else if(i==2){
                    uv.register();
                }
                else {
                    break;
                }
            }while(true);
        }
    }
    /**
     * @Author Liu Lei
     * @Date 2020/8/31 15:58
     * @Version 1.0
     * @description
     *创建一个User 类,包括:用户登录名(username)、密码(password)、用户真实姓
     * 名(name)、电子邮件地址(email)属性和相应的构造方法及setter/getter 方法。
     */
    public class User {
        private String username ;
        private  String password ;
        private String name ;
        private  String email ;
    
        public User() {
        }
    
    
        public User(String username, String password, String name, String email) {
            this.username = username;
            this.password = password;
            this.name = name;
            this.email = email;
        }
    
    
        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 getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    }
    /**
     * @Author Liu Lei
     * @Date 2020/8/31 16:03
     * @Version 1.0
     * @description
     */
    public interface UserBiz {
    
        void register(String username, String password, String rePassword,
                      String name, String email) throws IllegalRegisterException ;//用户注册
        void login(String username, String password) throws IllegalLoginException ;//用户登录
    
    }
    import java.util.Scanner;
    
    /**
     * @Author Liu Lei
     * @Date 2020/8/31 16:05
     * @Version 1.0
     * @description
     */
    public class UserBizImpl implements UserBiz {
    
        static int index=0;
        static Scanner sc = new Scanner(System.in);
        static User[] users=new User[10];
        static{
            users[0]=new User("admin", "admin", "Adminstrator", "admin@123.com");
            users[1]=new User("Tom", "cat", "tomcat", "tomcat@cat.com");
        }
    
        @Override
        public void register(String username, String password, String rePassword, String name, String email) throws IllegalRegisterException {
            if(!password.equals(rePassword)){
                throw new IllegalRegisterException("两次密码不一致");
            }
            for(int i = 0;i<users.length;i++){
                if(users[i] ==null){
    
                    break;
                }
                if(users[i].getUsername().equals(username)){
    
                    throw new IllegalRegisterException("用户名已经存在");
                }
            }
            users[index]=new User(username, rePassword, name, email);
            index++;
            System.out.println("注册成功");
        }
    
        @Override
        public void login(String username, String password) throws IllegalLoginException {
            boolean flag = false;
            for(int i=0;i<users.length;i++) {
                if (users[i] == null) {
                    break;
                }
                if (users[i].getUsername().equals(username)) {
                    if (!users[i].getPassword().equals(password)) {
                        throw new IllegalLoginException("用户名与密码不匹配");
                    }
                    if (users[i].getUsername().equals(username)) {
                        flag = true;
                    }
                }
            }
            if(!flag) {
                throw new IllegalLoginException("用户名不存在");
            }
            System.out.println("登陆成功");
        }
    
    
    
    
    
        }
    /**
     * @Author Liu Lei
     * @Date 2020/8/31 16:23
     * @Version 1.0
     * @description
     */
    public interface UserView {
    
        void login() throws IllegalLoginException;
        void register();
    
    }
    import java.util.Scanner;
    
    /**
     * @Author Liu Lei
     * @Date 2020/8/31 16:24
     * @Version 1.0
     * @description
     */
    public class UserViewImpl implements UserView {
        static UserBiz ub =new UserBizImpl();
        static Scanner sc= new Scanner(System.in);
    
        @Override
        public void login()  {
    
            System.out.println("请输入用户名:");
            String username = sc.next();
            System.out.println("请输入密码:");
            String password = sc.next();
            try{
    
                ub.login(username,password);
    
            }catch (IllegalLoginException e){
                e.printStackTrace();
            }
    
    
    
        }
    
        @Override
        public void register() {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username=sc.next();
            System.out.println("请输入密码:");
            String password=sc.next();
            System.out.println("请再次输入密码:");
            String password2 = sc.next();
            System.out.println("请输入姓名:");
            String name = sc.next();
            System.out.println("请输入邮箱地址");
            String email = sc.next();
            try {
                ub.register(username, password, password2, name, email);
            } catch (IllegalRegisterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return ;
            }
        }
    }
  • 相关阅读:
    DataGridViewComboBoxColumn值无效
    你 我 我们 技术 让世界变得更好一些
    .NET Core中使用IHostedService结合队列执行定时任务
    参加胶东开发者技术大会有感
    在Windows下使用Nodist进行Node版本控制
    React中props.children和React.Children的区别
    React同构直出原理浅析
    React学习资料
    git在分支上创建目录和文件
    从一个例子中体会React的基本面
  • 原文地址:https://www.cnblogs.com/shanghuliu/p/13593793.html
Copyright © 2020-2023  润新知