?? main.java
字號:
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;//返回選中的電梯。
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -