Basic OTP (Java)

import java.util.*;

class TimeOutTask extends TimerTask {
boolean isTimedOut = false;

public void run() {
isTimedOut = true;
}
}

class BasicOTP {
public static void main(String args[]) {
Random r = new Random();
String otp = new String();
for(int i=0 ; i<8 ; i++) {
otp += r.nextInt(10);
}
System.out.println(otp);

TimeOutTask task = new TimeOutTask();
Timer t = new Timer();
t.schedule(task, 10000L);
ClientThread c = new ClientThread(otp, t, task);
c.start();
}
}

class ClientThread extends Thread {
String otp;
Timer t;
TimeOutTask task;
ClientThread(String otp, Timer t, TimeOutTask task) {
this.otp = otp;
this.t = t;
this.task = task;
}

public void run() {
System.out.println(“Enter the OTP:”);
String input = new Scanner(System.in).nextLine();
if(task.isTimedOut) {
System.out.println(“Time Out!”);
} else if(!input.equals(otp)) {
System.out.println(“Incorrect OTP!”);
} else {
System.out.println(“Logged In!”);
}
System.exit(0);
}
}

/*

Output:
04195447
Enter the OTP:
04195447
Logged In!

*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s