📄 accountsmanager.java
字号:
package za.co.halo.SecureCommunications.gui.server;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Vector;
import za.co.halo.SecureCommunications.AccountOfClient;
import za.co.halo.SecureCommunications.ServerAccount;
import za.co.halo.SecureCommunications.ServerAdmin;
import za.co.halo.SecureCommunications.util.FileUtil;
public class AccountsManager implements Serializable{
private Vector<Account> accounts;
public AccountsManager(Vector<Account> accounts)
{
this.accounts = accounts;
}
public void addAccount(Account account)
{
accounts.addElement(account);
}
public void removeAccount(Account account)
{
accounts.removeElement(account);
}
public int getNumberOfAccounts()
{
return accounts.size();
}
public Account getAccount(int pos)
{
return accounts.get(pos);
}
public void save()
{
File f = new File("servera.dat");
try
{
f.createNewFile();
if (f.canWrite())
{
FileUtil.write(f,this);
}
else
{
ServerAdmin.error.warning("Could not save the accounts.\nPlease make sure the "+f.getAbsolutePath()+" file is accessable.");
}
}
catch(IOException e)
{
e.printStackTrace();
ServerAdmin.error.warning("Could not save the accounts.\nPlease make sure the "+f.getAbsolutePath()+" file is accessable.");
}
}
public void load()
{
File f = new File("servera.dat");
try
{
if(f.exists())
{
if(f.canRead())
{
AccountsManager m = (AccountsManager)FileUtil.readFile(f);
this.accounts = m.accounts;
}
else
{
ServerAdmin.error.alert("Could not read accounts from file " + f.getAbsolutePath());
}
}
else
{
ServerAdmin.error.warning("Could not load accounts from file. Creating a new file.");
}
}
catch(IOException e)
{
e.printStackTrace();
ServerAdmin.error.alert("Could not load accounts from file");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
/*public Vector<Account> getAccounts()
{
Vector<Account> a = null;
File f = new File("servera.dat");
if (f.exists())
try {
a = (Vector<Account>)FileUtil.readFile(f);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*Vector<AccountOfClient> e = new Vector<AccountOfClient>(10);
e.addElement(new AccountOfClient("JonnyDraco", "Isabel",
"hiv", "tuksSMTPServer.co.za", "Isabel van Vuuren"));
e.addElement(new AccountOfClient("ThePrince", "Isabel",
"hiv", "tuksSMTPServer.co.za", "Isabel van Vuuren"));
e.addElement(new AccountOfClient("Anlien","eclipse","athena","Anlien"));*/
/*if (a == null)
a = new Vector<Account>(10);
return a;
}*/
public static void main(String[] args)
{
/*Account a1 = new Account("n1","v1","u1","p1","e1");
Account a2 = new Account("n2","v2","u2","p2","e2");
Account a3 = new Account("n3","v3","u3","p3","e3");
Account a4 = new Account("n4","v4","u4","p4","e4");
Account a5 = new Account("n5","v5","u5","p5","e5");
Vector<Account> v = new Vector();
AccountsManager man = new AccountsManager(v);
man.addAccount(a1);
man.save();
man.addAccount(a2);
man.save();
man.addAccount(a3);
man.save();
man.addAccount(a4);
man.save();
man.addAccount(a5);
man.save();*/
Vector<Account> v = new Vector();
AccountsManager man = new AccountsManager(v);
//man.load();
//Vector<Account> acc = new Vector();
//acc = man.getAccounts();
//man.getAccounts();
man.load();
//System.out.println(man.getNumberOfAccounts());
//Account aman.getAccount(0))
for (int i = 0; i < man.getNumberOfAccounts(); i++)
{
Account ama = man.getAccount(i);
System.out.println(ama.getFirstName());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -