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

📄 automatserver.java

📁 程序设计模式java入门源码大全
💻 JAVA
字号:
import java.io.*;
import java.net.*;

public class AutomatServer implements Runnable, AutomatInterface
{
  State waitingState;
  State gotApplicationState;
  State apartmentRentedState;
  State fullyRentedState;
  State state;
  int count;
  private Thread thread;
  ServerSocket socket;
  PrintWriter out;
  Socket communicationSocket;

  public static void main(String args[])
  {
    AutomatServer d = new AutomatServer();
  }

  public AutomatServer()
  {
    count = 9;
    waitingState = new WaitingState(this);
    gotApplicationState = new GotApplicationState(this);
    apartmentRentedState = new ApartmentRentedState(this);
    waitingState = new WaitingState(this);
    state = waitingState;

    try {    
      socket = new ServerSocket(8765);

      communicationSocket = socket.accept();

      out = new PrintWriter (communicationSocket.getOutputStream(), true);

      thread = new Thread(this);
      thread.start();

    }
    catch (Exception e) 
    {
      System.err.println(e.getMessage());
    } 
  }
  
  public void gotApplication()
  {
    out.println(state.gotApplication());
  }

  public void checkApplication()
  {
    out.println(state.checkApplication());
  }

  public void rentApartment()
  {
    out.println(state.rentApartment());
    out.println(state.dispenseKeys());
  }

  public State getWaitingState()
  {
    return waitingState;
  }

  public State getGotApplicationState()
  {
    return gotApplicationState;
  }

  public State getApartmentRentedState()
  {
    return apartmentRentedState;
  }

  public State getFullyRentedState()
  {
    return fullyRentedState;
  }

  public int getCount()
  {
    return count;
  }

  public void setCount(int n)
  {
    count = n;
  }

  public void setState(State s)
  {
    state = s;
  }

  public void run() 
  {
    String incomingString;
    try {    
       
      BufferedReader in = new BufferedReader (new 
      InputStreamReader(communicationSocket.getInputStream()));
      while((incomingString = in.readLine()) != null){
        if (incomingString.equals("gotApplication")){
          gotApplication();
        } else if (incomingString.equals("checkApplication")) {
          checkApplication();
        } else if (incomingString.equals("rentApartment")) {
          rentApartment();
        }
      }
    }catch (Exception e) 
    {
      System.err.println(e.getMessage());
    } 
  }
}

⌨️ 快捷键说明

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