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

📄 busstation.java

📁 高速公路客运仿真
💻 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 + -