黑马程序员_面试题(一)交通灯管理系统
android培訓(xùn)、java培訓(xùn)、期待與您交流!
【Road類】:
每個(gè)Road對象都有一個(gè)name成員變量來代表方向,有一個(gè)vehicles成員變量來代表方向上的車輛集合。
在Road對象的構(gòu)造方法中啟動(dòng)一個(gè)線程每隔一個(gè)隨機(jī)的時(shí)間向vehicles集合中增加一輛車(用一個(gè)“路線名_id”形式的字符串進(jìn)行表示)。
在Road對象的構(gòu)造方法中啟動(dòng)一個(gè)定時(shí)器,每隔一秒檢查該方向上的燈是否為綠,是則打印車輛集合和將集合中的第一輛車移除掉。
?
public class Road {
????? privateArrayList<String> vehicles = new ArrayList<String>();
????? private String name;
????
????? public Road(Stringname){
?????????? this.name = name;
?????????? ExecutorServicepool = Executors.newSingleThreadExecutor();
?????????? pool.execute(newRunnable(){
???????????????? public voidrun(){
????????????????????? for(inti=1;i<1000;i++){
???????????????????????????try {
?????????????????????????????????Thread.sleep((new Random().nextInt(10) + 1) * 1000);
??????????????????????????? }catch (InterruptedException e) {
?????????????????????????????????e.printStackTrace();
???????? ???????????????????}
???????????????????????????vehicles.add(Road.this.name + "_" + i);
????????????????????? }???????????????????
???????????????? }????????
?????????? });
?????????
??????????ScheduledExecutorService timer =?Executors.newScheduledThreadPool(1);
??????????timer.scheduleAtFixedRate(
????????????????????? newRunnable(){
???????????????????????????public void run(){
?????????????????????????????????if(vehicles.size()>0){
?????????????????????????????????????? booleanstatus = Lamp.valueOf(Road.this.name).getStatus();
??????????????????????????????????????if(status){
????????????????????????????????????????????System.out.println(vehicles.remove(0) + " is traversing !");
?????????????????????????????????????? }
?????????????????????? ???????????}
????????????????????????????????
??????????????????????????? }
????????????????????? },
????????????????????? 1,
????????????????????? 1,
?????????????????????TimeUnit.SECONDS);
????? }
}
?
【Lamp類】:
系統(tǒng)中有12個(gè)方向上的燈,在程序的其他地方要根據(jù)燈的名稱就可以獲得對應(yīng)的燈的實(shí)例對象,綜合這些因素,將Lamp類用java5中的枚舉形式定義更為簡單。
每個(gè)Lamp對象中的亮黑狀態(tài)用lighted變量表示,選用S2N、S2W、E2W、E2N這四個(gè)方向上的Lamp對象依次輪詢變亮,Lamp對象中還要有一個(gè)oppositeLampName變量來表示它們相反方向的燈,再用一個(gè)nextLampName變量來表示此燈變亮后的下一個(gè)變亮的燈。這三個(gè)變量用構(gòu)造方法的形式進(jìn)行賦值,因?yàn)槊杜e元素必須在定義之后引用,所以無法再構(gòu)造方法中彼此相互引用,所以,相反方向和下一個(gè)方向的燈用字符串形式表示。?
增加讓Lamp變亮和變黑的方法:light和blackOut,對于S2N、S2W、E2W、E2N這四個(gè)方向上的Lamp對象,這兩個(gè)方法內(nèi)部要讓相反方向的燈隨之變亮和變黑,blackOut方法還要讓下一個(gè)燈變亮。
除了S2N、S2W、E2W、E2N這四個(gè)方向上的Lamp對象之外,其他方向上的Lamp對象的nextLampName和oppositeLampName屬性設(shè)置為null即可,并且S2N、S2W、E2W、E2N這四個(gè)方向上的Lamp對象的nextLampName和oppositeLampName屬性必須設(shè)置為null,以便防止light和blackOut進(jìn)入死循環(huán)。
public enum Lamp {
?????S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
?????N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),????
?????S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
????
????? private Lamp(Stringcomposite,String nextLamp,boolean status){
?????????? this.opposite =composite;
?????????? this.nextLamp =nextLamp;
?????????? this.status =status;
????? }
????? public booleangetStatus(){
?????????? return status;
????? }
????? public voidsetStatus(boolean status){
?????????? this.status =status;
????? }
????? public LampgetNextLamp(){
???? ??????return Lamp.valueOf(this.nextLamp);
????? }
????
????? public voidturnStatus(){
?????????? if (getStatus()) {
????????????????setStatus(false);
????????????????System.out.println(this.name()+"turns from green to red");
???????????????? if (opposite!= null) {//一定要判斷,否則進(jìn)入無限遞歸
?????????????????????Lamp.valueOf(opposite).turnStatus();
???????????????? }
???????????????? if(nextLamp!= null){ //一定要判斷,否則進(jìn)入無限遞歸
?????????????????????Lamp.valueOf(nextLamp).turnStatus();
???????????????? }
????????????? ?????????????
?????????? }else{
????????????????setStatus(true);
????????????????System.out.println(this.name()+"turns from red to green");
???????????????? if (opposite!= null) {//一定要判斷,否則進(jìn)入無限遞歸
?????????????????????Lamp.valueOf(opposite).turnStatus();???????
???????????????? }
?????????? }
????? }
?
????? //注釋掉的部分是張老師的原來代碼,我講light和blackOut方法整合成了turnStatus函數(shù)
???? / *public void light(){
???????? this.status = true;
???????? if(opposite != null){
?????????????Lamp.valueOf(opposite).light();
???????? }
????????System.out.println(name() + " lamp is green");
???????
???? }
???
?
???? public Lamp blackOut(){
???????? this.status = false;
???????? if(opposite != null){
?????????????Lamp.valueOf(opposite).blackOut();
???????? }??????
???????
???????? Lamp tempnextLamp=null;
???????? if(nextLamp != null){
????????????? tempnextLamp =Lamp.valueOf(nextLamp);
?????????????System.out.println("" + name() + "-------->" +nextLamp);???????????
?????????????tempnextLamp.light();
???????? }
???????? return tempnextLamp;
???? }* /
????
????? private String opposite=null;
????? private String nextLamp= null;
????? private boolean status =false;
}
?
【LampController類】:
整個(gè)系統(tǒng)中只能有一套交通燈控制系統(tǒng),所以,LampController類最好是設(shè)計(jì)成單例。
LampController構(gòu)造方法中要設(shè)定第一個(gè)為綠的燈。
LampController對象的start方法中將當(dāng)前燈變綠,然后啟動(dòng)一個(gè)定時(shí)器,每隔10秒將當(dāng)前燈變紅和將下一個(gè)燈變綠。
public class LampController {
????? private LampcurrentLamp;
????
????? public LampController(){
?????????? currentLamp =Lamp.S2N;
??????????currentLamp.turnStatus();
??????????//currentLamp.light();
?????????
??????????ScheduledExecutorService timer =?Executors.newScheduledThreadPool(1);
??????????timer.scheduleAtFixedRate(
????????????????????? newRunnable(){
???????????????????????????public? void run(){
????????????????????????????????currentLamp.turnStatus();
?????????????????????????????????currentLamp = currentLamp.getNextLamp();
?????????????????????????????????//currentLamp = currentLamp.blackOut();
????????????????????? }
????????????????????? },
????????????????????? 10,
?????????? ???????????10,
?????????????????????TimeUnit.SECONDS);
????? }
}
?
【MainClass類】:
l
用for循環(huán)創(chuàng)建出代表12條路線的對象。
l
接著再獲得LampController對象并調(diào)用其start方法。
public class MainClass {
?
????? public static voidmain(String[] args) {
?????????? String[] directions= new String[]{
?????????????????????"S2N","S2W","E2W","E2S",
?????????????????????"N2S","N2E","W2E","W2N",??
?????????????????????"S2E","E2N","N2W","W2S"
?????????? };
?????????? for (String str:directions) {
???????????????? newRoad(str);
?????????? }
??? ????????new LampController();
????? }
}
總結(jié)
以上是生活随笔為你收集整理的黑马程序员_面试题(一)交通灯管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS 多线程安全数组
- 下一篇: 全网最全 数据结构 代码