import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.io.*;
import java.net.*;

public class WhoAmI extends Applet implements MouseListener, Runnable {
   static final int QUESTION = 0;
   static final int GUESS = 1;
   static final int GIVEUP = 2;
   static final int RESPONSE = 3;

   static final int WELCOMEY= 50;
   static final int WELCOMEX= 90;
   static final int WELCOMEW= 450;
   static final int WELCOMEH= 270;

   static final int QUESTIONY= 100;
   static final int QUESTIONX= 110;
   static final int QUESTIONW= 500;
   static final int QUESTIONH= 200;

   static final int LABEL1= 135;	
   static final int LABEL2= 155;

   static final int BTNBOTTOM= 260;
   static final int BTNSTART= 450;

   static final int BUTTONW= 80;
   static final int BUTTONH= 30;

   static final int PREFERRED_QUESTIONS[] = {0, 1, 7, 12, 17};

   static final String theLosers[][] = {
	{"You're Lying!!!", 
	 "You may play again if you would like but your", 
	 "lying will not be tolerated for long!"},
	{"You're Wrong!!!",
	 "I'll let you play again but first you", 
	 "better make sure you know who you are!"},
	{"Are You Kidding!!!",
	 "We both know who you are.", 
	 "Come back when you are willing to admit it!"},
	{"I Think You're Mistaken!!!",
	 "If you check a mirror you.", 
	 "will realize that you are who I say you are!"}
   };

   Vector thePeople = new Vector();
   Vector theQuestions = new Vector();
   Vector holdPeople = new Vector();
   Vector holdQuestions = new Vector();
						     
   Font smFont = new Font("SansSerif", Font.BOLD, 10);
   Font medFont = new Font("SansSerif", Font.BOLD, 12);
   Font iFont = new Font("SansSerif", Font.ITALIC, 12);
   Font lgFont = new Font("SansSerif", Font.BOLD, 16);
   Font xlgFont = new Font("SansSerif", Font.BOLD, 24);

   boolean gameInProgress = false;
   boolean errorFlag = false;

   Thread hmmm = null;	

   Question question = null;
   Person guessPerson = null;
   Person holdPerson = null;
   int guesses = 0;
   int guessSw;
   int gamesPlayed = 0;
   int gamesWon = 0;
   
   // Set up clicking areas
   Rectangle PlayAgain = new Rectangle(	520, 260, BUTTONW, BUTTONH);
   Rectangle Play = new Rectangle(BTNSTART, 280, BUTTONW, BUTTONH);
   Rectangle Yes = new Rectangle(150, BTNBOTTOM, BUTTONW, BUTTONH);
   Rectangle NotSure = new Rectangle(300, BTNBOTTOM, BUTTONW, BUTTONH);
   Rectangle No = new Rectangle(BTNSTART, BTNBOTTOM, BUTTONW, BUTTONH);

   //Initialize the applet
   public void init() {
      setBackground(Color.orange);
      addMouseListener(this);
      thePeople = assemblePeople();
      assembleQuestions();
      holdPeople = (Vector)thePeople.clone();
      holdQuestions = (Vector)theQuestions.clone();
   }

/************************************************************************ 				PAINT METHODS
************************************************************************/ 
   public void update(Graphics g) {
	paint(g);
   }
	
   public void paint(Graphics g) {
	if(errorFlag) {
	    errorHandler();
	}
	if(!gameInProgress) {
	    paintWelcome(g);
	}
	else {
	    paintQuestion();
	}
   }

   private void paintWelcome(Graphics g) {
	g.clearRect(0, 0, 514, 400);
	g.setColor(Color.yellow);
	g.fillRect(WELCOMEX, WELCOMEY, WELCOMEW, WELCOMEH);
	g.setColor(Color.black);
	g.fillRect(WELCOMEX + 3, WELCOMEY + 3, WELCOMEW - 6, WELCOMEH - 6);
	g.setColor(Color.white);
	g.setFont(lgFont);
	g.drawString("WELCOME!!!", WELCOMEX + 15, WELCOMEY + 35);
	g.setFont(medFont);
	g.drawString("Your mission, should you choose to accept, is to", LABEL1, WELCOMEY + 55);
	g.drawString("pretend to be a character from a popular tv sit-com.", LABEL1, WELCOMEY + 67);
	g.drawString("I will try to guess who you are.", LABEL1, WELCOMEY + 90);
	g.drawString("If I am successful, you will turn over all your worldy ", LABEL1, WELCOMEY + 115);
	g.drawString("possessions (arrangements will be made).", LABEL1, WELCOMEY + 127);
	g.drawString("If I am unable to correctly figure out who you are,", LABEL1, WELCOMEY + 140);
	g.drawString("you will get a point.", LABEL1, WELCOMEY + 152);
	g.setFont(iFont);
	g.drawString("Remember: Your chosen character may have changed over the years.", WELCOMEX + 25, WELCOMEY + 180);
	g.drawString("My questions only consider the person as we mostly knew them.", WELCOMEX + 25, WELCOMEY + 192);

	// Add a Play button
	g.setFont(medFont);
	g.setColor(Color.lightGray);
	g.fill3DRect(BTNSTART, 280, BUTTONW, BUTTONH, true);
	g.setColor(Color.black);
	g.drawRect(BTNSTART - 2, 280 - 2, BUTTONW, BUTTONH);
	g.drawString("Let's Play", BTNSTART + 15, 300); 
   }

   private void paintScore(Graphics g) {
	g.clearRect(470, 5, 200, 70);

	if(gamesPlayed > 0) {
	    g.setFont(medFont);
	    String score = "We've played " + gamesPlayed + " game";
	    if(gamesPlayed > 1) {
	        score += "s.";
	    }
	    g.drawString(score, 480, 20);	
	    if(gamesWon < 1) {
	        g.drawString("I haven't won yet!!", 500, 40);
	        //g.drawString("What are you, cheating??", 500, 55);	
	    }
	    else if(gamesWon == gamesPlayed) {
	        g.drawString("You haven't won yet!!", 500, 40);	
	    }
	    else {
	        int won = gamesPlayed - gamesWon;
	        g.drawString("You have won " + won + " of them!!", 500, 40);	
    	    }
	}
   }

   private void paintQuestion() {
	Graphics g = getGraphics();
	g.clearRect(WELCOMEX, WELCOMEY, WELCOMEW, WELCOMEH);
	
	paintScore(g);

	g.setColor(Color.black);
	g.fillRect(QUESTIONX, QUESTIONY, QUESTIONW, QUESTIONH);
	g.setColor(Color.cyan);
	g.fillRect(QUESTIONX+3,QUESTIONY + 3,QUESTIONW - 6,QUESTIONH - 6);
	g.setColor(Color.black);

	g.setFont(medFont);

	// Add a Yes button
	g.setColor(Color.lightGray);
	g.fill3DRect(150, BTNBOTTOM, BUTTONW, BUTTONH, true);
	g.setColor(Color.black);
	g.drawRect(150 - 2, BTNBOTTOM - 2, BUTTONW, BUTTONH);
	g.drawString("YES", 150 + 27, BTNBOTTOM + 17); 

	// Add a NO/OK button
        g.setColor(Color.lightGray);
	g.fill3DRect(BTNSTART, BTNBOTTOM, BUTTONW, BUTTONH, true);
	g.setColor(Color.black);
	g.drawRect(BTNSTART - 2, BTNBOTTOM - 2, BUTTONW, BUTTONH);
	g.drawString("NO", BTNSTART + 27, BTNBOTTOM + 17); 

	if(guessSw == GUESS) {
	    guessSw = RESPONSE;
	    String name = guessPerson.Name;
	    String show = guessPerson.Show; 
	    g.setFont(xlgFont);
	    g.drawString("Are You " + name, LABEL1, QUESTIONY + 60);
	    g.drawString("from " + show + "??", LABEL2, QUESTIONY+ 95);
	}
  	else {
	    // Add a Maybe button
	    g.setColor(Color.lightGray);
	    g.fill3DRect(300, BTNBOTTOM, BUTTONW, BUTTONH, true);
	    g.setColor(Color.black);
	    g.drawRect(300 - 2, BTNBOTTOM - 2, BUTTONW, BUTTONH);
	    g.drawString("NOT SURE", 300 + 12, BTNBOTTOM + 17); 
	  
	    g.setFont(xlgFont);
            String myQuestion = question.theQuestion;
	    if(myQuestion.length() < 36) {
	         g.drawString(myQuestion, LABEL1, QUESTIONY + 60);
	    }
	    else {
	          int split = wordWrapper(myQuestion, 35);
		  g.drawString(myQuestion.substring(0, split), LABEL1, QUESTIONY + 60);
		  String questPart2 = myQuestion.substring(split + 1);
		  if(questPart2.length() < 32) {	         		      		      		      g.drawString(myQuestion.substring(split + 1), QUESTIONX + 40, QUESTIONY + 95);
		  }
		  else {
		      split = wordWrapper(questPart2, 31);
                      g.drawString(questPart2.substring(0, split), QUESTIONX + 40, QUESTIONY + 95);
                      g.drawString(questPart2.substring(split + 1), QUESTIONX + 60, QUESTIONY + 130);
		  }
	    }
	}
   }

   private void errorHandler() {
	Graphics g = getGraphics();
	g.clearRect(WELCOMEX, WELCOMEY, WELCOMEW, WELCOMEH);
	g.setColor(Color.black);
	g.fillRect(QUESTIONX, QUESTIONY, QUESTIONW, QUESTIONH);
	g.setColor(Color.yellow);
	g.fillRect(QUESTIONX+3,QUESTIONY + 3,QUESTIONW - 6,QUESTIONH - 6);
	g.setColor(Color.black);

	g.setFont(xlgFont);
       g.drawString("I Don't Feel Like Playing Now.", LABEL1, QUESTIONY + 60);
	g.drawString("Go Away!", LABEL2, QUESTIONY + 95);
   }

   private void sayGoodbye(int answer) {
	gameInProgress = false;

	Graphics g = getGraphics();
	g.clearRect(WELCOMEX, WELCOMEY, WELCOMEW, WELCOMEH);
	g.setColor(Color.black);
	g.fillRect(QUESTIONX, QUESTIONY, QUESTIONW, QUESTIONH);
	g.setColor(Color.yellow);
	g.fillRect(QUESTIONX+3,QUESTIONY + 3,QUESTIONW - 6,QUESTIONH - 6);
	g.setColor(Color.black);

	g.setFont(xlgFont);

	if(guessSw == GIVEUP) {
	    if(holdPerson == null) {
	        g.drawString("I Give Up.", LABEL1, QUESTIONY + 60);
	        g.drawString("You Get 1 Point!", LABEL2, QUESTIONY + 95);
	    }
	    else {
	        guessPerson = holdPerson;
	        paintQuestion();
	    }
	}
	else {
	    if(answer == 1) {
	        g.drawString("Naturally!!!", LABEL1, QUESTIONY + 60);
    	        String lbl = "It took me ";
	        String boast = "I can't believe you stuck around that long.";
		if(guesses < 11) {
                    lbl += "ONLY ";
		    boast = "Admit it, you're amazed by my powers!";	 
		}
		else if(guesses < 21) {
                    lbl += "ONLY ";
		    boast = "Impressed?? Go tell your friends!";	 
		}
    	        else if(guesses < 31) {
		    boast = "You'll have to do better than that next time!";	 
		}
                else if(guesses < 41) {
	            boast = "You're good, but not that good!";
	        }
                else if(guesses < 51) {
	            boast = "I bet you thought you had me!";
	        }
    	        else if(guesses < 61) {
		    boast = "You almost got me this time.";
	        }
	        lbl += guesses + " guesses to figure you out!!";
	        g.setFont(lgFont);
	        g.drawString(lbl, LABEL2, QUESTIONY + 100);
	        g.drawString(boast, LABEL2, QUESTIONY + 125);
	    }
	    else if(answer == 0) {
		 int x = (int)(Math.random() * 4);
	        g.drawString(theLosers[x][0], LABEL1, QUESTIONY + 60);
	        g.setFont(lgFont);
	        g.drawString(theLosers[x][1], LABEL2, QUESTIONY + 100);
	        g.drawString(theLosers[x][2], LABEL2, QUESTIONY + 125);
	    }
	}

	gamesPlayed++;
	paintScore(g);

	// Add a Play button
	g.setFont(medFont);
	g.setColor(Color.lightGray);
	g.fill3DRect(520, 260, BUTTONW, BUTTONH, true);
	g.setColor(Color.black);
	g.drawRect(520 - 2, 260 - 2, BUTTONW, BUTTONH);
	g.drawString("Play Again", 520 + 12, 280); 
  }

/***********************************************************************
		Logical Methods	
************************************************************************/ 

   public void stop() {
	hmmm = null;
   }	

   public void run() {
	Graphics g = getGraphics();
	g.clearRect(WELCOMEX, WELCOMEY, WELCOMEW, WELCOMEH);
	
	paintScore(g);

	g.setColor(Color.black);
	g.fillRect(QUESTIONX, QUESTIONY, QUESTIONW, QUESTIONH);
	g.setColor(Color.cyan);
	g.fillRect(QUESTIONX+3,QUESTIONY + 3,QUESTIONW - 6,QUESTIONH - 6);
	g.setColor(Color.black);

	g.setFont(lgFont);
	g.drawString("Hmmmmm...", LABEL2, QUESTIONY + 60);
	try {
	    hmmm.sleep(1500);
	    hmmm = null;
	}
	catch(InterruptedException ie){}
	paintQuestion();
   }

   private Vector assemblePeople() {
       Vector people = new Vector();

	// first - read file of keywords - store for now
	String line = null;
	URL url = null; 
	Vector keys = new Vector();
	try {
	    url = new URL (getCodeBase(), "keywords.txt");
	}
	catch (Exception e1) {
            System.out.println("Assemble People 1:" + e1);
	    errorFlag = true;
        } 
	try { 
	    InputStream in = url.openStream(); 
            BufferedReader dis = new BufferedReader(new InputStreamReader(in)); 
	    while ((line = dis.readLine()) != null){ 
		 keys.addElement(line);
	    } 
	    //System.out.println("There are " + keys.size() + " keys");
	    in.close(); 
        } 
	catch (Exception e2) {
            System.out.println("Assemble People 2:" + e2);
	    errorFlag = true;
        } 

	// second - read file of people
	try {
	    url = new URL (getCodeBase(), "people.txt");
	}
	catch (Exception e3) {
            System.out.println("Assemble People 3:" + e3);
	    errorFlag = true;
        } 
	try { 
	    InputStream in = url.openStream(); 
            BufferedReader dis = new BufferedReader(new InputStreamReader(in));
	    while ((line = dis.readLine()) != null){ 
	         StringTokenizer stok = new StringTokenizer(line, ",");
		 String name = stok.nextToken();
		 String show = stok.nextToken();
		 String values = stok.nextToken();
		 System.out.println(name + ":" + show); 		 		 System.out.println("Values=" + values);
		 Person temp = new Person(name, show, keys, values);
		 System.out.println(temp.Name + ":" + temp.Show); 		 		 people.addElement(temp);
	         //System.out.println("There are " + people.size() + " people");
	    } 
	    //System.out.println("There are " + people.size() + " people");
	    in.close(); 
        } 
	catch (Exception e4) {
            System.out.println("Assemble People 4:" + e4);
	    errorFlag = true;
        } 
	return people;
   }

   private void assembleQuestions() {
	if(theQuestions.size() > 0) {
	    theQuestions.removeAllElements();
	}
	String line = null;
	URL url = null; 
	try {
	    url = new URL (getCodeBase(), "questions.txt");
	}
	catch (Exception e ) { 
	   System.out.println("Assemble Questions 1:" + e); 
	   errorFlag = true;
	} 
	try { 
	    InputStream in = url.openStream(); 
            BufferedReader dis = new BufferedReader(new InputStreamReader(in));
	    int i = 0;
	    while ((line = dis.readLine()) != null){ 
	         StringTokenizer stok = new StringTokenizer(line, "*");
		 String quest = stok.nextToken();
		 String key = stok.nextToken();
		 String val = stok.nextToken();
		 theQuestions.addElement(new Question(quest, key, i, val));
		 i++;
	    } 
            //System.out.println("There are " + theQuestions.size() + " questions");
	    in.close(); 
       } 
       catch (Exception ioe) {
            System.out.println("Assemble Questions 2:" + ioe);
	    errorFlag = true;
       } 
   }
   
   private void removePeople(String answer) {
   	String keyword = question.keyword;
	//System.out.println("Answer= " + answer);
	//System.out.println("Keyword = " + keyword);
	//System.out.println("There are " + thePeople.size() + " left");
	Person person = null;
	for(int i = (thePeople.size()-1); i > -1; i--) {
	    person = (Person)thePeople.elementAt(i);
	    //System.out.println("Checking..." + person.Name);
	    Hashtable hash = person.Keywords;
	    String value = (String)hash.get(keyword);
	    if(!answer.equals(value)) {
		 person.Strikes++;
	        if(keyword.equals("MALE") || person.Strikes > 1) {
		     //System.out.println(" Removing " + person.Name);
	    	     thePeople.removeElement(person);
		 }
	    }
	}
	//System.out.println("There are NOW " + thePeople.size());
	prepareNextQuestion(Integer.parseInt(answer));
   }

   private void prepareNextQuestion(int answer) {
	hmmm = null;
	// first determine if we are ready to make a guess or give up
	int peopleCount = thePeople.size();
	if(peopleCount == 1) {
	    guessPerson = (Person)thePeople.elementAt(0);
	    guessSw = GUESS;
	    paintQuestion();
	    return;
	}

	if(peopleCount == 0 || theQuestions.size() == 0) {
	    guessSw = GIVEUP;
	    sayGoodbye(-1); 
	}
	else {
	    if(peopleCount < 10) {
	    	holdPerson = (Person)thePeople.elementAt(0);
	    }
	    
	    // if we are not prepared to guess or Give up, see if we
	    // need to remove any questions that don't apply from the array

	    //System.out.println("QuestionSize = " + theQuestions.size() );
	    if(question.keyword.equals("MALE") && answer == 0) {
	        removeQuestions("MALE");
	    }
	    else if(question.keyword.equals("MALE") && answer == 1) {
	        removeQuestions("FEMALE");
	    }
	    else if(question.keyword.equals("ADULT") && answer == 0) {
		 removeQuestions("ADULT");
	    }
	    else if(question.keyword.equals("ADULT") && answer == 1) {
		 removeQuestions("CHILDREN");
	    }
	    else if(question.keyword.equals("WORK") && answer == 0) {
		 removeQuestions("WORK");
	    }
	    else if(question.keyword.equals("MARRIED") && answer == 0) {
		 removeQuestions("MARRIED");
	    }
	    else if(question.keyword.equals("CHILD") && answer == 0) {
		 removeQuestions("PARENT");
	    }
	    if(question.onlyForLive && answer == 1) {
		 removeQuestions("LIVE");
	    }
	    if(theQuestions.size() == 0) {
	        guessSw = GIVEUP;
	        sayGoodbye(-1); 
	        return;
	    }

	    guesses++;
	    // prepare the next question to be asked
	    int which = 0;
	    if(peopleCount < 20) {
	        for(int x = peopleCount - 1; x > 0; x--) {
		     Person p1 = (Person)thePeople.elementAt(x);
		     Person p2 = (Person)thePeople.elementAt(x - 1);
		     //System.out.println("p1=" + p1.Name);
		     //System.out.println("p2=" + p2.Name);
		     if(!p1.StrValues.equals(p2.StrValues)) {
		         for(int i = 0; i < p1.StrValues.length(); i++) {
		             String val1 = p1.StrValues.substring(i,i+1);
		             String val2 = p2.StrValues.substring(i,i+1);
		 	      //System.out.println("val1=" + val1);
		             //System.out.println("val2=" + val2);
		             if(!val1.equals(val2)) {
 		                 which = findQuestion(i);	
				   if(which != -1) {
				       x = 0;
				       break;
				   }	            
			      }
		         }
		     }
		 }
		 if(which == -1) {
		     which = 0;
		 }
	    }
	    else if(guesses > 6) {
	        which = (int)(Math.random() * theQuestions.size());
	    }
            else if (guesses < 6) {
		which = findQuestion(PREFERRED_QUESTIONS[guesses - 1]);
		if(which == -1) {
		     which = 0;
		}
	    }	 
	    question = (Question)theQuestions.elementAt(which);
	    theQuestions.removeElementAt(which);
	    guessSw = QUESTION;

	    if(guesses % 20 == 0) {
	        hmmm = new Thread(this);
	        hmmm.start();
	    }
	    else {
	        paintQuestion();
            }
	}
   }

   private int wordWrapper(String question, int maxLen) {
	int split = -1;
	while (true) {
	    int y = question.indexOf(" ", split + 1);
	    if(y > 0 && y < maxLen) {
	        split = y;
	    }
	    else {
	        break;
	    }
	}
	return split;
   }

   private int findQuestion(int which) {
	//System.out.println("which=" + which);
	for(int i = 0; i < theQuestions.size(); i++) {
	    Question temp = (Question)theQuestions.elementAt(i);
	    //System.out.println(temp.keyword + " index=" + temp.indx);
	    if(which == temp.indx) {
	        return i;	
	    }    
	}
	return -1;
   }

   private void removeQuestions(String which) {
	Question temp = null;
	for(int i = theQuestions.size() - 1; i > 0; i--) {
	    temp = (Question)theQuestions.elementAt(i);
	    if(which.equals("MALE") && temp.onlyForMales) {
	        theQuestions.removeElementAt(i);    	
	    }
	    if(which.equals("FEMALE") && temp.onlyForFemales) {
	        theQuestions.removeElementAt(i);    	
	    }
	    if(which.equals("PARENT") && temp.onlyForParents) {
	        theQuestions.removeElementAt(i);    	
	    }
	    if(which.equals("ADULT") && temp.onlyForAdults) {
	        theQuestions.removeElementAt(i);    	
	    }
	    if(which.equals("CHILDREN") && temp.onlyForChildren) {
	        theQuestions.removeElementAt(i);    	
	    }
	    if(which.equals("WORK") && temp.onlyForWorkers) {
	        theQuestions.removeElementAt(i);    	
	    }
	    if(which.equals("MARRIED") && temp.onlyForMarried) {
	        theQuestions.removeElementAt(i);    	
	    }
	    if(which.equals("LIVE") && temp.onlyForLive) {
	        theQuestions.removeElementAt(i);    	
	    }
 	}
   }

/************************************************************************ 
The user has clicked the applet. Figure out where and what they want.
************************************************************************/ 
   public void mouseReleased(MouseEvent e) {
       int MEx = e.getX();
       int MEy = e.getY();
     
       if(gameInProgress == false) {
	    if(Play.contains(MEx, MEy) || PlayAgain.contains(MEx, MEy)) {
	        gameInProgress = true;
	        guesses = 1;
		holdPerson = null;

		if(gamesPlayed > 0) {
		    // assemble people
	            thePeople = (Vector)holdPeople.clone();
		    // assemble questions
	            theQuestions = (Vector)holdQuestions.clone();
		}
	        if(errorFlag) {
		    errorHandler();
		    return;
		}
	
	        // present question
	        question = (Question)theQuestions.firstElement();
	        theQuestions.removeElementAt(0);
	        guessSw = QUESTION;
	        paintQuestion();
	    }
       }
       else if(guessSw == QUESTION) {
           if(Yes.contains(MEx, MEy)) {
               removePeople("1");	
           }
           else if(NotSure.contains(MEx, MEy) || No.contains(MEx, MEy)) {
               removePeople("0");	
           }
       }
       else if(guessSw == RESPONSE) {
           if(Yes.contains(MEx, MEy)) {
		 gamesWon++;
		 sayGoodbye(1);
           }
           else if(No.contains(MEx, MEy)) {
		 sayGoodbye(0);
           }
       }
   }

