📄 serverthread.java
字号:
package tarena.bank.server;
import java.util.*;
import java.io.*;
import java.net.Socket;
public class ServerThread extends Thread {
private Socket so;
private boolean isConnected = false;
private DataInputStream inStream = null;
private DataOutputStream outStream = null;
public ServerThread(Socket so) {
this.so = so;
isConnected = true;
try {
inStream = new DataInputStream(so.getInputStream());
outStream = new DataOutputStream(so.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
// 服务线程的发送信息的方法
public void send(String str) {
try {
outStream.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while (isConnected) {
String str = inStream.readUTF();// 读取客户发来的信息
if("exit".equals(str.substring(0, 4))){//必须放在首个判断,不然会出现java.lang.StringIndexOutOfBoundsException
for(int i=0;i<Bank.accountList.size();i++){
if(String.valueOf(Bank.accountList.get(i).getId()).equals(str.substring(4))){
Bank.accountList.remove(i);
}
}
}
if ("register".equals(str.substring(0, 8))) {
Account a = creatAccount(str.substring(8));
FileDAO.accept(a);//写入文件中保存
Bank.accountList.add(a);//将刚注册的用户加入服务队列
for(int i=0;i<Bank.accountList.size();i++){
System.out.println(Bank.accountList.get(i).toString());
}
send("注册成功");
}
//所有的/都是为了分离消息方便而在各个参数之间加的界限标志,如:id/passwd
if ("Loading".equals(str.substring(0, 7))) {
Account a = Bank.getBank().register(
Long.parseLong(str.substring(7, str.indexOf('/'))),
str.substring(str.indexOf('/') + 1));
Bank.accountList.add(a);//将刚登录的用户加入服务队列
for(int i=0;i<Bank.accountList.size();i++){
System.out.println(Bank.accountList.get(i).toString());
}
if (a.type == SavingAccount.ACCOUNT_TYPE) {
SavingAccount sa = (SavingAccount) a;
send("登录成功" + sa.getId() + "/" + sa.getName() + "/"
+ sa.getPersonId() + "/" + sa.getBalance()
+ "/" + sa.type + "/");
} else if (a.type == CreditAccount.ACCOUNT_TYPE) {
CreditAccount ca = (CreditAccount) a;
send("登录成功" + ca.getId() + "/" + ca.getName() + "/"
+ ca.getPersonId() + "/" + ca.getBalance()
+ "/" + ca.getCeiling() + "/" + ca.type + "/");
} else if (a.type == LoanSavingAccount.ACCOUNT_TYPE) {
LoanSavingAccount lsa = (LoanSavingAccount) a;
send("登录成功" + lsa.getId() + "/" + lsa.getName() + "/"
+ lsa.getPersonId() + "/" + lsa.getBalance()
+ "/" + lsa.getLoan() + "/" + lsa.type + "/");
} else if (a.type == LoanCreditAccount.ACCOUNT_TYPE) {
LoanCreditAccount lca = (LoanCreditAccount) a;
send("登录成功" + lca.getId() + "/" + lca.getName() + "/"
+ lca.getPersonId() + "/" + lca.getBalance()
+ "/" + lca.getCeiling() + "/" + lca.getLoan()
+ "/" + lca.type + "/");
}
}
if ("withdraw".equals(str.substring(0, 8))) {
Account a = Bank.getBank().withdraw(
Long.parseLong(str.substring(8, str.indexOf('/'))),
Double.parseDouble(str
.substring(str.indexOf('/') + 1)));
FileDAO.accept(a);
send("取款成功" + a.getBalance());
}
if ("deposit".equals(str.substring(0, 7))) {
Account a = Bank.getBank().deposit(
Long.parseLong(str.substring(7, str.indexOf('/'))),
Double.parseDouble(str
.substring(str.indexOf('/') + 1)));
FileDAO.accept(a);
send("存款成功" + a.getBalance());
}
if ("setCeiling".equals(str.substring(0, 10))) {
Account a = Bank.getBank()
.setCeiling(
Long.parseLong(str.substring(10, str
.indexOf('/'))),
Double.parseDouble(str.substring(str
.indexOf('/') + 1)));
if(a!=null){
FileDAO.accept(a);
send("透支额度设置成功" + ((CreditAccount) a).getCeiling());
}else {
System.out.println("您的账户类型与您的操作不匹配");
}
}
if (str.length() > 11 && "requestLoan".equals(str.substring(0, 11))) {
Account a = Bank.getBank()
.requestLoan(
Long.parseLong(str.substring(11, str
.indexOf('/'))),
Double.parseDouble(str.substring(str
.indexOf('/') + 1)));
if(a!=null){
FileDAO.accept(a);
if (a instanceof LoanSavingAccount) {
send("代款成功" + ((LoanSavingAccount) a).getLoan());
} else if (a instanceof LoanCreditAccount) {
send("代款成功" + ((LoanCreditAccount) a).getLoan());
}
}else {
System.out.println("您的账户类型与您的操作不匹配");
}
}
if ("payLoan".equals(str.substring(0, 7))) {
Account a = Bank.getBank().payLoan(
Long.parseLong(str.substring(7, str.indexOf('/'))),
Double.parseDouble(str
.substring(str.indexOf('/') + 1)));
if(a!=null){
FileDAO.accept(a);
if (a instanceof LoanSavingAccount) {
send("还代款成功" + ((LoanSavingAccount) a).getLoan() + "/"
+ a.getBalance());
} else if (a instanceof LoanCreditAccount) {
send("还代款成功" + ((LoanCreditAccount) a).getLoan() + "/"
+ a.getBalance());
}
}else {
System.out.println("您的账户类型与您的操作不匹配");
}
}
}
inStream.close();
so.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public Account creatAccount(String s) {
List<String> list = new ArrayList<String>();
int index = 0;
//用于解析分离客户端发来的创建账户所需的参数信息
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '/') {
list.add(s.substring(index, i));
index = i + 1;
}
}
return Bank.register(Long.parseLong(list.get(0)), list.get(1), list
.get(2), list.get(3), list.get(4), Double.parseDouble(list
.get(5)), Integer.parseInt(list.get(6)));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -