package main;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import lifter.Lifter;
import lifter.Request;
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame implements ActionListener {//function: main,entry of the program
private static Container[] container;//容器用于建立图形时用
private static int row;//行,大楼的高度。
private static int column;//电梯的数目。
public static Request receiverequest;//外部发出的请求。
public static Lifter[] lifters;//生成的电梯,此时没有初始化。
public static Lifter choosedLifter = null;//选中的电梯,调度的时候用。
public class Request {
public List<Integer> requestedFloor= (List)new java.util.ArrayList<Integer>(); ;
public int floorSentRequest;
public int upordown;
}
public static void main(String[] args) {//入口函数。
// Promt the user to enter the number of the lift
String rowString = JOptionPane.showInputDialog(null, "请输入楼层数", "输入",
JOptionPane.QUESTION_MESSAGE);
// Promt the user to enter the number of the elevator
String columnString = JOptionPane.showInputDialog(null, "请输入电梯数", "输入",
JOptionPane.QUESTION_MESSAGE);
try {
row = Integer.parseInt(rowString);//从对话框中获得大楼的高度和电梯的数目。
column = Integer.parseInt(columnString);
container = new Container[column + 2];//用于界面的布局。
for (int iii = 0; iii <column + 2; iii++) {
container[iii] = new Container();
}
Main m = new Main();//设置界面。
m.setTitle("欢迎使用电梯系统");
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setVisible(true);
container[0] = m.getContentPane();
container[0].setLayout(new GridLayout(1, column + 1, 5, 5));
container[1].setLayout(new GridLayout(row, 3, 5, 5));
container[0].add(container[1]);
for(int ii = 2; ii < column + 2; ii ++){
container[ii].setLayout(new GridLayout(1, row, 5, 5));
container[0].add(container[ii]);
}
/*container[2].setLayout(new GridLayout(row, 2, 5, 5));
container[3].setLayout(new GridLayout(1, column, 5, 5));
container[0].add(container[1]);
container[0].add(container[2]);
container[0].add(container[3]);*/
// Set GirdLayout,gaps 5 between compontent horizontally and vertically
// Add buttons to frame
for (int i = row; i >= 1; i--) {//增加外部请求的按钮。
JLabel b = new JLabel(" " + i);
container[1].add(b);
//JButton a = new JButton("up " + i);
//JButton a = new JButton("∧");
JButton a = new JButton("up " + i);
a.addActionListener(m);//每个按钮的listener为m
container[1].add(a);
//JButton aa = new JButton("down " + i);//增加外部请求的按钮。
//JButton aa = new JButton("∨");
JButton aa = new JButton("down " + i);
aa.addActionListener(m);
container[1].add(aa);
}
lifters = new Lifter[column];//电梯的初始化。电梯的绘制也在这里进行。
for (int ii = 0; ii < column; ii++) {
lifters[ii] = new Lifter(row, 0, 0, container[ii + 2]);
}
for (int i = 0; i < lifters.length; i++) {
new Thread(lifters[i]).start();//让新绘制的电梯启动
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void actionPerformed(ActionEvent e) {//外部请求得主响应函数。
String estr = e.getActionCommand();
if (estr.charAt(0) == 'u' ||estr.charAt(0) == 'd') {
//if (estr.equals("up") == true ||estr.equals("down") == true) {
receiverequest = new Request();
receiverequest.floorSentRequest = Integer.parseInt(estr
.substring(estr.indexOf(" ") + 1));
if (estr.charAt(0) == 'u')
//if (estr.equals("up") == true)
receiverequest.upordown = 1;
if (estr.charAt(0) == 'd')
//if (estr.equals("down") == true)
receiverequest.upordown = -1;
choosedLifter = chooseLifter(lifters, receiverequest);// 从电梯中选者一部电梯,如果没有符合条件的电梯就返回空.
if (choosedLifter != null) { // 如果返回为空,即当前没有电梯符合条件,当前请求就被忽视
choosedLifter.printDest();//输出destFloors的状态。
if ((receiverequest.upordown == 1&&choosedLifter.currentFloor<receiverequest.floorSentRequest)||(receiverequest.upordown==-1&&choosedLifter.currentFloor<receiverequest.floorSentRequest)) {
choosedLifter.addDestUp(new Integer(
receiverequest.floorSentRequest));// 往选中的电梯目标队列当中添加receiverequest.floorsentrequest
choosedLifter.moveState=1;
}else if((receiverequest.upordown==-1&&choosedLifter.currentFloor>receiverequest.floorSentRequest)||(receiverequest.upordown == 1&&choosedLifter.currentFloor>receiverequest.floorSentRequest)){
choosedLifter.addDestDown(new Integer(receiverequest.floorSentRequest));
choosedLifter.moveState=-1;
}
choosedLifter.printDest();
if (choosedLifter.flag == 1) { // 如果当前电梯可以开启的话就开启,不能开启意味着此电梯已经开启。对已经开启的电梯只需要让他跑完就可以了。
// flag为0的情况是:电梯已经开启,下一个请求还是选中了这部电梯,
new Thread(choosedLifter).start();
choosedLifter.flag = 0; // 当电梯队列里的成员都被跑完后,flag会改为1
}
}
// System.out.println("5");
receiverequest = null;
}
}
public static synchronized Lifter chooseLifter(Lifter[] l,
Request receiverequest) {// 选者电梯
Lifter choosedLifter = null;
int temp, temp2;
if (receiverequest != null) {
if (receiverequest.upordown == 1) {//向上的话就从该楼层以下的电梯里选者同向的电梯。
try {
temp = Math.abs(receiverequest.floorSentRequest
- l[0].currentFloor);
for (int i = 0; i < l.length; i++) {
if (l[i].currentFloor <= receiverequest.floorSentRequest
&& l[i].moveState == receiverequest.upordown) {
if (temp >= Math
.abs(receiverequest.floorSentRequest
- l[i].currentFloor)) {
choosedLifter = l[i];
temp = Math.abs(receiverequest.floorSentRequest
- l[i].currentFloor);
}
}
}
} catch (Exception e) {
} finally {
if (choosedLifter == null) {//如果没有找到合适的就从静止的电梯中找离它最近的,再没有找到的话就忽视该请求
temp2 = Math.abs(receiverequest.floorSentRequest
- l[0].currentFloor);
for (int j = 0; j < l.length; j++) {
if (l[j].moveState == 0) {
if (temp2 >= Math
.abs(receiverequest.floorSentRequest
- l[j].currentFloor)) {
choosedLifter = l[j];
temp2 = Math
.abs(receiverequest.floorSentRequest
- l[j].currentFloor);
}
}
}
}
}
}
if (receiverequest.upordown == -1) {//向下的话就从该楼层以上的电梯里选者同向的电梯。
try {
temp = Math.abs(l[0].currentFloor
- receiverequest.floorSentRequest);
for (int i = 0; i < l.length; i++) {
if (l[i].currentFloor >= receiverequest.floorSentRequest
&& l[i].moveState == receiverequest.upordown) {
if (temp >= l[i].currentFloor
- receiverequest.floorSentRequest) {
choosedLifter = l[i];
temp = l[i].currentFloor
- receiverequest.floorSentRequest;
}
}
}
} catch (Exception e) {
} finally {
if (choosedLifter == null) {//如果没有找到合适的就从静止的电梯中找离它最近的。
temp2 = Math.abs(receiverequest.floorSentRequest
- l[0].currentFloor);
for (int j = 0; j < l.length; j++) {
if (l[j].moveState == 0) {
if (temp2 >= receiverequest.floorSentRequest
- l[j].currentFloor) {
choosedLifter = l[j];
temp2 = receiverequest.floorSentRequest
- l[j].currentFloor;
}
}
}
}
}
}
}
return choosedLifter;//返