java中的迭代器状态集

cedebl8k  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(384)

所以我写了这部分申请。但不是一直都有效。

  1. public void setStatus(int id, int area, boolean free)
  2. {
  3. Iterator<Ambulance> ab = cars.iterator();
  4. while(ab.hasNext()){
  5. Ambulance ambulance = ab.next();
  6. if(ambulance.getID() == id){
  7. ambulance.moveTo(area);
  8. ambulance.setBusy();
  9. }
  10. }
  11. }

对于这个设置状态,我有这个测试条件,但是这个部分 assertEquals(!oldStatus, selected.isFree()); 这种情况的任何一种都不起作用。我该怎么办?

  1. public void moveAmbulance()
  2. {
  3. // Select a random Ambulance to be moved.
  4. Ambulance selected = shadow.get(rand.nextInt(shadow.size()));
  5. int oldArea = selected.getArea();
  6. boolean oldStatus = selected.isFree();
  7. control.setStatus(selected.getID(), oldArea + 1, !oldStatus);
  8. assertEquals(oldArea + 1, selected.getArea());
  9. assertEquals(!oldStatus, selected.isFree());
  10. }
mpbci0fu

mpbci0fu1#

你不使用 free 中的参数 setStatus 方法

  1. public void setStatus(int id, int area, boolean free)
  2. {
  3. Iterator<Ambulance> ab = cars.iterator();
  4. while(ab.hasNext()){
  5. Ambulance ambulance = ab.next();
  6. if ((free) && (ambulance.getID() == id)) {
  7. ambulance.moveTo(area);
  8. ambulance.setBusy();
  9. }
  10. }
  11. }
wribegjk

wribegjk2#

  1. public void setStatus(int id, int area, boolean free)
  2. {
  3. Iterator<Ambulance> ab = cars.iterator();
  4. while(ab.hasNext()){
  5. Ambulance ambulance = ab.next();
  6. if(ambulance.getID() == id){
  7. ambulance.moveTo(area);
  8. }
  9. if(free){
  10. ambulance.setFree();
  11. } else{
  12. ambulance.setBusy();
  13. }
  14. }
  15. }

相关问题