import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.IOException;
import java.util.Date;

public class Tqotd implements Runnable {

  // to be recovered from the main
  private Socket csocket;

  //constructor to define a parameter to the class
  public Tqotd(Socket csocket){
    this.csocket = csocket;
  }

  public void run(){

    System.out.println(csocket);

    try{
      // Build a BufferedReader to read text from socket
      BufferedReader entreeSocket = new BufferedReader(new InputStreamReader(csocket.getInputStream()));

      // Build a PrintStream to send text on socket
      PrintStream sortieSocket = new PrintStream(csocket.getOutputStream());

      // read from socket
      String checkapp = entreeSocket.readLine();

      if (checkapp.equals("qotd")) {
        Date d = new Date();
        String reponse = "the date is : " + d + ", Have a nice day!";

        // send qotd on socket
        sortieSocket.println(reponse);
      }

      // close connection
      csocket.close();
    } catch (IOException e){
      System.out.println(e);
    }

  }
}
