java面试题之一 交通灯管理系统



代码模块

1路

       目的:

1、用集合模拟车辆,集合中元素的增加和减少模拟车辆的增加和减少

              2、随机往路末尾加入车辆

       代码:

       public class Road{

              List<String> vechicles = ArrayList<E>//1,面向接口编程

      

              //指定路线的名字

              private String name = null ;

              public Road(String name){

       this.name = name ;

       //随机加入车辆

ExcecutorService pool = Executors.newSingleThreadExecutor() ;  //2线程池

//利用线程池随机添加车辆

pool.execute(new Runnable(){

       public run(){

for(int i = 1 ; i< 1000 ,I ++){

              try{

Thread.sleep(

(new Random().nextInt(10)+1)*1000) ;//3随机值

}catch(InterruptedException e){

      

}

vechicles.add(this.name + “” + i)

//4内部类访问外部类的成员变量1成员变量被final修饰2Road.this.name

             

}

}

}) ;

                            //定时移开车辆

//5定时器,调度池

ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);

       timer.scheduleAtFixedRate(

new Runnable(){

       public void run(){

       if(vechicles.size() > 0){

       boolean lighted = true ;

if(lighted){

       //移开车辆

       vechicles.remove(0) ;

}

}

}

},

              1,

              1,

              TimeUnit.SECONDS

) ;

}

}

      

2

       目的:

1、12个灯,使用枚举,只有4个灯有逻辑

              2、个成员变量下一个灯 对应方向的灯 自己的状态

       代码:

       public enum Lamp{

       S2N,S2W,E2W,E2S,

       N2S,N2E,W2E,W2N,

      

       S2N,E2W,N2W,W2S ;//1分号

       //对应的灯

       private Lamp opposite ;

       //灯的状态

       private boolean liglted ;

       public boolean isLighted(){

              return lighted ;

}

//灯变绿变红的方法

public void liglt(){

       this.lighted = true ;

       if(opposite != null)

              opposite.light() ;

}

public void blackOut(){

       this.lighted = false ;

       if(opposite != null){

       Lamp.valueOf(opposite).blackOut();

}

Lamp nextLamp = null ;

if(next != null){

nextLamp = Lamp.valueOf(next) ;

nextLamp.light() ;

       }

       return nextLamp ;

}

}

3灯的控制器

       当前的绿灯变红

       定时器

当灯变红,下一个灯返回,便于下一次变绿

public class LampContro{

       //当前的灯

       private Lamp currentLamp;

       public LampContro(){

              //指定当前的灯

       currentLamp = Lamp.S2N;

       //让当前的灯是绿的

       currentLamp.light() ;

       //每隔10S让当前灯变红,让下一个灯变绿

       ScheduledExecutorService timer = Excutors.newScheduledThreadPool(1)

       timer.scheduleAtFixedRate(

              new Runnable(){

public void run(){

       currentLamp = currentLamp.blackOut() ;

}

},

                                   10,

                                   10,

                                   TimeUnit.SECONDS;

);

}

}

4主程序

       public class MainClass{

              public static void main(){

                     //1字符数组,循环遍历

String[] directions = new String[]{

                            S2N,S2W,E2W,E2S,

                            N2S,N2E,W2E,W2N,

       S2E,E2N,N2W,W2S ;

};

for(int i = 0 ; i<directions.length ; i++){

       new Road(directions[i]) ;

}

new LampContro();

}

}



代码模块

1路

       目的:

1、用集合模拟车辆,集合中元素的增加和减少模拟车辆的增加和减少

              2、随机往路末尾加入车辆

       代码:

       public class Road{

              List<String> vechicles = ArrayList<E>//1,面向接口编程

      

              //指定路线的名字

              private String name = null ;

              public Road(String name){

       this.name = name ;

       //随机加入车辆

ExcecutorService pool = Executors.newSingleThreadExecutor() ;  //2线程池

//利用线程池随机添加车辆

pool.execute(new Runnable(){

       public run(){

for(int i = 1 ; i< 1000 ,I ++){

              try{

Thread.sleep(

(new Random().nextInt(10)+1)*1000) ;//3随机值

}catch(InterruptedException e){

      

}

vechicles.add(this.name + “” + i)

//4内部类访问外部类的成员变量1成员变量被final修饰2Road.this.name

             

}

}

}) ;

                            //定时移开车辆

//5定时器,调度池

ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);

       timer.scheduleAtFixedRate(

new Runnable(){

       public void run(){

       if(vechicles.size() > 0){

       boolean lighted = true ;

if(lighted){

       //移开车辆

       vechicles.remove(0) ;

}

}

}

},

              1,

              1,

              TimeUnit.SECONDS

) ;

}

}

      

2

       目的:

1、12个灯,使用枚举,只有4个灯有逻辑

              2、个成员变量下一个灯 对应方向的灯 自己的状态

       代码:

       public enum Lamp{

       S2N,S2W,E2W,E2S,

       N2S,N2E,W2E,W2N,

      

       S2N,E2W,N2W,W2S ;//1分号

       //对应的灯

       private Lamp opposite ;

       //灯的状态

       private boolean liglted ;

       public boolean isLighted(){

              return lighted ;

}

//灯变绿变红的方法

public void liglt(){

       this.lighted = true ;

       if(opposite != null)

              opposite.light() ;

}

public void blackOut(){

       this.lighted = false ;

       if(opposite != null){

       Lamp.valueOf(opposite).blackOut();

}

Lamp nextLamp = null ;

if(next != null){

nextLamp = Lamp.valueOf(next) ;

nextLamp.light() ;

       }

       return nextLamp ;

}

}

3灯的控制器

       当前的绿灯变红

       定时器

当灯变红,下一个灯返回,便于下一次变绿

public class LampContro{

       //当前的灯

       private Lamp currentLamp;

       public LampContro(){

              //指定当前的灯

       currentLamp = Lamp.S2N;

       //让当前的灯是绿的

       currentLamp.light() ;

       //每隔10S让当前灯变红,让下一个灯变绿

       ScheduledExecutorService timer = Excutors.newScheduledThreadPool(1)

       timer.scheduleAtFixedRate(

              new Runnable(){

public void run(){

       currentLamp = currentLamp.blackOut() ;

}

},

                                   10,

                                   10,

                                   TimeUnit.SECONDS;

);

}

}

4主程序

       public class MainClass{

              public static void main(){

                     //1字符数组,循环遍历

String[] directions = new String[]{

                            S2N,S2W,E2W,E2S,

                            N2S,N2E,W2E,W2N,

       S2E,E2N,N2W,W2S ;

};

for(int i = 0 ; i<directions.length ; i++){

       new Road(directions[i]) ;

}

new LampContro();

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值