Compare commits
No commits in common. "sync" and "main" have entirely different histories.
@ -1,37 +0,0 @@
|
||||
package exp11;
|
||||
|
||||
public class Account {
|
||||
private String username;
|
||||
private String password;
|
||||
private boolean lock;
|
||||
|
||||
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() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
boolean isLock() {
|
||||
return lock;
|
||||
}
|
||||
|
||||
void lock() {
|
||||
this.lock = true;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.username +"," +this.password +"," +this.lock;
|
||||
}
|
||||
}
|
@ -1,10 +1,7 @@
|
||||
package exp11;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.*;
|
||||
|
||||
public class AccountDemo {
|
||||
public static void main(String[] args) {
|
||||
JFrame loginFrame = LoginFrame.creatLonginFrame();
|
||||
loginFrame.setVisible(true);
|
||||
}
|
||||
JFrame rejister = new JFrame();
|
||||
}
|
||||
|
@ -1,104 +0,0 @@
|
||||
package exp11;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AccountManager {
|
||||
private String userinfoPath = "D:\\MyProjects\\CoreJava\\exp11\\userinfo.text";
|
||||
private int faildCount=0;
|
||||
|
||||
private ArrayList<Account> getAccounts() {
|
||||
File userinfo = new File(userinfoPath);
|
||||
ArrayList<Account> accounts = new ArrayList<>();
|
||||
try{
|
||||
BufferedReader reader = new BufferedReader(new FileReader(userinfo));
|
||||
String line;
|
||||
while ((line=reader.readLine()) != null) {
|
||||
String[] strs = line.split(",");
|
||||
Account account = new Account(strs[0], strs[1], strs[2]);
|
||||
accounts.add(account);
|
||||
}
|
||||
reader.close();
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return accounts;
|
||||
}
|
||||
|
||||
private Account getAccount(String username) {
|
||||
ArrayList<Account> accounts = getAccounts();
|
||||
for (Account account : accounts) {
|
||||
if(account.getUserName().equals(username)) {
|
||||
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.equals(account.getUserName())){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addUser(Account account) {
|
||||
if (userIsExist(account.getUserName())) {
|
||||
}else {
|
||||
File userinfo = new File(userinfoPath);
|
||||
try{
|
||||
FileWriter writer = new FileWriter(userinfo, true);
|
||||
writer.write(account.toString());
|
||||
writer.close();
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
boolean usernameVerify(String username) {
|
||||
if(username.matches("^[a-zA-Z][a-zA-Z1-9]*$")){
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int accountVerify(String username, String password) {
|
||||
Account account = getAccount(username);
|
||||
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 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
package exp11;
|
||||
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class LoginFrame {
|
||||
|
||||
static JFrame creatLonginFrame() {
|
||||
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){
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
JButton registerButton = new JButton("注册");
|
||||
registerButton.setBounds(90, 200, 60, 30);
|
||||
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,95 +0,0 @@
|
||||
package exp11;
|
||||
|
||||
import java.awt.*;
|
||||
import java.time.LocalDateTime;
|
||||
import javax.swing.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class RegisterFrame {
|
||||
|
||||
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){
|
||||
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 +0,0 @@
|
||||
stu1,123456,false
|
@ -1,21 +0,0 @@
|
||||
package exp12;
|
||||
|
||||
import exp12.TicketingSystem.TicketingSystem;
|
||||
|
||||
public class Person extends Thread{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(3);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if ( !TicketingSystem.buy(Thread.currentThread().getName())) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package exp12.TicketingSystem;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TicketingSystem {
|
||||
private int tickets = 200;
|
||||
private Map<String, Integer> map = new HashMap<>();
|
||||
final int MAX_PERSON = 5;
|
||||
final int MAX_TICKET_PER_PERSON =10;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
public synchronized boolean buy(String user) {
|
||||
if(!map.containsValue(user) && map.size()<MAX_PERSON) {
|
||||
map.put(user, 1);
|
||||
this.tickets--;
|
||||
}else if(map.get(user) < MAX_PERSON) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
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