From 222e3def13767d1e6760a5924e9df411c44bda14 Mon Sep 17 00:00:00 2001 From: skyofdream <1662992055@qq.com> Date: Wed, 15 May 2024 23:50:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exp11/Account.java | 18 +++++++- exp11/AccountDemo.java | 14 ++---- exp11/AccountManager.java | 46 ++++++++++++------- exp11/LoginFrame.java | 69 ++++++++++++++++++++++++++-- exp11/RegisterFrame.java | 94 ++++++++++++++++++++++++++++++++++----- exp11/Tools.java | 36 --------------- exp11/userinfo.text | 1 + test/RegexTest.java | 16 +++++++ 8 files changed, 213 insertions(+), 81 deletions(-) delete mode 100644 exp11/Tools.java create mode 100644 exp11/userinfo.text create mode 100644 test/RegexTest.java diff --git a/exp11/Account.java b/exp11/Account.java index a800382..281b3da 100644 --- a/exp11/Account.java +++ b/exp11/Account.java @@ -3,10 +3,16 @@ package exp11; public class Account { private String username; private String password; + private boolean lock; - public Account(String username, String password) { + public Account(String username, String password, String lock) { this.username = username; this.password = password; + if(lock.equals("true")){ + this.lock = true; + }else { + this.lock = false; + } } String getUserName() { @@ -17,7 +23,15 @@ public class Account { return this.password; } + boolean isLock() { + return lock; + } + + void lock() { + this.lock = true; + } + public String toString() { - return this.username +"," +this.password; + return this.username +"," +this.password +"," +this.lock; } } diff --git a/exp11/AccountDemo.java b/exp11/AccountDemo.java index 269584d..30a4bcb 100644 --- a/exp11/AccountDemo.java +++ b/exp11/AccountDemo.java @@ -1,18 +1,10 @@ package exp11; -import java.awt.*; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; - -import javax.swing.*; +import javax.swing.JFrame; public class AccountDemo { public static void main(String[] args) { - JFrame register = LoginFrame.creatLonginFrame(); - - register.setVisible(true); + JFrame loginFrame = LoginFrame.creatLonginFrame(); + loginFrame.setVisible(true); } - - - } diff --git a/exp11/AccountManager.java b/exp11/AccountManager.java index 47e37f2..aac1c55 100644 --- a/exp11/AccountManager.java +++ b/exp11/AccountManager.java @@ -8,7 +8,8 @@ import java.io.IOException; import java.util.ArrayList; public class AccountManager { - private String userinfoPath = "userinfo.txt"; + private String userinfoPath = "D:\\MyProjects\\CoreJava\\exp11\\userinfo.text"; + private int faildCount=0; private ArrayList getAccounts() { File userinfo = new File(userinfoPath); @@ -18,7 +19,7 @@ public class AccountManager { String line; while ((line=reader.readLine()) != null) { 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); } reader.close(); @@ -29,22 +30,29 @@ public class AccountManager { return accounts; } - Account getAccount(String username) { - Account account = null; + private Account getAccount(String username) { ArrayList accounts = getAccounts(); - for (Account a : accounts) { - if(a.getUserName() == username) { - account = a; + for (Account account : accounts) { + if(account.getUserName().equals(username)) { + return account; } } - return account; + return null; + } + + int getFaildCount(){ + return this.faildCount; + } + + void resetFaildCount() { + this.faildCount=0; } public boolean userIsExist(String username) { ArrayList accounts = getAccounts(); for (Account account : accounts) { - if (username == account.getUserName()){ + if (username.equals(account.getUserName())){ return true; } } @@ -54,7 +62,6 @@ public class AccountManager { public void addUser(Account account) { if (userIsExist(account.getUserName())) { - System.out.println("用户已存在!"); }else { File userinfo = new File(userinfoPath); try{ @@ -69,21 +76,28 @@ public class AccountManager { } boolean usernameVerify(String username) { - if(username.matches("a-zA-Z")){ + if(username.matches("^[a-zA-Z][a-zA-Z1-9]*$")){ return true; }else { - System.out.println("用户名不合法"); return false; } } - boolean accountVerify(String username, String password) { + int accountVerify(String username, String password) { Account account = getAccount(username); - if ((account.getUserName()==username) && (account.getPassword()==password)){ - return true; + System.out.println(account.toString()); + 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; } diff --git a/exp11/LoginFrame.java b/exp11/LoginFrame.java index 6f9f418..7f91456 100644 --- a/exp11/LoginFrame.java +++ b/exp11/LoginFrame.java @@ -1,18 +1,59 @@ package exp11; -import java.awt.event.*; +import java.awt.*; +import java.time.LocalDateTime; import javax.swing.*; +import java.awt.event.*; public class LoginFrame { + 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("登录"); loginJButton.setBounds(240, 200, 60, 30); loginJButton.addMouseListener(new MouseAdapter() { @Override 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() { @Override 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(registerButton); 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); + } } diff --git a/exp11/RegisterFrame.java b/exp11/RegisterFrame.java index 8f6f2b9..dd6259e 100644 --- a/exp11/RegisterFrame.java +++ b/exp11/RegisterFrame.java @@ -1,25 +1,95 @@ package exp11; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; - -import javax.swing.JButton; -import javax.swing.JFrame; +import java.awt.*; +import java.time.LocalDateTime; +import javax.swing.*; +import java.awt.event.*; public class RegisterFrame { - static JFrame creatRegisterFrame() { - JFrame f = Tools.creatDefaultJFrame("注册"); - JButton registerButton = new JButton("注册"); - registerButton.setBounds(220, 200, 60, 30); - registerButton.addMouseListener(new MouseAdapter() { + static JFrame creatRegisterFrame() { + 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); + + 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 - 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; } + + 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); + } } diff --git a/exp11/Tools.java b/exp11/Tools.java deleted file mode 100644 index 21ec923..0000000 --- a/exp11/Tools.java +++ /dev/null @@ -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; - } - - -} \ No newline at end of file diff --git a/exp11/userinfo.text b/exp11/userinfo.text new file mode 100644 index 0000000..1bc0146 --- /dev/null +++ b/exp11/userinfo.text @@ -0,0 +1 @@ +stu1,123456,false \ No newline at end of file diff --git a/test/RegexTest.java b/test/RegexTest.java new file mode 100644 index 0000000..0aae3be --- /dev/null +++ b/test/RegexTest.java @@ -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)的字符"); + } + } +}