Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package elevator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. public class Elevator
  9. {
  10.     private static final int CAPACITY = 3;
  11.  
  12.     private static final int PERSONS_AT_START = 5;
  13.  
  14.     private static final int TOTAL_FLOORS = 5;
  15.  
  16.     private int direction;
  17.  
  18.     private int currentFloor = 1;
  19.  
  20.     private final List<Person> waitingPersons = new ArrayList<Person>();
  21.  
  22.     private final List<Person> personsIn = new ArrayList<Person>();
  23.  
  24.     private final Random random = new Random();
  25.  
  26.     public static void main(String[] args) {
  27.         new Elevator().run();
  28.     }
  29.  
  30.     public void run() {
  31.         for (int i = 1; i <= PERSONS_AT_START; i++) {
  32.             final Person person = new Person(this, i);
  33.             waitingPersons.add(person);
  34.         }
  35.         while (!waitingPersons.isEmpty() || !personsIn.isEmpty()) {
  36.             unloadPeople();
  37.             loadPeople();
  38.             moveToNextFloor();
  39.         }
  40.     }
  41.  
  42.     private void moveToNextFloor() {
  43.         if (currentFloor == 1) {
  44.             direction = 1;
  45.         } else if (currentFloor == TOTAL_FLOORS) {
  46.             direction = -1;
  47.         }
  48.         currentFloor += direction;
  49.         System.out.format("Moving to floor %d%n", currentFloor);
  50.     }
  51.  
  52.     private boolean hasCapacity() {
  53.         return personsIn.size() < CAPACITY;
  54.     }
  55.  
  56.     private void loadPeople() {
  57.         if (hasCapacity()) {
  58.             for (final Iterator<Person> iter = waitingPersons.iterator();
  59.                     iter.hasNext();)
  60.             {
  61.                 final Person person = iter.next();
  62.                 if (person.getStartFloor() == currentFloor) {
  63.                     iter.remove();
  64.                     personsIn.add(person);
  65.                     System.out.format("Person %d entered at floor %d%n",
  66.                             person.getId(), currentFloor);
  67.                 }
  68.                 if (!hasCapacity()) {
  69.                     return;
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     private void unloadPeople() {
  76.         for (final Iterator<Person> iter = personsIn.iterator();
  77.                 iter.hasNext();)
  78.         {
  79.             final Person person = iter.next();
  80.             if (person.getEndFloor() == currentFloor) {
  81.                 // person.delivered();
  82.                 iter.remove();
  83.                 System.out.format("Person %d exited at floor %d%n",
  84.                         person.getId(), currentFloor);
  85.             }
  86.         }
  87.     }
  88.  
  89.     public int getRandomFloor() {
  90.         return random.nextInt(TOTAL_FLOORS) + 1;
  91.     }
  92. }
  93.  
  94. package elevator;
  95.  
  96. public class Person
  97. {
  98.     private final int id;
  99.  
  100.     private final int startFloor;
  101.  
  102.     private int endFloor;
  103.  
  104.     public Person(Elevator elevator, int id) {
  105.         this.id = id;
  106.         startFloor = elevator.getRandomFloor();
  107.         while ((endFloor = elevator.getRandomFloor()) == startFloor) {
  108.             // try again
  109.         }
  110.         System.out.format("Person %d want to ride from %d to %d%n",
  111.                 id, startFloor, endFloor);
  112.     }
  113.  
  114.     public int getId() {
  115.         return id;
  116.     }
  117.  
  118.     public int getStartFloor() {
  119.         return startFloor;
  120.     }
  121.  
  122.     public int getEndFloor() {
  123.         return endFloor;
  124.     }
  125.  
  126. //  public void delivered() {
  127. //  }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement