java實現(xiàn)注冊登錄系統(tǒng)
本文實例為大家分享了java實現(xiàn)注冊登錄系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
1、創(chuàng)建菜單,注冊,登錄,退出
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.?? ?注冊登錄系統(tǒng)
?? ?//1.?? ?創(chuàng)建菜單,注冊,登錄,退出
?? ?public static void MySQLmenu() {
?? ??? ?System.out.println("***************************");
?? ??? ?System.out.println("*****MySQL注冊登錄系統(tǒng)*****");
?? ??? ?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("輸入次數(shù)達到上限,休眠30秒");
?? ??? ??? ??? ?Thread.sleep(30000);
?? ??? ??? ??? ?break;
?? ??? ??? ?case 3:
?? ??? ??? ??? ?System.out.println("已退出系統(tǒng)");
?? ??? ??? ??? ?flag=false;
?? ??? ??? ??? ?break;
?? ??? ??? ?default:
?? ??? ??? ??? ?System.out.println("輸入異常!請重新輸入");
?? ??? ??? ?}
?? ??? ?}
?? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一文詳解如何使用線程池來優(yōu)化我們的應(yīng)用程序
線程池是一種工具,但并不是適用于所有場景。在使用線程池時,我們需要根據(jù)應(yīng)用程序的性質(zhì)、計算資源的可用性和應(yīng)用程序的需求進行適當(dāng)?shù)呐渲?。本文主要介紹了如何使用線程池來優(yōu)化我們的應(yīng)用程序,需要的可以參考一下2023-04-04
java為什么使用BlockingQueue解決競態(tài)條件問題面試精講
這篇文章主要為大家介紹了java為什么使用BlockingQueue解決競態(tài)條件問題面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
Java數(shù)據(jù)結(jié)構(gòu)之鏈表的增刪查改詳解
在這篇文章中,小編將帶大家了解一下Java數(shù)據(jù)結(jié)構(gòu)中鏈表的增刪查改(以下結(jié)果均在IDEA中編譯)希望在方便自己復(fù)習(xí)的同時也能幫助到大家2022-09-09

