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 getAccounts() { File userinfo = new File(userinfoPath); ArrayList 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 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 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; } }