   public void mousePressed(MouseEvent e) {}

   public void mouseClicked(MouseEvent e) {}

   public void mouseEntered(MouseEvent e) {}

   public void mouseExited(MouseEvent e) {}

   public void destroy(){
      removeMouseListener(this);
   }

   public String getAppletInfo() {
      return "Who Am I by Jay Lipp";
   }

/************************************************************************ 			
Inner classes
************************************************************************/ 

   class Person {
	public String Name;
	public String Show;
	public Hashtable Keywords;
	public String StrValues;
	public int Strikes;

	public Person(String name, String show, 
			Vector keys, String values) throws Exception {
	    Name = name;
	    Show = show;
	    Strikes = 0;
	    Keywords = new Hashtable();
	    StrValues = values;
	    for(int x = 0; x < keys.size(); x++) {
	        String tempStr = (String)keys.elementAt(x);
		 String val = values.substring(x, x+1);
		 //System.out.println(tempStr + ":" + val);
		 Keywords.put(tempStr, val);
	    }
	}
   }

   class Question {
	public String theQuestion;
	public String keyword;
	public boolean onlyForMales;
	public boolean onlyForFemales;
	public boolean onlyForAdults;
	public boolean onlyForWorkers;
	public boolean onlyForMarried;
	public boolean onlyForParents;
	public boolean onlyForLive;
        public boolean onlyForChildren;
	public int indx;

	public Question(String question, String key, int i) {
	    theQuestion = question;
	    keyword = key;
            onlyForMales = false;
            onlyForFemales = false;
            onlyForAdults = false;
	    onlyForWorkers = false;
	    onlyForParents = false;
	    onlyForLive = false;
            onlyForChildren = false;

	    indx = i;
	}

	public Question(String question, String key, int i, String values) {
	    theQuestion = question;
	    keyword = key;
	    indx = i;

	    onlyForMales = false;
	    if(values.substring(0, 1).equals("1")) {
	        onlyForMales = true;
	    }
            onlyForAdults = false;
	    if(values.substring(1, 2).equals("1")) {
	        onlyForAdults = true;
	    }
	    onlyForWorkers = false;
	    if(values.substring(2, 3).equals("1")) {
	        onlyForWorkers = true;
	    }
	    onlyForMarried = false;
	    if(values.substring(3, 4).equals("1")) {
	        onlyForMarried = true;
	    }
	    onlyForLive = false;
	    if(values.substring(4, 5).equals("1")) {
	        onlyForLive = true;
	    }
	    onlyForFemales = false;
	    if(values.substring(5, 6).equals("1")) {
	        onlyForFemales = true;
	    }
	    onlyForParents = false;
	    if(values.substring(6, 7).equals("1")) {
	        onlyForParents = true;
	    }
	    onlyForChildren = false;
	    if(values.substring(7).equals("1")) {
	        onlyForChildren = true;
	    }

	}
   }
}