欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java实现注册登录系统

时间:2022-09-01 09:26:10|栏目:JAVA代码|点击:

本文实例为大家分享了java实现注册登录系统的具体代码,供大家参考,具体内容如下

1、创建菜单,注册,登录,退出

2、注册模块:

a) 通过键盘输入用户名,密码
b) 保存用户名密码到user.txt文件(包含用户名和密码)
c) 注册成功

3、登录模块

a) 通过键盘输入用户名和密码
b) 判断(超过三次提示过多错误,需要休眠30秒)
c) 登陆成功

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

class TestRegex{
    public boolean isUser(String user) {
        String regex="[1-9][0-9]{4,9}";
        boolean b=user.matches(regex);
        return b;
    }
    public boolean isMiMa(String mm) {
        String regex="\\w+(\\.*\\w)";
        boolean b=mm.matches(regex);
        return b;
    }
}
public class MySQLregisterTest{
    //1.    注册登录系统
    //1.    创建菜单,注册,登录,退出
    public static void MySQLmenu() {
        System.out.println("***************************");
        System.out.println("*****MySQL注册登录系统*****");
        System.out.println("**1.注册");
        System.out.println("**2.登录");
        System.out.println("**3.退出");
    }
    //2.    注册模块:
    //a)    通过键盘输入用户名,密码
    //b)    保存用户名密码到user.txt文件(包含用户名和密码)
    //c)    注册成功
    public static void MySQLregister() throws IOException {
        TestRegex tr=new TestRegex();
        File f=new File("user.txt");

        Scanner sc=new Scanner(System.in);
        System.out.println("欢迎来到注册界面!");
        System.out.println("请输入用户名!");
        String s=sc.next();
        boolean bu=tr.isUser(s);
        FileInputStream fis=new FileInputStream("user.txt");
        Properties pro=new Properties();
        pro.load(fis);
        String user=pro.getProperty("user");
        String pass=pro.getProperty("pass");
        if(bu==false&&user.equals(s)) {
            System.out.println("账号注册失败");
        }else {
            FileOutputStream fos=new FileOutputStream(f,true);
            byte[] bye=new byte[512];
            int len=0;
            fos.write(("user="+s+"\r\n").getBytes());
            fos.flush();
            fos.close();
            fis.close();
            System.out.println("注册成功");
        }
        System.out.println("请输入密码!");
        String st=sc.next();
        boolean bm=tr.isMiMa(st);
        if(bm==false&&pass.equals(st)) {
            System.out.println("密码注册失败");
        }else {
            FileOutputStream fos=new FileOutputStream(f,true);
            byte[] bye=new byte[512];
            int len=0;
            fos.write(("pass="+st+"\r\n").getBytes());
            fos.flush();
            fos.close();
            fis.close();
            System.out.println("账号注册成功");
        }
    }
    //3.     登录模块
    //a)    通过键盘输入用户名和密码
    
    public static boolean Login() throws IOException{
        boolean flag=false;
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名:");
        String s=sc.next();
        FileInputStream fis=new FileInputStream("user.txt");
        Properties pro=new Properties();
        pro.load(fis);
        String user=pro.getProperty("user");
        String pass=pro.getProperty("pass");
        if(s.equals(user)) {
            System.out.println("请输入密码:");
        }
        String ms=sc.next();
        if(ms.equals(pass)) {
            System.out.println("登录成功");
            flag=true;
        }
        return flag;
    }
    //b)    判断(超过三次提示过多错误,需要休眠30秒)
    //c)    登陆成功
    public static void Oder() {
        int n = 1;
        abc: while (n <4) {
            try {
                boolean flag = Login();
                if (flag == false) {
                    n++;
                } else {
                    System.out.println("账号或密码错误,请确认账号密码");
                    n = 4;
                    break abc;
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException, Exception {
        boolean flag=true;
        while(flag) {
            MySQLmenu();
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入选择项:");
            int n=sc.nextInt();
            switch(n) {
            case 1:
                MySQLregister();
                break;
            case 2:
                Oder();
                System.out.println("输入次数达到上限,休眠30秒");
                Thread.sleep(30000);
                break;
            case 3:
                System.out.println("已退出系统");
                flag=false;
                break;
            default:
                System.out.println("输入异常!请重新输入");
            }
        }
    }
}

上一篇:详解springboot springsecuroty中的注销和权限控制问题

栏    目:JAVA代码

下一篇:SpringBoot自定义注解使用读写分离Mysql数据库的实例教程

本文标题:java实现注册登录系统

本文地址:http://www.codeinn.net/misctech/212431.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有