CoreJava/exp11/AccountManager.java

105 lines
2.7 KiB
Java
Raw Permalink Normal View History

2024-05-15 20:49:31 +08:00
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 {
2024-05-15 23:50:53 +08:00
private String userinfoPath = "D:\\MyProjects\\CoreJava\\exp11\\userinfo.text";
private int faildCount=0;
2024-05-15 20:49:31 +08:00
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(",");
2024-05-15 23:50:53 +08:00
Account account = new Account(strs[0], strs[1], strs[2]);
2024-05-15 20:49:31 +08:00
accounts.add(account);
}
reader.close();
}catch(IOException e) {
e.printStackTrace();
}
return accounts;
}
2024-05-15 23:50:53 +08:00
private Account getAccount(String username) {
2024-05-15 20:49:31 +08:00
ArrayList<Account> accounts = getAccounts();
2024-05-15 23:50:53 +08:00
for (Account account : accounts) {
if(account.getUserName().equals(username)) {
return account;
2024-05-15 20:49:31 +08:00
}
}
2024-05-15 23:50:53 +08:00
return null;
}
int getFaildCount(){
return this.faildCount;
}
void resetFaildCount() {
this.faildCount=0;
2024-05-15 20:49:31 +08:00
}
public boolean userIsExist(String username) {
ArrayList<Account> accounts = getAccounts();
for (Account account : accounts) {
2024-05-15 23:50:53 +08:00
if (username.equals(account.getUserName())){
2024-05-15 20:49:31 +08:00
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) {
2024-05-15 23:50:53 +08:00
if(username.matches("^[a-zA-Z][a-zA-Z1-9]*$")){
2024-05-15 20:49:31 +08:00
return true;
}else {
return false;
}
}
2024-05-15 23:50:53 +08:00
int accountVerify(String username, String password) {
2024-05-15 20:49:31 +08:00
Account account = getAccount(username);
2024-05-15 23:50:53 +08:00
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();
}
2024-05-15 20:49:31 +08:00
}
2024-05-15 23:50:53 +08:00
return 0;
2024-05-15 20:49:31 +08:00
}
}