Advertisement
Guest User

accountmanager

a guest
Dec 7th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /**
  5.  *
  6.  * @author Zion
  7.  *
  8.  * This class manages all stored accounts
  9.  *
  10.  */
  11. public class AccountManager {
  12.    
  13.     /**
  14.      * Account file save location
  15.      */
  16.     private static File saveAccountFile = new File(Signlink.findcachedir() + "/accounts.dat");
  17.    
  18.     /**
  19.      * Stores the accounts
  20.      */
  21.     public static List<AccountData> accounts = new LinkedList<AccountData>();
  22.    
  23.     /**
  24.      * Gets all the stored accounts in the map
  25.      *
  26.      * @return
  27.      */
  28.     public static List<AccountData> getAccounts() {
  29.         return accounts;
  30.     }
  31.    
  32.     /**
  33.      * Gets account for specified name
  34.      *
  35.      * @param username
  36.      * @return
  37.      */
  38.     public static AccountData getAccount(String username) {
  39.         for (AccountData account : accounts) {
  40.             if (account.username.equalsIgnoreCase(username)) {
  41.                 return account;
  42.             }
  43.         }
  44.         return null;
  45.     }
  46.    
  47.     /**
  48.      *  Adds the account to the account map
  49.      *
  50.      * @param account
  51.      */
  52.     public static void addAccount(AccountData account) {
  53.         if (!accounts.isEmpty()) {
  54.             for (int size = 0; size < accounts.size(); size++) {
  55.                 if (accounts.get(size).username.equalsIgnoreCase(account.username)) {
  56.                     accounts.get(size).uses += 1;
  57.                     saveAccount();
  58.                     return;
  59.                 }
  60.             }
  61.         }
  62.         if (account.username == null && account.username.length() <= 0 || account.password == null && account.password.length() <= 0) {
  63.             return;
  64.         }
  65.         accounts.add(account);
  66.         saveAccount();
  67.     }
  68.    
  69.     /**
  70.      * Clears the account map
  71.      */
  72.     public static void clearAccountList() {
  73.         accounts.clear();
  74.         saveAccount();
  75.     }
  76.    
  77.     /**
  78.      * Removes a desired account from the <accounts> map
  79.      *
  80.      * @param account
  81.      */
  82.     public static void removeAccount(AccountData account) {
  83.         if (!accounts.contains(account)) {
  84.             return;
  85.         }
  86.         accounts.remove(account);
  87.         saveAccount();
  88.     }
  89.    
  90.     /**
  91.      * Saves the account
  92.      */
  93.     public static void saveAccount() {
  94.         if (accounts == null || accounts.isEmpty()) {
  95.             return;
  96.         }
  97.         try {
  98.             DataOutputStream output = new DataOutputStream(new FileOutputStream(saveAccountFile));
  99.             output.writeByte(accounts.size());
  100.             for (int index = 0; index < accounts.size(); index++) {
  101.                 AccountData account = accounts.get(index);
  102.                 output.writeInt(account.rank);
  103.                 output.writeInt(account.uses);
  104.                 output.writeUTF(account.username);
  105.                 output.writeUTF(account.password);
  106.             }
  107.             output.close();
  108.         } catch (Exception e) {
  109.             e.printStackTrace();
  110.         }
  111.     }
  112.    
  113.     /**
  114.      * Loads the account
  115.      */
  116.     public static void loadAccount() {
  117.         if (!saveAccountFile.exists()) {
  118.             return;
  119.         }
  120.         try {
  121.             DataInputStream input = new DataInputStream(new FileInputStream(saveAccountFile));
  122.             int fileSize = input.readByte();
  123.             for (int index = 0; index < fileSize; index++) {
  124.                 int rank = input.readInt();
  125.                 int uses = input.readInt();
  126.                 String username = input.readUTF();
  127.                 String password = input.readUTF();
  128.                 AccountData account = new AccountData(rank, uses, username, password);
  129.                 accounts.add(account);
  130.             }
  131.             input.close();
  132.         } catch(Exception e) {
  133.             e.printStackTrace();
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement