Home > Java > javaTutorial > How to implement registration and login system in java

How to implement registration and login system in java

WBOY
Release: 2023-04-24 23:31:13
forward
2792 people have browsed it

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("输入异常!请重新输入");
            }
        }
    }
}
Copy after login

The above is the detailed content of How to implement registration and login system in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template