import java.net.URL;
import java.awt.*;
import java.sql.*;
import java.util.*;

/************************************************************************
 * a multiple choice exam application that relies on a database for the 
 * questions, choices, and answers. The database schema is as follows....
 * 
 * |Field Name	 | Datatype	        | Comments
 * ---------------------------------------------------------------------
 * |Question     | Text                 | Primary key
 * |choice1	 | Text			|
 * |choice2	 | Text			|
 * |choice3	 | Text			|
 * |choice4	 | Text			|
 * |answer	 | Integer		|
 ***********************************************************************/

class Exam extends Frame {
	 
	 Exam(String title){
	  		 super(title);
	 }

     public static void main (String args[]) {
	 		 Exam exam = new Exam("Sample Exam");
			 exam.init();
			 exam.pack();
			 exam.resize(300,200);
			 exam.show();
	 }

	 public void init()	 {
			 //Create URL String
			 TRUE = new Boolean("true");
			 FALSE = new Boolean("false");
			 currentCard=0;

             try {
                     // Load the jdbc-odbc bridge driver
					 // Attempt to connect to the driver
                     // Create a statement Object
					 // Submit a query, creating a ResultSet object
					 
					 questionCards = new Panel();
			 		 questionCards.setLayout(new CardLayout());
					 // Pass in your ResultSet Object to createExam
					 // createExam (myResultSetObject);
					 // close the result set
					 // close the statement
					 // close the connection
             }
             catch (SQLException ex) {
			 
			 System.out.println ("\n*** SQLException caught ***\n");

             while (ex != null) {
                     System.out.println ("SQLState: " +
                                     ex.getSQLState ());
                     System.out.println ("Message:  " + ex.getMessage ());
                     System.out.println ("Vendor:   " +
                                     ex.getErrorCode ());
                     ex = ex.getNextException ();
                     System.out.println ("");
                     }
             }
             catch (java.lang.Exception ex) {

                     // Got some other type of exception.  Dump it.
                     ex.printStackTrace ();
             }

			 //Initialize the interface
			 
			 
			 setLayout(new BorderLayout());
			 
			 add("Center",questionCards);
			 buttonPanel=new Panel();
			 buttonPanel.setLayout(new FlowLayout());
			 buttonPanel.add(new Button("previous"));
			 buttonPanel.add(new Button("next"));
			 buttonPanel.add(new Button("exit"));
			 add("South",buttonPanel);
			 
     }

	 public boolean action(Event evt, Object arg) {
	    //grade the answer.
		Checkbox checked;

		if (evt.target instanceof Checkbox) {
			checked = (Checkbox)evt.target;
			System.out.println("Current card=" + checked.getLabel() + "," + answer.elementAt(currentCard) + "," + currentCard );
			System.out.println("Correct?"+correct.elementAt(currentCard));
			if (checked.getLabel().equals(answer.elementAt(currentCard))) correct.setElementAt(TRUE,currentCard);
			else correct.setElementAt(FALSE,currentCard);
			
		}
		else if ("next".equals(arg)){
			((CardLayout)questionCards.getLayout()).next(questionCards);
			currentCard++;
			if (currentCard>5)currentCard=0;
		}
		else if ("previous".equals(arg)) {
			((CardLayout)questionCards.getLayout()).previous(questionCards);
			currentCard--;
			if (currentCard<0)currentCard=answer.size(); 
		}
		else if ("exit".equals(arg)) {
			ResultsDialog rd=new ResultsDialog(this, correct);
		}
		return true;
    }

     //-------------------------------------------------------------------
     // createExam Creates a cardLayout of gui components that represent 
	 //            the exam in the database.
     //-------------------------------------------------------------------

     private void createExam (ResultSet rs) throws SQLException
     {
             int i=0;
			 CheckboxGroup cg;
			 answer = new Vector();
			 correct = new Vector();
			 
             boolean more = rs.next ();
             while (more) {
                     //build a vector of GUI components for the question questionCards
					 Panel questionPanel = new Panel();
					 questionPanel.setLayout(new GridLayout(5,1));
					 					 
					 cg=new CheckboxGroup();
					 //call getString( corresponding columnname) on your result set object
					 //in the following constructors note: rs is the name of the result set
					 //object in this context.
					 questionPanel.add(new WrappingLabel(/*insert here*/));
					 questionPanel.add(new Checkbox(/*insert here*/));
					 questionPanel.add(new Checkbox(/*insert here*/));
					 questionPanel.add(new Checkbox(/*insert here*/));
					 questionPanel.add(new Checkbox(/*insert here*/));
					 answer.addElement(new String(/*insert here*/));
					 correct.addElement(FALSE);
					 questionCards.add(String.valueOf(i++), questionPanel);
                     more = rs.next ();
             }

     }

	 Panel questionCards, buttonPanel;
	 Vector answer, correct;
	 int currentCard;
	 Boolean TRUE, FALSE;
	 Button nextButton, previousButton;
}






