📄 bank.java
字号:
package tarena.bank.server;
import java.io.*;
import java.net.*;
import java.util.*;
public class Bank extends Thread {
private ServerSocket ss = null;
static List<Account> accountList;
private static Bank bank;
public Bank() {
accountList = new ArrayList<Account>();
try {
ss = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized static Bank getBank() {
if (bank == null) {
bank = new Bank();
}
return bank;
}
public void run() {
while (true) {
try {
Socket so = ss.accept();
ServerThread st = new ServerThread(so);
st.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Account register(long id, String passwd, String name,
String personId, String email, double balance, int type) {
if (type == SavingAccount.ACCOUNT_TYPE) {
return new SavingAccount(id, passwd, name, personId, email,
balance, type);
} else if (type == CreditAccount.ACCOUNT_TYPE) {
return new CreditAccount(id, passwd, name, personId, email,
balance, type);
} else if (type == LoanSavingAccount.ACCOUNT_TYPE) {
return new LoanSavingAccount(id, passwd, name, personId, email,
balance, type);
} else if (type == LoanCreditAccount.ACCOUNT_TYPE) {
return new LoanCreditAccount(id, passwd, name, personId, email,
balance, type);
}
return null;
}
public Account register(long id, String passwd) {
//从文件中读取Account
return FileDAO.getAccount(id, passwd);
}
public Account deposit(long id, double dmoney) {
// Account a = null;//同上
int i = 0;
for (i = 0; i < accountList.size(); i++) {
if (accountList.get(i).getId() == id) {
accountList.get(i).deposit(dmoney);
// accountList.get(i).setBalance(accountList.get(i).getBalance()
// + dmoney);
break;
}
}
return accountList.get(i);
}
public Account withdraw(long id, double tmoney) {
// Account a = null;
int i = 0;
for (i = 0; i < accountList.size(); i++) {
if (accountList.get(i).getId() == id) {
// a = account[i];
if (accountList.get(i) instanceof SavingAccount) {
SavingAccount sa = (SavingAccount) accountList.get(i);
sa.withdraw(tmoney);
} else if (accountList.get(i) instanceof CreditAccount) {
CreditAccount ca = (CreditAccount) accountList.get(i);
ca.withdraw(tmoney);
}
break;
}
}
return accountList.get(i);
}
public Account setCeiling(long id, double cmoney) {
// Account a = null;
int i = 0;
for (i = 0; i < accountList.size(); i++) {
if (accountList.get(i).getId() == id) {
// a = account[i];
if (accountList.get(i) instanceof CreditAccount) {
CreditAccount ca = (CreditAccount) accountList.get(i);
ca.setCeiling(cmoney);
}
return accountList.get(i);
}
}
return null;
}
public double statAll(List<Account> accountList) {
double all = 0;
for (int i = 0; i < accountList.size(); i++) {
all += accountList.get(i).getBalance();
}
return all;
}
public double statAllCeiling(List<Account> accountList) {
double all = 0;
for (int i = 0; i < accountList.size(); i++) {
if (accountList.get(i) instanceof CreditAccount) {
CreditAccount ca = (CreditAccount) accountList.get(i);
all += ca.getCeiling();
}
}
return all;
}
public Account requestLoan(long id, double lmoney) {
int i = 0;
for (i = 0; i < accountList.size(); i++) {
if (accountList.get(i).getId() == id) {
if (accountList.get(i) instanceof LoanSavingAccount) {
((LoanSavingAccount) accountList.get(i))
.requestLoan(lmoney);
return accountList.get(i);
} else if (accountList.get(i) instanceof LoanCreditAccount) {
((LoanCreditAccount) accountList.get(i))
.requestLoan(lmoney);
return accountList.get(i);
}
}
}
return null;
}
public Account payLoan(long id, double pmoney) {
int i = 0;
for (i = 0; i < accountList.size(); i++) {
if (accountList.get(i).getId() == id) {
if (accountList.get(i) instanceof LoanSavingAccount) {
((LoanSavingAccount) accountList.get(i)).payLoan(pmoney);
return accountList.get(i);
} else if (accountList.get(i) instanceof LoanCreditAccount) {
((LoanCreditAccount) accountList.get(i)).payLoan(pmoney);
return accountList.get(i);
}
}
}
return null;
}
public double statAllLoan(List<Account> accountList) {
double all = 0;
for (int i = 0; i < accountList.size(); i++) {
if (accountList.get(i) instanceof LoanCreditAccount) {
LoanCreditAccount ca = (LoanCreditAccount) accountList.get(i);
all += ca.getAllLoan();
} else if (accountList.get(i) instanceof LoanSavingAccount) {
LoanSavingAccount ca = (LoanSavingAccount) accountList.get(i);
all += ca.getAllLoan();
}
}
return all;
}
public static void main(String[] args) {
getBank().start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -