zoukankan      html  css  js  c++  java
  • (自定义异常)完成某个计费系统的用户登录和注册模块

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

    /**
     * @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 ;
            }
        }
    }
  • 相关阅读:
    Ajax学习笔记3种Ajax的实现
    分页学习笔记真分页和假分页实现
    学习笔记链表练习,模仿StringBuilder的山寨版StringBuilder
    学习笔记将Asp.Net网站发布到IIS的四种方法及注意事项
    3D 音频技术产品介绍(1):Iosono the future of spatial audio
    国际顶级语音信号增强工作组:IWAENC(International Workshop on Acoustic Echo and Noise Control)
    转:《欢聚时代(多玩YY)IPO招股书》(概要)
    苏州阔地网络科技有限公司专利分析
    CELT和SILK以及Opus的位分配方法
    ISAC 码流格式
  • 原文地址:https://www.cnblogs.com/shanghuliu/p/13593793.html
Copyright © 2011-2022 走看看