同步
This commit is contained in:
parent
b674ec18c6
commit
222e3def13
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -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<Account> 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<Account> 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<Account> 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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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("注册");
|
||||
AccountManager am = new AccountManager();
|
||||
|
||||
JButton registerButton = new JButton("注册");
|
||||
registerButton.setBounds(220, 200, 60, 30);
|
||||
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
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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
1
exp11/userinfo.text
Normal file
@ -0,0 +1 @@
|
||||
stu1,123456,false
|
16
test/RegexTest.java
Normal file
16
test/RegexTest.java
Normal 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)的字符");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user