0

I'm trying to make an application which copy an elevator operation (2 floors). But when I ask the user which floor he want to go, there are 2 differents possibilities. The first one, user enter a floor and the elevator move. The second one, after 10 secondes, still no response from user, at this point, the elevator have to close his door and turn off the light.

So my probleme is with the timeout, because I want my "while" to continue.

My main:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class TP5 {
  private String floorAsk = "";

  TimerTask task = new TimerTask() {
    public void run() {
      if (floorAsk.equals("")) {
        System.out.println("No response ...");
        task.cancel();
      }
    }    
  };

  public boolean getInput() throws Exception {
    Timer timer = new Timer();
    timer.schedule(task, 10 * 1000);

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    floorAsk = in.readLine();

    timer.cancel();
    System.out.println("Floor ask " + floorAsk);

    return true;
  }


  /**
   * main.
   * @param args ceci est un String[]
   */
  public static void main(String[] args) {
    Controller c = new Controller();
    Door p = new Door();
    Light l = new Light();
    Engine m = new Engine();
    Button b = new CallButton();

    while (true) {
      Scanner sc = new Scanner(System.in);
      System.out.println("What is your floor ?");
      String actualFloorString = sc.nextLine();

      int actualFloor = Integer.parseInt(actualFloorString);

      if (actualFloor == 0 || etageAct == 1) {
        System.out.println("You are at floor " + actualFloorString);
        if (actualFloor != c.getShaft()) {
          if (actualFloor > c.getShaft()) {
            m.up();
          } else if (actualFloor < c.getShaft()) {
            m.down();
          }
        }

        if (!l.isOn()) {
          l.on();
        }

        if (!p.isOpen()) {
          p.open();
        }

        System.out.println("Which floor do you want to go ?");
        try {
          (new TP5()).getInput();
        } catch (Exception e) {
          System.out.println(e);
        }

        System.out.println("test");

      } else {
        System.out.println("Please enter a valid floor (0 or 1)");
      }
    }

  }
}

I try to use this solution: Time limit for an input

but I want to go back in my "while" and I don't know how to do it or if I'm using the right way to do it.

I also have some other class, Contoleur, Lumiere, Moteur, Porte, Bouton. But I haven't code the functions yet.

Thanks for you answer

EDIT

Ok I maybe find a way, I modify my code and now I have a function which take in parameter the scanner and a String:

public static int ask(Scanner sc, String t) {
    System.out.println(t);

    return sc.nextInt();
}

And I was wondering that maybe it's possible to put a timeout on a function. Do you know if it's possible ?

J.Doe
  • 45
  • 5
  • Your code would be far better to understand If you translate it to english. – Dilyano Senders Feb 22 '19 at 14:32
  • Sorry, I translate it – J.Doe Feb 22 '19 at 14:40
  • Try to instantiate a boolean. And use that boolean for your while statement. That way you can step out and step back into your while loop. You can set the boolean to true/false using your TimerTask – Dilyano Senders Feb 22 '19 at 14:49
  • Doesn't work, after the 10 sec, I just got the message "No response ..." (I add a print after my loop) but doesn't seem to affect the programm – J.Doe Feb 22 '19 at 14:57
  • Somebody know if there is a way to get back in the loop, or if there is any other way ? – J.Doe Feb 23 '19 at 09:52

1 Answers1

0

I would suggest the use of the Timer class. https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html

After a 10 second timeout, have the timer call a method that "closes" the elevator door.

The Timer can be canceled if the user inputs a valid integer. A new timer can be created when the door opens.

Edit: I didn't see you were already using the Timer object as I was scrolled down to the main method.

aglassman
  • 2,643
  • 1
  • 17
  • 30
  • I tried to return a boolean value with my function getInput() but I don't know how I can return the value false if no input – J.Doe Feb 22 '19 at 16:59