📄 sytcpserverthread.java
字号:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class SYTCPServerThread implements Runnable {
private SYBank SYBank=null;
public SYTCPServerThread(SYBank SYBank){
this.SYBank=SYBank;
}
private Socket s=null;
public SYTCPServerThread(Socket s) {
this.s=s;
}
private boolean t=false; //控制是否正确的帐号
private boolean t1=false;//控制是否正确的密码
private boolean t2=false;//控制是否进入取钱
private boolean t3=false;//控制是否进入存钱
private boolean t4=false;
private boolean t5=false;
private int x=0;//当前帐户的下标;
private String tempmoney=null;//当前帐号现有多少钱String
private String temppassword=null;//当前帐号的密码
private String tempid=null;//当前帐户的ID
public void run() {
try {
this.MyRun();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void MyRun() throws IOException{
System.out.println("已经创建线程"+Thread.currentThread().getName()+
"客户机IP:"+s.getInetAddress()+
"端口号:"+s.getPort() );
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream());
String line=null;
String line2=null;
do{
line=br.readLine();
String rs=process(line);
line2=rs;
pw.println(line2);
pw.flush();
}while(!"exit".equals(line2));
t=false; t1=false; t2=false; t3=false; t4=false;t5=false;
System.out.println("线程即将结束运行:"+Thread.currentThread().getName());
br.close();
pw.close();
s.close();
}
private String process(String data) throws IOException{
System.out.println(""+data);
BufferedReader br = new BufferedReader(new FileReader("c:\\123.txt"));
String line=null;
long q=Long.parseLong(data);
if(q>100000&&t4==false){
for(int i=0;i<=SYBank.list.size()-1;i++){
String t=""+SYBank.list.get(i);
String[] n=t.trim().split("\\s+");
if(n[0].equals(data)==true){
this.x=i;
this.tempid=n[0];
this.tempmoney=""+n[2];
this.temppassword=""+n[1];
this.t=true;
this.t4=true;
return "请输入密码";
}//if2
}//for
return "帐号错误,请重新输入帐号1";
}//if1
if(data.equals(this.temppassword)==false&&t==true){return "密码错误请重新输入";}
if(data.equals(this.temppassword)==true&&t==true){
t1=true;t=false;
return "1:取款 2:存款 3:查询余额 4:退出";
}
if(data.equals("1")==true&&t1==true){
t2=true;
return "请输入取款面额:50 100 300 500 1000 2000 "+" 按5返回上一轮菜单";
}
if(data.equals("2")==true&&t1==true){
t3=true;
return "请输入存款面额: 50 100 300 500 1000 2000 "+" 按5返回上一轮菜单";
}
if(data.equals("3")==true&&t1==true){
tempmoney= ""+SYBank.list.get(x);
String[] n=tempmoney.trim().split("\\s+");
this.tempmoney=""+n[2];
return "您帐户的余额为:"+tempmoney+" 按5返回上一轮菜单";
}
if(data.equals("4")&&t1==true){
t5=true;
return "exit";
}
if(data.equals("5")==true&&t1==true){
return "1:取款 2:存款 3:查询余额 4:退出";
}
if(data.equals("50")|data.equals("100")|data.equals("300")|data.equals("500")|data.equals("1000")|data.equals("2000")&&t1==true&&t2==true){
tempmoney= ""+SYBank.list.get(x);
String[] n=tempmoney.trim().split("\\s+");
this.tempmoney=""+n[2];
long m1=Long.parseLong(tempmoney);
if(m1<q){
t2=false;
return "对不起,您的余额不足 按5返回上一轮菜单";
}
int t=Integer.parseInt(tempmoney);
int d=Integer.parseInt(data);
int k=t-d;
SYBank.list.set(x, new ZhangHu(""+this.tempid,""+this.temppassword,""+k));
PrintWriter pw=new PrintWriter("c:\\123.txt");
pw.print(SYBank.list);
pw.flush();
pw.close();
String k2=this.tempid+" "+d+" "+"qu";
Socket sc=new Socket("127.0.0.1",4444);
PrintWriter pwz=new PrintWriter(sc.getOutputStream());
BufferedReader brz=new BufferedReader(
new InputStreamReader(sc.getInputStream()));
String line2=null;
do{
pwz.println(k2);
pwz.flush();
line2=brz.readLine();
System.out.println("总行操作:"+line2);
}while(!(line2.equals("操作成功")==true|line2.equals("操作失败")==true));
pwz.close();
brz.close();
sc.close();
t2=false;
return "取款成功"+" 按5返回上一轮菜单";
}//取钱
if(data.equals("50")|data.equals("100")|data.equals("300")|data.equals("500")|data.equals("1000")|data.equals("2000")&&t1==true&&t3==true){
long t=Long.parseLong(tempmoney);
long d=Long.parseLong(data);
long k=t+d;
SYBank.list.set(x, new ZhangHu(""+this.tempid,""+this.temppassword,""+k));
PrintWriter pw=new PrintWriter("c:\\123.txt");
pw.print(SYBank.list);
pw.flush();
pw.close();
String k2=this.tempid+" "+d+" "+"cun";
Socket sc=new Socket("127.0.0.1",4444);
PrintWriter pwz=new PrintWriter(sc.getOutputStream());
BufferedReader brz=new BufferedReader(
new InputStreamReader(sc.getInputStream()));
String line2=null;
do{
pwz.println(k2);
pwz.flush();
line2=brz.readLine();
System.out.println("总行操作:"+line2);
}while(!(line2.equals("操作成功")==true|line2.equals("操作失败")==true));
pwz.close();
brz.close();
sc.close();
t3=false;
return "存款成功"+" 按5返回上一轮菜单";
}//存钱
return "错误的指令,请按规则输入";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -