日期:2014-05-20 浏览次数:20839 次
//保存为TestBox.java //测试类 public class TestBox{ public static void main(String[] args){ Box box = new Box(); box.b1(); //打开电源 box.b2();//按b2 (对应L1) box.b3(); //按b3 (对应L2) box.b3(); //按b3 (对应L2) box.showL1L2Status(); box.brokenLight1(); //砸烂L1 box.showL1L2Status();//显示2个灯的状态 box.b4(); //关灯 box.showL1L2Status(); //显示2个灯的状态 box.b1(); //再开灯 box.showL1L2Status(); //显示2个灯的状态 box.repairLight(); //关灯,修理灯 box.showL1L2Status(); //显示2个灯的状态 box.b1(); //修理后重新打开电源 box.b2(); box.showL1L2Status(); } } //盒子类 class Box { private boolean power = false;//电源 private Light l1 = new Light();//电灯一 private Light l2 = new Light();//电灯二 public void b1(){ //开打电源 power = true; l1.setCount(0); //每次重开电源,重新计算L1,L2被按的次数 l2.setCount(0); } public void b2(){ //按b2按钮,对应L1 if( !power){ //如果电源没开,不响应, return; } else{ this.l1.AddCount(); //System.out.println("l2:" + l1.getCount()); if( !checkBroken()){ //只有灯没坏时才响应 int light1Count = l1.getCount(); int light2Count = l2.getCount(); if( light1Count > light2Count){ l1.setStatus("light"); l2.setStatus("unlight"); } else {if ( light2Count > light1Count){ l2.setStatus("light"); l2.setStatus("unlight"); } else { l1.setStatus("unlight"); l2.setStatus("unlight"); }} }//if(!checkBroken) else{ //灯坏了,就不响应 if( l1.getStatus().equals("broken")){ l2.setStatus("glimpse"); } else{ l1.setStatus("glimpse"); } }// checkBroken else } } public void b3(){ //参照b2的注释 if( !power){ return; } else{ this.l2.AddCount(); //System.out.println("l2:" + l2.getCount()); if( !checkBroken()){ int light1Count = l1.getCount(); int light2Count = l2.getCount(); if( light1Count > light2Count){ l1.setStatus("light"); l2.setStatus("unlight"); } else{ if ( light2Count > light1Count){ l2.setStatus("light"); l1.setStatus("unlight"); } else { l1.setStatus("unlight"); l2.setStatus("unlight"); } } }//if(!checkBroken) else{ if( l1.getStatus().equals("broken")){ l2.setStatus("glimpse"); } else{ l1.setStatus("glimpse"); } }// checkBroken else } } public void b4(){ //关电源 power = false; //没坏的话,就设置灯为关闭状态,否则保持原来的状态 if ( (!this.l1.getStatus().equals("broken")) &&(!this.l2.getStatus().equals("broken"))){ l1.setStatus("unlight"); l2.setStatus("unlight"); } } //关灯,并将坏的灯泡休息好 public void repairLight(){ System.out.println("操作:关电灯,修理"); b4(); l1.setStatus("unlight"); l2.setStatus("unlight"); } //将L1砸烂 public void brokenLight1(){ l1.setStatus("broken"); l2.setStatus("glimpse"); } //将L2砸烂 public void brokenLight2(){ l2.setStatus("broken"); l1.setStatus("glimpse"); } //显示目前灯的状态 public void showL1L2Status(){ if(power){ System.out.println("L1的状态:" + l1.getStatus()); System.out.println("L2的状态:" + l2.getStatus()); } else{ System.out.println("L1的状态:unlight"); System.out.println("L2的状态:unlight"); } } //检查是否有灯坏了,有的话返回true,否则返回false public boolean checkBroken(){ boolean flag = false; if( l1.getStatus().equals("broken") || l2.getStatus().equals("broken")){ flag = true; } return flag; } } //灯泡类 class Light{ private String status = "unlight";//状态,从unlight light glimpse broken中选一个 private int count = 0; //被按下按钮的次数+1 public void AddCount(){ this.count++; } //取得被按下的次数 public int getCount(){ return this.count; } //设置被按下的次数 public void setCount(int count){ this.count = count; } //取得灯泡的状态 public String getStatus(){ return this.status; } //设置灯泡的状态 public void setStatus(String status){ this.status = status; } }