⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atmserver.java

📁 模拟银行的ATM自动取款机
💻 JAVA
字号:
import java.net.*;
import java.io.*;

public class ATMServer{
   private ServerSocket server;
   private Socket s;
   
   public static void main(String args[]){
      ATMServer server=new ATMServer();
   }
   
   public ATMServer(){
      try{
         server=new ServerSocket(5555);
         s=null;
      }catch(IOException e){}
      Listen();
   }
   
   private void Listen(){
      while(true){
         try{
            s=server.accept();
            ServerThread st=new ServerThread(s);
         
            new Thread(st).start();
         }catch(IOException e){}
      }
   }
}


class ServerThread implements Runnable{
   private Socket s;
   private DataOutputStream dout;
   private DataInputStream din;
   
   public ServerThread(Socket s){
      this.s=s;
      try{
         dout=new DataOutputStream(s.getOutputStream());
         din=new DataInputStream(s.getInputStream());
      }catch(IOException e){}
   }
   
   public void run(){

         while(true){//等待用户插卡操作
            String ID=getString();
            Account account=new Account();
            int check=account.IsAccountExsit(ID);
            if(check==1){
               sendString("true");
            }
            else{
               sendString("false");
               continue;
            }
            
            
            String password=getString();
            int i=0;
            while(i<3){
               boolean c=account.checkPassWord(password);
               if(c==true){
                  sendString("true");
                  break;
               }
               else{
                  sendString("false");
                  i++;
               }
            }
            if(i==3) break;
            
            
            while(true){//等待用户发送操作命令
               String command=getString();
               if(command=="Query"){
                  String cash=String.valueOf(account.showMoney());
                  sendString(cash);
                  command=null;
               }
               if(command=="Draw"){
                  String str=getString();
                  float draw=Float.parseFloat(str);
                  boolean ch=account.draw(draw);
                  if(ch){
                     sendString("true");
                  }
                  else{
                     sendString("false");
                  }
                  command=null;
               }
               if(command=="ChangePassWord"){
                  String newPassword=getString();
                  account.changePassWord(newPassword);
                  sendString("true");
                  command=null;
               }
               /*if(command=="MoveMoney"){
                  String nextID=getString();
                  String check=Account Target=new Account(nextID);
                  sendString(check);
                  if(check=="true"){
                     String cash=getString();
                     //转帐处理
                     sendString("true");
                     command=null;
                  }
                  else{
                     command=null;
                     continue;
                  }
               }*/
               if(command=="Quit"){//结束操作,进入下一等待状态
                  command=null;
                  break;
               }
            }
                   
                     
               
            
            
         }

   }
   
   public String getString(){ 
         String str=null;
         try{
            str=din.readUTF();
         }catch(IOException e){}    
         return str;    
   }
   
   public void sendString(String str){
      try{
         dout.writeUTF(str);
      }catch(IOException e){}
   }
}
         
      
   
   
   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -