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

📄 business.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

import java.awt.*;
import java.awt.event.*;

public abstract class Business
    extends Panel
    implements Runnable
{
  protected Agent[] agent = new Agent[Simulator.NUM_AGENTS];
  protected Label[] labelAgent = new Label[Simulator.NUM_AGENTS];
  protected Label labelQueue = new Label("正在等待的顾客: 0");
  protected Label labelServed = new Label("服务的顾客: 0");
  protected Label labelWait = new Label("平均等待: 0");
  protected int numAgents = Simulator.NUM_INITIAL_AGENTS;
  protected int numCustomer = 0;
  protected long totalWait = 0L;

  private Thread thread = null;
  private boolean doorIsOpen = false;

  public Business(String title)
  {
    super();
    setup(title);
  }

  public abstract void generateCustomer();

  public abstract Customer requestCustomerFor(int ID);

  public abstract void updateDisplay();

  public void checkoutCustomer(int handled, long waitTime)
  {
    numCustomer++;
    totalWait += waitTime;
  }

  public void addAgent()
  {
    if (numAgents < Simulator.NUM_AGENTS)
    {
      agent[numAgents] = new Agent(this, numAgents);
      agent[numAgents].start();
      numAgents++;
    }
  }

  public void retireAgent()
  {
    if (numAgents > 1)
    {
      agent[numAgents - 1].halt();
      numAgents--;
    }
  }

  public void start()
  {
    if (thread == null)
    {
      thread = new Thread(this);
      doorIsOpen = true;
      thread.start();
      for (int i = 0; i < numAgents; i++)
      {
        agent[i].start();
      }
    }
  }

  public void stop()
  {
    doorIsOpen = false;
    thread = null;
    for (int i = 0; i < numAgents; i++)
    {
      agent[i].halt();
    }
  }

  public void openDoor()
  {
    doorIsOpen = true;
  }

  public void closeDoor()
  {
    doorIsOpen = false;
  }

  public void run()
  {
    while (thread == Thread.currentThread())
    {
      try
      {
        thread.sleep( (int) (Math.random() * Simulator.MAX_NO_CUSTOMERS));
        if (doorIsOpen)
        {
          generateCustomer();
        }
        updateDisplay();
      }
      catch (InterruptedException ie)
      {
        System.out.println("Business exception: " + ie);
      }
    }
  }

  private void setup(String title)
  {
    Panel agentPanel = new Panel();
    agentPanel.setLayout(new GridLayout(Simulator.NUM_AGENTS + 3, 1));
    for (int i = 0; i < Simulator.NUM_AGENTS; i++)
    {
      labelAgent[i] = new Label("Agent " + i + ": served 0");
      agentPanel.add(labelAgent[i]);
      agent[i] = new Agent(this, i);
    }
    for (int i = numAgents; i < Simulator.NUM_AGENTS; i++)
    {
      labelAgent[i].setText("Agent " + i + ": inactive");
    }
    agentPanel.add(labelQueue);
    agentPanel.add(labelServed);
    agentPanel.add(labelWait);
    setLayout(new BorderLayout());
    add("Center", agentPanel);
    add("North", new Label(title));
  }
}

⌨️ 快捷键说明

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