1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
package org.elevator;
import org.elevator.event.Event;
import org.elevator.model.Person;
import org.elevator.model.WaitingList;
import org.elevator.student.DumbElevator;
import org.elevator.system.ShadowElevator;
import java.time.LocalTime;
import java.util.*;
public class Building {
public static final int ELEVATOR_CAPACITY = 5;
public static final int MAX_FLOOR = 4;
public static final LocalTime START_TIME = LocalTime.of(6, 0, 0); // 6h 0mn 0s
public static final int MAX_PEOPLE_ARRIVING = 50;
public static final int MAX_NUMBER_OF_PEOPLE_IN_LINE = 7;
public static final float PROBABILITY_TO_SEE_A_NEW_PERSON_IN_LINE = 0.025f;
public static Random random = new Random(10L);
public static void main(String[] args) {
NavigableMap<LocalTime, Event> events = new TreeMap<>();
LocalTime time = START_TIME;
Event startEvent = new Event(Event.ELEVATOR_STARTS);
events.put(time, startEvent);
WaitingList peopleWaitingPerFloor = new WaitingList();
Elevator elevator = new DumbElevator(ELEVATOR_CAPACITY);
int totalNumberOfPeople = peopleWaitingPerFloor.countPeople();
elevator.peopleWaiting(peopleWaitingPerFloor.getLists());
ShadowElevator shadowElevator = new ShadowElevator(ELEVATOR_CAPACITY, peopleWaitingPerFloor);
printInitInfos(peopleWaitingPerFloor, totalNumberOfPeople);
while (totalNumberOfPeople < MAX_PEOPLE_ARRIVING || !shadowElevator.isStopped()) {
if (!events.containsKey(time)) {
if (totalNumberOfPeople < MAX_PEOPLE_ARRIVING) {
totalNumberOfPeople += addNewPersonToWaitingLists(time, peopleWaitingPerFloor, elevator);
if (totalNumberOfPeople == MAX_PEOPLE_ARRIVING) {
shadowElevator.lastPersonArrived();
elevator.lastPersonArrived();
}
}
time = time.plusSeconds(3);
continue;
}
Event nextEvent = events.get(time);
events.remove(time);
if (nextEvent.getName().equals(Event.ELEVATOR_STARTS)) {
Event event = Event.fromElevatorStart(time, elevator, shadowElevator);
LocalTime arrivalTime = event.getTimeOfArrivalFrom(time);
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.ARRIVES_AT_FLOOR)) {
Event event = Event.fromArrivesAtFloor(time, elevator, shadowElevator);
LocalTime arrivalTime = event.getTimeOfArrivalFrom(time);
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.DOOR_OPENING)) {
Event event = Event.fromDoorOpening(time, elevator, shadowElevator);
LocalTime arrivalTime = event.getTimeOfArrivalFrom(time);
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.DOOR_CLOSING)) {
Event event = Event.fromDoorClosing(time, shadowElevator);
LocalTime arrivalTime = time.plus(event.getDuration());
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.LOADING_FIRST_PERSON)) {
Event event = Event.fromLoadingFirstPerson(time, shadowElevator, elevator, nextEvent);
LocalTime arrivalTime = event.getTimeOfArrivalFrom(time);
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.LOADING_NEXT_PERSON)) {
Event event = Event.fromLoadingNextPerson(time, shadowElevator, elevator, nextEvent);
LocalTime arrivalTime = event.getTimeOfArrivalFrom(time);
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.UNLOADING_FIRST_PERSON)) {
Event event = Event.fromUnloadingFirstPerson(time, elevator, shadowElevator, nextEvent);
LocalTime arrivalTime = event.getTimeOfArrivalFrom(time);
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.UNLOADING_NEXT_PERSON)) {
Event event = Event.fromUnloadingNextPerson(time, elevator, shadowElevator, nextEvent);
LocalTime arrivalTime = event.getTimeOfArrivalFrom(time);
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.STAND_BY_AT_FLOOR)) {
Event event = Event.fromStandByAtFloor(time, elevator, shadowElevator);
LocalTime arrivalTime = event.getTimeOfArrivalFrom(time);
events.put(arrivalTime, event);
} else if (nextEvent.getName().equals(Event.STOPPING_AT_FLOOR)) {
shadowElevator.stopping();
}
if (totalNumberOfPeople < MAX_PEOPLE_ARRIVING) {
totalNumberOfPeople += addNewPersonToWaitingLists(time, peopleWaitingPerFloor, elevator);
if (totalNumberOfPeople == MAX_PEOPLE_ARRIVING) {
shadowElevator.lastPersonArrived();
elevator.lastPersonArrived();
}
}
time = time.plusSeconds(3);
}
peopleWaitingPerFloor.print();
shadowElevator.printPeople();
System.out.printf("[%s] Times up\n", time);
}
private static void printInitInfos(WaitingList peopleWaitingPerFloor, int totalNumberOfPeople) {
double averageNumberOfPeoplePerMinute = 1d - Math.pow(1d - PROBABILITY_TO_SEE_A_NEW_PERSON_IN_LINE, 20d);
System.out.printf("Average number of people in line per minute = %4.2f\n", averageNumberOfPeoplePerMinute);
System.out.printf("Number of people waiting = %d\n", totalNumberOfPeople);
peopleWaitingPerFloor.print();
}
private static int addNewPersonToWaitingLists(LocalTime time, WaitingList peopleWaitingPerFloor, Elevator elevator) {
Optional<Map.Entry<Integer, Person>> newPersonWaiting = peopleWaitingPerFloor.addNewPeopleToLists();
if (newPersonWaiting.isPresent()) {
int floor = newPersonWaiting.orElseThrow().getKey();
Person person = newPersonWaiting.orElseThrow().getValue();
elevator.newPersonWaitingAtFloor(floor, person);
System.out.printf("\n[%s] %s calls the elevator from floor %d to go to floor %d\n", time, person.getName(), floor, person.getDestinationFloor());
System.out.printf("Waiting list is now:\n");
peopleWaitingPerFloor.print();
return 1;
} else {
return 0;
}
}
} |
Partager