電梯

2013-04-30 112 views
1

實現優先級隊列,我有以下的代碼來實現電梯:電梯

public class Elevator{ 

    Direction dir; 
    int floorNum; 
    int capacity; 

    public void moveUp{ 
     dir = Direction.Up; 
    } 

    public void moveDown{ 
    dir = Direction.Down 
    } 

    public boolean isMoving{ 
    returns dir.equals(Direction.STATIONARY); 
    } 
} 


public class ElevatorController{ 

    Elevator[] elevators; 

    PriorityQueue<Integer> queue = new PriorityQueue<Integer>; 

    public void buttonPressed{Direction d, int fromFloot, int toFloor){ 


    } 
} 

我讀了實現電梯將實現優先級隊列,以獲得電梯,但我不知道如何的好辦法。

隊列將包含目的地樓層。

你會如何建議實施的?

+0

依賴。我認爲它必須依賴於從電梯內選擇的目的地和目的地,你不覺得嗎?例如:電梯在5號,客戶選擇-1(車庫),客戶2在3號,並推動「獲得」。它應該拿起client2嗎?有沒有getForMovingUp和getForMOvingDown或者只有中立的get? – Fildor 2013-04-30 16:55:14

+0

有一個方向可以告訴方向簽名中的方向是向上還是向下。是的,它會在途中接他們 – Dejell 2013-04-30 16:59:23

回答

4

一種可能性是使用兩個獨立的TreeSets訂購地板,updown。如果您要添加上述currentFloor地板,然後將其添加到up,如果您要添加下面currentFloor地板,然後將其添加到down;如果您添加的樓層等於currentFloor,則丟棄該樓層。 TreeSet自動丟棄重複項。當確定下一層要訪問時,如果方向== UP,則訪問up中的下一個最低層,並且如果方向== DOWN,則訪問down中的下一個最高層。

或者您可以使用一個TreeSet中,並試圖想出一個聰明的比較,是以電梯方向考慮,但是,似乎是更多的麻煩比它的價值。

private TreeSet<Integer> up = new TreeSet<>(); // floors above currentFloor 
private TreeSet<Integer> down = new TreeSet<>(); // floors below currentFloor 
private int currentFloor = 0; 
private Enum direction = direction.UP; 

public void addFloor(int f) { 
    if(f < currentFloor) { 
     down.add(f); 
    } else if(f > currentFloor) { 
     up.add(f); 
    } 
    // else f == currentFloor, so don't add the floor to either queue 
} 

public int nextFloor() { 
    if(direction == direction.DOWN) { 
     return down.pollLast(); // highest floor in down, or null if empty 
    } else { 
     return up.pollFirst(); // lowest floor in up, or null if empty 
    } 
} 
2

我試圖使用一個單一的TreeSet實現電梯。

這裏是一個完整的解決方案:你想要它表現得像什麼

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.TreeSet; 

public class MyLift { 

    public static void main(String[] args) { 
     System.out.println("Welcome to MyLift"); 
     // RequestListenerThread to read requested floor and add to Set 
     Thread requestListenerThread = new Thread(new RequestListener(), 
       "RequestListenerThread"); 
     // RequestProcessorThread to read Set and process requested floor 
     Thread requestProcessorThread = new Thread(new RequestProcessor(), 
       "RequestProcessorThread"); 
     requestListenerThread.start(); 
     requestProcessorThread.start(); 
    } 
} 

class Elevator { 
    private static Elevator elevator = null; 
    private static TreeSet<Integer> requestSet = new TreeSet<Integer>(); 
    private int currentFloor = 0; 

    private Direction direction = Direction.UP; 

    private Elevator() { 
    }; 

    /** 
    * @return singleton instance 
    */ 
    static Elevator getInstance() { 
     if (elevator == null) { 
      elevator = new Elevator(); 
     } 
     return elevator; 
    } 

    /** 
    * Add request to Set 
    * 
    * @param floor 
    */ 
    public synchronized void addFloor(int f) { 
     requestSet.add(f); 
     // Notify the thread that a new request has come. 
     notify(); 
    } 

    /** 
    * @return next request to process based on elevator current floor and 
    *   direction 
    */ 
    public synchronized int nextFloor() { 

     Integer floor = null; 

     if (direction == Direction.UP) { 
      if (requestSet.ceiling(currentFloor) != null) { 
       floor = requestSet.ceiling(currentFloor); 
      } else { 
       floor = requestSet.floor(currentFloor); 
      } 
     } else { 
      if (requestSet.floor(currentFloor) != null) { 
       floor = requestSet.floor(currentFloor); 
      } else { 
       floor = requestSet.ceiling(currentFloor); 
      } 
     } 
     if (floor == null) { 
      try { 
       System.out.println("No Request to process. Waiting"); 
       wait(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      System.out.println("Processing Request : " + floor); 
      requestSet.remove(floor); 
     } 
     return (floor == null) ? -1 : floor; 
    } 

    public int getCurrentFloor() { 
     return currentFloor; 
    } 

    /** 
    * Set current floor and direction based on requested floor 
    * 
    * @param currentFloor 
    */ 
    public void setCurrentFloor(int currentFloor) { 
     if (this.currentFloor > currentFloor) { 
      setDirection(Direction.DOWN); 
     } else { 
      setDirection(Direction.UP); 
     } 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     this.currentFloor = currentFloor; 
     System.out.println("Floor : " + currentFloor); 
    } 

    public Direction getDirection() { 
     return direction; 
    } 

    public void setDirection(Direction direction) { 
     this.direction = direction; 
    } 

} 

class RequestProcessor implements Runnable { 

    @Override 
    public void run() { 
     while (true) { 
      Elevator elevator = Elevator.getInstance(); 
      int floor = elevator.nextFloor(); 
      int currentFloor = elevator.getCurrentFloor(); 
      if (floor >= 0) { 
       if (currentFloor > floor) { 
        while (currentFloor > floor) { 
         elevator.setCurrentFloor(--currentFloor); 
        } 
       } else { 
        while (currentFloor < floor) { 
         elevator.setCurrentFloor(++currentFloor); 
        } 
       } 
      } 
     } 
    } 
} 

class RequestListener implements Runnable { 

    @Override 
    public void run() { 

     while (true) { 
      String floorNumberStr = null; 
      try { 
       // Read input from console 
       BufferedReader bufferedReader = new BufferedReader(
         new InputStreamReader(System.in)); 
       floorNumberStr = bufferedReader.readLine(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      if (isValidFloorNumber(floorNumberStr)) { 
       System.out.println("User Pressed : " + floorNumberStr); 
       Elevator elevator = Elevator.getInstance(); 
       elevator.addFloor(Integer.parseInt(floorNumberStr)); 
      } else { 
       System.out.println("Floor Request Invalid : " + floorNumberStr); 
      } 
     } 
    } 

    /** 
    * This method is used to define maximum floors this elevator can process. 
    * @param s - requested floor 
    * @return true if requested floor is integer and upto two digits. (max floor = 99) 
    */ 
    public boolean isValidFloorNumber(String s) { 
     return (s != null) && s.matches("\\d{1,2}"); 
    } 

} 

enum Direction { 
    UP, DOWN 
}