/**          A faily complex pass-fail mark processor program
 *           to demonstrate several types of conditionals
 *
 *           Note uses while loop (see next section for details).
 */

import uk.ac.ed.ph.sciprog.*;

public class PassFail {

    public static void main(String args[]) {

	//      Create Dsiplay and three Inputs

	Display panel = new Display("Pass - Fail demo");
	Input passMarkInput = new Input("Pass mark",40);
	Input markInput = new Input("Mark",0);
	Input endInput = new Input("At the end ",false);

	panel.addInput(passMarkInput);    // Add ONLY passMarkinput here

	boolean first = true;             // First execution
	boolean atTheEnd = false;         // At end of the data

	//                   Variables

	int passMark = 0;
	int fails = 0;
	int passes = 0;
	int papers = 0;

	while(!atTheEnd) {                // Loop til at end (see later)
	    panel.waitForButtonPress();

	    if (first) {             // First pass only execute once
		
		passMark = passMarkInput.getInt();  // Read pass mark
		panel.removeInput(passMarkInput);   // Remove input field
		panel.addInput(markInput);          // Add mark input field
		panel.addInput(endInput);           // Add end input field
		first = false;                      // set first flag to false
		panel.println("Enter marks");       // Print message
	    }
	    else {                                   // Not first,process marks
		atTheEnd = endInput.getBoolean();    // Read boolean flag
	
		if (atTheEnd) {                     // Are we finsihed
		    
		    // Print out the results
		    panel.println("Total number of papers : " + papers);
		    panel.println("Number of passes : " + passes);
		    panel.println("Number of fails  : " + fails);
		}
		else {                                  // Process the mark
		    int mark = markInput.getInt();      // Read the mark
		    papers++;                           // Total number
		    if (mark >= passMark) {             // is it a pass
			passes++;                       // Pass number
		    }
		    else {                
			fails++;                        // Fail numebr
		    }
		    
		    
		    panel.println("Another mark");     // Another mark
		}
	    }
	}
    }
}
