// Copyright (c) bowen hui

import java.io.*;

// this program is a *very* simple version of ``eliza'' -- the infamous
// conversational robot. with the limited tricks we have learned about java,
// we can program a simple version of eliza. 
//
// this program makes use of variables, procedures, input, output, random
// numbers, and if-else. something new that you have not seen before is in the
// main procedure ``RobotFriend Jay = new RobotFriend();''. this line actually
// creates an object. what does this mean? if you really want to know, ask the
// TA. otherwise, don't worry about it!

public class RobotFriend
{
   // private variables to keep track of things
   String myname;
   String mycountry;
   String username;

   public final static void main( String argv[] ) throws IOException
   {
      // prepare for getting input from user
      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
      String response;

      // creates a student object -- let's call him "Jay"
      RobotFriend Jay = new RobotFriend();

      // ask the user for his/her name
      System.out.print( "Please enter your name: " );
      response = input.readLine(); 
      Jay.setFriend( response );

      // ask the user for Jay's real name
      System.out.print( "Please enter the name of the Robot: " );
      response = input.readLine(); 
      Jay.setName( response );

      // ask the user for Jay's home country
      System.out.print( "Please enter the Robot's country of origin: " );
      response = input.readLine(); 
      Jay.setCountry( response );

      // now, Jay will initiate the conversation with the user
      Jay.startConversation();
   }

   // these procedures are responsible for setting the private variables to
   // the values given by the user
   public void setName( String n ){    myname    = n; }
   public void setCountry( String c ){ mycountry = c; }
   public void setFriend( String f ){  username  = f; }

   // this procedure is responsible for directing the conversation with the
   // user and trying to understand what the user says
   public void startConversation() throws IOException
   {
      // again, prepare for getting input from user
      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
      String answer;

      // introduce yourself to the user
      System.out.print( "\nHi " + username + "! " );
      System.out.print( "My name is " + myname ); 
      System.out.println( " and I am from " + mycountry + "." );

      // initiate conversation with user
      System.out.println( "" );
      System.out.println( "So, " + username + ", what is your favourite colour?" );
      answer = input.readLine();
      favouriteThings( answer );
      System.out.println( "What is your favourite sport?" );
      answer = input.readLine();
      favouriteThings( answer );

      // attempt to understand what the user says (very simple stuff)
      // if you want to learn how to do fancier things here, read up on
      // strings, substrings, and parsing.
      System.out.println( "Have you been to " + mycountry + " before?" );
      answer = input.readLine();
      if( answer.equals( "yes" ))
         System.out.println( "Great! Did you like it?" );
      else
         System.out.println( "That's too bad. It's a nice place. I miss it a lot." );

      // ask the user for a topic
      System.out.println( "" ); 
      System.out.println( "So, what would you like to talk about next, " + username + "?" ); 
      answer = input.readLine(); 
      dontknow(); 
      answer = input.readLine(); 
      dontknow(); 
      answer = input.readLine(); 
      dontknow();

      // end the conversation
      System.out.println( "" );
      System.out.println( "Bye!" );
      System.out.println( "" );
   }

   // this procedure handles typical responses about favourite things
   public void favouriteThings( String s )
   {
      // randomize the answers
      int i = ( int )( Math.random() * 6);
      if( i == 0 ) 
         System.out.println( "No, I don't like it that much. " + 
         "What about your second favourite one?" ); 
      else 
      if( i == 1 ) 
         System.out.println( "I'm not sure about " + s + "... let me think about that." );
      else 
         System.out.println( "Really? Mine too!!!" );
   }

   // this procedure handles typical responses for everything else
   public void dontknow()
   {
      // randomize the answers to look more intelligent!
      int i = ( int )( Math.random() * 6);
      if( i == 0 )
         System.out.println( "What did you say?" );
      else
      if( i == 1 )
         System.out.println( "Are you crazy?" );
      else
      if( i == 2 )
         System.out.println( "Could you repeat that please?" );
      else
      if( i == 3 )
         System.out.println( "Maybe you're right." );
      else
      if( i == 4 )
         System.out.println( "I have to go now. Let's talk later." );
      else
         System.out.println( "I don't understand what you said..." );
    }
}

