package org.joow.codeelevator;

import org.joow.elevator.BetterElevatorEngine;
import org.joow.elevator.Direction;
import org.joow.elevator.ElevatorEngine;
import static spark.Spark.*;
import spark.*;

public class CodeElevator {
    private static final int DEFAULT_PORT = 9000;

    public static void main(String[] args) {
        setPort(getPort());

        final ElevatorEngine elevatorEngine = new BetterElevatorEngine();

        get(new Route("/call") {
            @Override
            public Object handle(Request request, Response response) {
                final int atFloor = Integer.parseInt(request.queryParams("atFloor"));
                final Direction to = Direction.of(request.queryParams("to"));
                elevatorEngine.callAt(atFloor, to);

                return "";
            }
        });

        get(new Route("/go") {
            @Override
            public Object handle(Request request, Response response) {
                final int floorToGo = Integer.parseInt(request.queryParams("floorToGo"));
                elevatorEngine.go(floorToGo);

                return "";
            }
        });

        get(new Route("/userHasEntered") {
            @Override
            public Object handle(Request request, Response response) {
                return "";
            }
        });

        get(new Route("/userHasExited") {
            @Override
            public Object handle(Request request, Response response) {
                return "";
            }
        });

        get(new Route("/reset") {
            @Override
            public Object handle(Request request, Response response) {
                elevatorEngine.reset();

                return "";
            }
        });

        get(new Route("/nextCommand") {
            @Override
            public Object handle(Request request, Response response) {
                return elevatorEngine.nextCommand();
            }
        });
    }

    private static int getPort() {
        String port = System.getenv("PORT");

        if (port == null) {
            return DEFAULT_PORT;
        }

        return Integer.valueOf(port);
    }
}

@@ -25,16 +25,56 @@
 vator;%0A%0A
+import com.google.common.base.Optional;%0A
 import o
@@ -288,19 +288,22 @@
 c final 
+Str
 in
-t
+g
  DEFAULT
@@ -314,12 +314,14 @@
 T = 
+%22
 9000
+%22
 ;%0A%0A 
@@ -376,24 +376,117 @@
     
-setPort(getPort(
+final String port = Optional.of(System.getenv(%22PORT%22)).or(DEFAULT_PORT);%0A        setPort(Integer.valueOf(port
 ));%0A
@@ -2095,205 +2095,7 @@
    %7D
-%0A%0A    private static int getPort() %7B%0A        String port = System.getenv(%22PORT%22);%0A%0A        if (port == null) %7B%0A            return DEFAULT_PORT;%0A        %7D%0A%0A        return Integer.valueOf(port);%0A    %7D
 %0A%7D%0A

Use optional to get server port.