// A simple psychometric-like type tool 	
	
import javax.swing.*;	
import java.awt.*;	
import java.awt.event.*;	
	
public class TypeIndicator {	
	// class constants 	
	private final int WINDOW_WIDTH = 400;	
	private final int WINDOW_HEIGHT = 325;	
	private final int TEXT_WIDTH = 30;	
	
	private final String LEGEND =	
		  "A series of preferences are asked. The responses are "	
		+ "used to determine a personality type profile. The tool "	
		+ "is primitive and is meant for fun. You should not make "	
		+ "any decisions based on its analysis. ";	
	
	// instance GUI variables 	
	private JFrame window = new JFrame("Type Indicator");	
	private JTextArea legendArea = 	
			new JTextArea(LEGEND, 4, TEXT_WIDTH);	
	private JTextArea resultArea = new JTextArea();	
	private JTextField statementPad = 	
			new JTextField("", TEXT_WIDTH);	
	private JRadioButton trueButton = new JRadioButton("True ");	
	private JRadioButton falseButton = new JRadioButton("False");	
	private JRadioButton hiddenButton = new JRadioButton("");	
	private ButtonGroup buttonGroup = new ButtonGroup();	
		
	// instance test variables 	
	private boolean introvert;	
	private boolean senser;	
	private boolean thinker;	
	private boolean judger;	
	
	// main(): application entry point	
	public static void main(String[] args) {	
		TypeIndicator testInstrument = new TypeIndicator();	
	}	
	
	// TypeIndicator(): default constructor	
	public TypeIndicator() {	
		configureAndDisplayGUI();	
	
		introvert = isIntroversionDominant();	
	
		senser = isSensingDominant();	
	
		thinker = isThinkingDominant();	
	
		judger = isJudgingDominant();	
	
		presentResults();	
	}	
	
	// configureAndDisplayGUI(): set up the GUI 	
	private void configureAndDisplayGUI() {	
		// configure elements 	
		window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);	
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
	
		legendArea.setEditable(false);	
		legendArea.setLineWrap(true);	
		legendArea.setWrapStyleWord(true);	
		legendArea.setBackground(window.getBackground());	
	
		statementPad.setVisible(false);	
		statementPad.setEditable(false);	
		statementPad.setHorizontalAlignment(SwingConstants.CENTER);	
		statementPad.setBackground(window.getBackground());	
	
		trueButton.setVisible(false);	
		falseButton.setVisible(false);	
		buttonGroup.add(trueButton);	
		buttonGroup.add(falseButton);	
		buttonGroup.add(hiddenButton);	
	
		resultArea.setEditable(false);	
		resultArea.setBackground(window.getBackground());	
	
		// add elements to window's content pane 	
		window.setLayout(new FlowLayout());	
	
		window.add(legendArea);	
		window.add(statementPad);	
		window.add(trueButton);	
		window.add(falseButton);	
		window.add(resultArea);	
	
		// ready to display GUI 	
		window.setVisible(true);	
	}	
	
	// isIntroversionDominant(): is introversion is dominant to	
	// extroversion	
	private boolean isIntroversionDominant() {	
		String s1 = "I prefer renting a video to going to a party.";	
		String s2 = "I am happy when house guests leave.";	
		String s3 = "Meeting new people makes me tense.";	
		String s4 = "I enjoy being alone.";	
		String s5 = "Crowds suck the life out of me.";	
		return doTraitTest(s1, s2, s3, s4, s5);	
	}	
	
	// isSensingDominant(): is sensing is dominant to intuition	
	private boolean isSensingDominant() {	
		String s1 = "Facts are more interesting than ideas.       ";	
		String s2 = "I need the details more than the big picture.";	
		String s3 = "I always measure, I never estimate.";	
		String s4 = "Seeing is believing.";	
		String s5 = "If you cannot touch it, it's not real.";
		return doTraitTest(s1, s2, s3, s4, s5);	
	}	
	
	// isThinkingDominant(): test whether thinking is dominant to	
	// feeling	
	private boolean isThinkingDominant() {	
		String s1 = "I prefer page 1 to the comics in a newspaper.";	
		String s2 = "I think therefore I am.";	
		String s3 = "I am not an emotional person.";	
		String s4 = "Tears will not cause me to change my mind.";	
		String s5 = "I'd rather be Data than Troi.";
		return doTraitTest(s1, s2, s3, s4, s5);	
	}	
	
	// isJudgingDominant(): is judging is dominant to perceiving	
	private boolean isJudgingDominant() {	
		String s1 = "It is easy to make a decision.";	
		String s2 = "My first impressions are usually right.";	
		String s3 = "Wrong decisions are better than none.";	
		String s4 = "I seldom have second thoughts.";	
		String s5 = "Uncertainty makes me uncomfortable.";	
		return doTraitTest(s1, s2, s3, s4, s5);	
	}	
	
	// doTraitTest(): test for dominant response for a type category	
	private boolean doTraitTest(String s1, String s2, String s3, 	
			String s4, String s5) {	
		// create running totals for responses	
		int choiceTrue = 0;	
		int choiceFalse = 0;	
	
		// pose queries and get responses	
		if (getTrueorFalseResponse(s1)) {  // do query 1	
			++choiceTrue;	
		}	
		else {	
			++choiceFalse;	
		}	
	
		if (getTrueorFalseResponse(s2)) {  // do query 2	
			++choiceTrue;	
		}	
		else {	
			++choiceFalse;	
		}	
	
		if (getTrueorFalseResponse(s3)) {  // do query 3	
			++choiceTrue;	
		}	
		else {	
			++choiceFalse;	
		}	
	
		if (getTrueorFalseResponse(s4)) {  // do query 4	
			++choiceTrue;	
		}	
		else {	
			++choiceFalse;	
		}	
	
		if (getTrueorFalseResponse(s5)) {  // do query 5	
			++choiceTrue;	
		}	
		else {	
			++choiceFalse;	
		}	
	
		// produce test result	
		return choiceTrue > choiceFalse;	
	}	
	
	// getTrueorFalseResponse(): pose a statement and get response	
	private boolean getTrueorFalseResponse(String statement) {	
	
		statementPad.setText(statement);	
		hiddenButton.setSelected(true);	
		statementPad.setVisible(true);	
		trueButton.setVisible(true);	
		falseButton.setVisible(true);	
	
		for (; ;) {	
			if (trueButton.isSelected() || falseButton.isSelected()) {	
				break;	
			}	
		}	
			
		statementPad.setVisible(false);	
		trueButton.setVisible(false);	
		falseButton.setVisible(false);	
			
		return trueButton.isSelected();	
	}	
	
	// presentResults(): display testing results	
	private void presentResults() {	
		// setup text	
		String result = "Your type is\n"	
			+ "    "	
	
			+ ((introvert) ? "I" : "E")	
			+ ((senser)    ? "S" : "N")	
			+ ((thinker)   ? "T" : "F")	
			+ ((judger)    ? "J" : "P")	
			+ "\n"	
	
			+ "where\n"	
			+ "* E = extroversion \n"	
			+ "* I = introversion \n"	
			+ "* S = sensing \n"	
			+ "* N = intuitive \n"	
			+ "* T = thinking \n"	
			+ "* F = feeling \n"	
			+ "* J = judgment \n"	
			+ "* P = perception \n"	
			+ "\n"	
	
			+ "Thank you for trying the tool.";	
	
		// display text and quit button	
		resultArea.setText(result);	
			
		window.setVisible(true);	
	}	
}	
	
