This commit is contained in:
skyofdream 2024-05-15 23:50:53 +08:00
parent b674ec18c6
commit 222e3def13
8 changed files with 213 additions and 81 deletions

View File

@ -3,10 +3,16 @@ package exp11;
public class Account { public class Account {
private String username; private String username;
private String password; private String password;
private boolean lock;
public Account(String username, String password) { public Account(String username, String password, String lock) {
this.username = username; this.username = username;
this.password = password; this.password = password;
if(lock.equals("true")){
this.lock = true;
}else {
this.lock = false;
}
} }
String getUserName() { String getUserName() {
@ -17,7 +23,15 @@ public class Account {
return this.password; return this.password;
} }
boolean isLock() {
return lock;
}
void lock() {
this.lock = true;
}
public String toString() { public String toString() {
return this.username +"," +this.password; return this.username +"," +this.password +"," +this.lock;
} }
} }

View File

@ -1,18 +1,10 @@
package exp11; package exp11;
import java.awt.*; import javax.swing.JFrame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class AccountDemo { public class AccountDemo {
public static void main(String[] args) { public static void main(String[] args) {
JFrame register = LoginFrame.creatLonginFrame(); JFrame loginFrame = LoginFrame.creatLonginFrame();
loginFrame.setVisible(true);
register.setVisible(true);
} }
} }

View File

@ -8,7 +8,8 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
public class AccountManager { public class AccountManager {
private String userinfoPath = "userinfo.txt"; private String userinfoPath = "D:\\MyProjects\\CoreJava\\exp11\\userinfo.text";
private int faildCount=0;
private ArrayList<Account> getAccounts() { private ArrayList<Account> getAccounts() {
File userinfo = new File(userinfoPath); File userinfo = new File(userinfoPath);
@ -18,7 +19,7 @@ public class AccountManager {
String line; String line;
while ((line=reader.readLine()) != null) { while ((line=reader.readLine()) != null) {
String[] strs = line.split(","); String[] strs = line.split(",");
Account account = new Account(strs[0], strs[1]); Account account = new Account(strs[0], strs[1], strs[2]);
accounts.add(account); accounts.add(account);
} }
reader.close(); reader.close();
@ -29,22 +30,29 @@ public class AccountManager {
return accounts; return accounts;
} }
Account getAccount(String username) { private Account getAccount(String username) {
Account account = null;
ArrayList<Account> accounts = getAccounts(); ArrayList<Account> accounts = getAccounts();
for (Account a : accounts) { for (Account account : accounts) {
if(a.getUserName() == username) { if(account.getUserName().equals(username)) {
account = a; return account;
} }
} }
return account; return null;
}
int getFaildCount(){
return this.faildCount;
}
void resetFaildCount() {
this.faildCount=0;
} }
public boolean userIsExist(String username) { public boolean userIsExist(String username) {
ArrayList<Account> accounts = getAccounts(); ArrayList<Account> accounts = getAccounts();
for (Account account : accounts) { for (Account account : accounts) {
if (username == account.getUserName()){ if (username.equals(account.getUserName())){
return true; return true;
} }
} }
@ -54,7 +62,6 @@ public class AccountManager {
public void addUser(Account account) { public void addUser(Account account) {
if (userIsExist(account.getUserName())) { if (userIsExist(account.getUserName())) {
System.out.println("用户已存在!");
}else { }else {
File userinfo = new File(userinfoPath); File userinfo = new File(userinfoPath);
try{ try{
@ -69,21 +76,28 @@ public class AccountManager {
} }
boolean usernameVerify(String username) { boolean usernameVerify(String username) {
if(username.matches("a-zA-Z")){ if(username.matches("^[a-zA-Z][a-zA-Z1-9]*$")){
return true; return true;
}else { }else {
System.out.println("用户名不合法");
return false; return false;
} }
} }
boolean accountVerify(String username, String password) { int accountVerify(String username, String password) {
Account account = getAccount(username); Account account = getAccount(username);
if ((account.getUserName()==username) && (account.getPassword()==password)){ System.out.println(account.toString());
return true; if(account.isLock()){
return -1;
}else if ((username.equals(account.getUserName())) && (password.equals(account.getPassword()))){
return 1;
}else {
this.faildCount++;
if(faildCount >= 3){
account.lock();
}
} }
return false; return 0;
} }

View File

@ -1,18 +1,59 @@
package exp11; package exp11;
import java.awt.event.*; import java.awt.*;
import java.time.LocalDateTime;
import javax.swing.*; import javax.swing.*;
import java.awt.event.*;
public class LoginFrame { public class LoginFrame {
static JFrame creatLonginFrame() { static JFrame creatLonginFrame() {
JFrame f = Tools.creatDefaultJFrame("登录"); AccountManager am = new AccountManager();
JFrame f = new JFrame("登录");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(400, 300);
f.setLayout(null);
f.setLocationRelativeTo(f);
f.setAlwaysOnTop(true);
f.setResizable(false);
JLabel accountJLabel = new JLabel("账号");
accountJLabel.setBounds(50, 50, 50, 20);
JTextField usernameTextField = new JTextField();
usernameTextField.setBounds(100, 50, 200, 20);
JLabel passwordJLabel = new JLabel("密码");
passwordJLabel.setBounds(50, 100, 50, 20);
JPasswordField passwordField = new JPasswordField();
passwordField.setBounds(100, 100, 200, 20);
JButton loginJButton = new JButton("登录"); JButton loginJButton = new JButton("登录");
loginJButton.setBounds(240, 200, 60, 30); loginJButton.setBounds(240, 200, 60, 30);
loginJButton.addMouseListener(new MouseAdapter() { loginJButton.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e){ public void mouseClicked(MouseEvent e){
System.out.println("点击[登录]"); String username = usernameTextField.getText();
String password = new String(passwordField.getPassword());
if (!am.userIsExist(username)) {
showDialog(f, "用户不存在!");
}else {
int verify = am.accountVerify(username, password);
if(am.getFaildCount() >= 3){
String text = "用户" +username +"已被锁定,请与管理联系!";
showDialog(f, text);
//am.resetFaildCount();
}else if (verify==1) {
showDialog(f, "登陆成功");
am.resetFaildCount();
}else if (verify==0){
String text = "密码错误,你还有" +(3-am.getFaildCount()) +"次机会";
showDialog(f, text);
}
}
} }
}); });
@ -21,14 +62,34 @@ public class LoginFrame {
registerButton.addMouseListener(new MouseAdapter() { registerButton.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e){ public void mouseClicked(MouseEvent e){
JFrame registerFrame = RegisterFrame.creatRegisterFrame();
registerFrame.setVisible(true);
f.dispose();
} }
}); });
f.add(accountJLabel);
f.add(usernameTextField);
f.add(passwordJLabel);
f.add(passwordField);
f.add(loginJButton); f.add(loginJButton);
f.add(registerButton); f.add(registerButton);
return f; return f;
} }
static void showDialog(JFrame f, String text) {
JDialog dialog = new JDialog(f, "提示");
dialog.setSize(200, 150);
dialog.setLocationRelativeTo(f);
dialog.setDefaultCloseOperation(1);
dialog.setLayout(new FlowLayout());
JLabel textLabel = new JLabel(text);
dialog.add(textLabel);
dialog.setVisible(true);
}
} }

View File

@ -1,25 +1,95 @@
package exp11; package exp11;
import java.awt.event.MouseAdapter; import java.awt.*;
import java.awt.event.MouseEvent; import java.time.LocalDateTime;
import javax.swing.*;
import javax.swing.JButton; import java.awt.event.*;
import javax.swing.JFrame;
public class RegisterFrame { public class RegisterFrame {
static JFrame creatRegisterFrame() {
JFrame f = Tools.creatDefaultJFrame("注册");
JButton registerButton = new JButton("注册"); static JFrame creatRegisterFrame() {
registerButton.setBounds(220, 200, 60, 30); AccountManager am = new AccountManager();
registerButton.addMouseListener(new MouseAdapter() {
JFrame f = new JFrame("注册");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(400, 300);
f.setLayout(null);
f.setLocationRelativeTo(f);
f.setAlwaysOnTop(true);
f.setResizable(false);
JLabel accountJLabel = new JLabel("账号");
accountJLabel.setBounds(50, 50, 50, 20);
JTextField usernameTextField = new JTextField();
usernameTextField.setBounds(100, 50, 200, 20);
JLabel passwordJLabel = new JLabel("密码");
passwordJLabel.setBounds(50, 100, 50, 20);
JPasswordField passwordField = new JPasswordField();
passwordField.setBounds(100, 100, 200, 20);
JLabel confirmPasswordJLabel = new JLabel("确认密码");
confirmPasswordJLabel.setBounds(50, 150, 50, 20);
JPasswordField confirmPasswordField = new JPasswordField();
confirmPasswordField.setBounds(100, 150, 200, 20);
JButton loginJButton = new JButton("登录");
loginJButton.setBounds(90, 200, 60, 30);
loginJButton.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e){
JFrame loginFrame = LoginFrame.creatLonginFrame();
loginFrame.setVisible(true);
f.dispose();
} }
}); });
JButton registerButton = new JButton("注册");
registerButton.setBounds(240, 200, 60, 30);
registerButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e){
String username = usernameTextField.getText();
String password = new String(passwordField.getPassword());
Account account = new Account(username, password, "false");
if (am.userIsExist(username)){
showDialog(f, "用户已存在!");
}else {
am.addUser(account);
String text = username +"" +LocalDateTime.now().toString() +"注册成功";
showDialog(f, text);
}
}
});
f.add(accountJLabel);
f.add(usernameTextField);
f.add(passwordJLabel);
f.add(passwordField);
f.add(confirmPasswordJLabel);
f.add(confirmPasswordField);
f.add(loginJButton);
f.add(registerButton);
return f; return f;
} }
static void showDialog(JFrame f, String text) {
JDialog dialog = new JDialog(f, "提示");
dialog.setSize(200, 150);
dialog.setLocationRelativeTo(f);
dialog.setDefaultCloseOperation(1);
dialog.setLayout(new FlowLayout());
JLabel textLabel = new JLabel(text);
dialog.add(textLabel);
dialog.setVisible(true);
}
} }

View File

@ -1,36 +0,0 @@
package exp11;
import javax.swing.*;
public class Tools {
private String userInfoPath = "userinfo.txt";
public static JFrame creatDefaultJFrame(String title){
JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(400, 300);
f.setLayout(null);
f.setLocationRelativeTo(f);
f.setAlwaysOnTop(true);
f.setResizable(false);
JLabel accountJLabel = new JLabel("账号");
accountJLabel.setBounds(50, 50, 50, 20);
JTextField accounJTextField = new JTextField();
accounJTextField.setBounds(100, 50, 200, 20);
JLabel passwordJLabel = new JLabel("密码");
passwordJLabel.setBounds(50, 100, 50, 20);
JPasswordField passwordField = new JPasswordField();
passwordField.setBounds(100, 100, 200, 20);
f.add(accountJLabel);
f.add(accounJTextField);
f.add(passwordJLabel);
f.add(passwordField);
return f;
}
}

1
exp11/userinfo.text Normal file
View File

@ -0,0 +1 @@
stu1,123456,false

16
test/RegexTest.java Normal file
View File

@ -0,0 +1,16 @@
package test;
public class RegexTest {
public static void main(String[] args) {
String regex = "^[a-zA-Z][a-zA-Z1-9]*$";
String testString = "Abc123"; // 测试字符串
boolean matches = testString.matches(regex);
if (matches) {
System.out.println("字符串仅由字母和数字1-9组成");
} else {
System.out.println("字符串包含非字母或非数字1-9的字符");
}
}
}