?? busstation.java
字號:
package Emluator;
import java.util.*;
/**
* <p>Title:BusStation </p>
* <p>Description:車站 ,可以用來實例化寶雞和西安站對象</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: 西安電子科技大學計算機學院研03軟件與理論</p>
* @author 任聲駿
* @version 1.0
*/
public class BusStation {
Vector ivecoQueue;//站內Iveco隊列
Vector volvoQueue;//站內Volve隊列
private Vector passengerQueue;//站內乘客隊列
private String stationName;//站名
static int ivecoAccount=0;//總的依維柯的數量
static int volvoAccount=0;//總的沃爾沃的數量
int ivIntervalTime=20;//依維柯的發車間隔
int voIntervalTime=60;//沃爾沃的發車間隔
int ivBeginTime=30;//首輛依維柯的發車時間
int voBeginTime=60;//首輛沃爾沃的發車時間
public BusStation(int volvoNum,int ivecoNum,String name){
stationName=name;
ivecoQueue=new Vector();
volvoQueue=new Vector();
passengerQueue=new Vector();
int direction=1;//設置方向,以供出創建車輛時使用
if(name=="西安") direction=1;
if(name=="寶雞") direction=0;
int volvotime=60;//volve的發車時間
int ivecotime=30;//Iveco的發車時間
//創建ivecoNum輛依維柯,并添加到依維柯隊列(ivecoQueue)中
for(int i=0;i<ivecoNum;i++){
Iveco iv=new Iveco("Ive"+ivecoAccount,direction,name,ivecotime);
ivecoQueue.addElement(iv);
ivecotime+=ivIntervalTime;
ivecoAccount++;
}
//創建volvoNum輛依維柯,并添加到沃爾沃隊列(volvoQueue)中
for(int j=0;j<volvoNum;j++){
Volvo vo=new Volvo("Vol"+volvoAccount,direction,name,volvotime);
volvoQueue.addElement(vo);
volvotime+=voIntervalTime;
volvoAccount++;
}
}
//返回站內乘客數目
public int passNum(){
return passengerQueue.size();
}
//每分鐘產生上限為max的乘客,隨機產生乘客的目的地。
public void producePass(int max,int seed){
Random passRand=new Random(seed);
int passAmount=passRand.nextInt(max+1);//獲得本次須產生的乘客數
for(int i=0;i<passAmount;i++){
String passDes="";
Random desRand=new Random();
int des=desRand.nextInt(6)+1;//從隨機數中獲得乘客的目的地
//若起點站是西安站,設置乘客的目的地
if(stationName=="西安"){
switch(des){
case 1:
passDes="咸陽";
break;
case 2:
passDes="興平";
break;
case 3:
passDes="武功";
break;
case 4:
passDes="蔡家坡";
break;
case 5:
passDes="虢鎮";
break;
case 6:
passDes="寶雞";
break;
default:
break;
}
}
//若起點站是寶雞站,設置乘客的目的地
if(this.stationName=="寶雞"){
switch(des){
case 1:
passDes="虢鎮";
break;
case 2:
passDes="蔡家坡";
break;
case 3:
passDes="武功";
break;
case 4:
passDes="興平";
break;
case 5:
passDes="咸陽";
break;
case 6:
passDes="西安";
break;
default:
break;
}
}
Passenger pass=new Passenger(stationName+i,passDes,stationName);//創建乘客
passengerQueue.addElement(pass);//向乘客隊列中添加乘客
}
}
//檢查Iveco隊列中是否有車到發車時間
public boolean isIvecoOut(int curtime){
boolean test=false;
if(ivecoQueue.isEmpty()) test=false;//若Iveco對列為空,則無車輛可發
//若隊列的頭元素,即對列中第一輛車已到發車時間發車時間,返回真
else if(((Vehicle)ivecoQueue.get(0)).isTakeoffTime(curtime)){
test=true;
}
return test;
}
//檢查Volvo隊列中是否有車到發車時間
public boolean isVolvoOut(int curtime){
boolean test=false;
if(volvoQueue.isEmpty()) test=false;//若Volvo對列為空,則無車輛可發
//若隊列的頭元素,即對列中第一輛車已到發車時間發車時間,返回真
else if(((Vehicle)volvoQueue.get(0)).isTakeoffTime(curtime)){
test=true;
}
return test;
}
//車站Iveco隊列出車
public Iveco ivecoOut(){
Iveco car=(Iveco)ivecoQueue.remove(0);//取出隊頭元素,并在隊列中刪除,表示發車
car.pickup(passengerQueue);//上乘客
car.setMovingstate(true);//設置車輛行駛狀態為true(moving)
return car;
}
//車站Volvo隊列出車(同上)
public Volvo volvoOut(){
Volvo car=(Volvo)volvoQueue.remove(0);
car.pickup(passengerQueue);
car.setMovingstate(true);
return car;
}
//車隊進站的處理
public void appendCar(Vehicle car,int curTime){
int ivecoEndTime=630;//Iveco的末班車時間
int volvoEndTime=600;//Volvo的末班車時間
//設置車輛的下一次的行駛方向
if(stationName=="西安") car.setDirection(1);
if(stationName=="寶雞") car.setDirection(0);
car.setMovingstate(false);//將車輛行駛狀態改為停
//Iveco的進站
if(car.getClass().getName().equals("Emluator.Iveco")){
if(curTime>ivecoEndTime){
car.setTakeoffTime(-1);//若已過末班車時間,設發車時間為-1
}else{
if(ivecoQueue.isEmpty()) car.setTakeoffTime(curTime+20);//若車隊為空,設發車時間為當前時間+20
else
car.setTakeoffTime(((Vehicle) ivecoQueue.lastElement()).getTakeoffTime()+20);//若車隊不控,發車時間為隊尾發車時間+20
}
ivecoQueue.addElement(car);//在依維柯隊列ivecoQueue中添加進站車輛
}
//Volvo的進站
if(car.getClass().getName().equals("Emluator.Volvo")){
if(curTime>volvoEndTime){
car.setTakeoffTime(-1);//若已過末班車時間,設發車時間為-1
}else{
if(volvoQueue.isEmpty()) car.setTakeoffTime(curTime+60);//若車隊為空,設發車時間為當前時間+60
else
car.setTakeoffTime(((Vehicle) volvoQueue.lastElement()).getTakeoffTime()+60);//若車隊不控,發車時間為隊尾發車時間+60
}
volvoQueue.addElement(car);//在沃爾沃隊列volvoQueue中添加進站車輛
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -