/*
 * GPATester.java
 *
 * A student is asked to enter credits earned and gpa.  Based on these
 * values, the student's class year is determined, and whether the student
 * is subject to academic probation, based on the rules in the Gordon
 * College catalog
 *
 * This program demonstrates the use of switch with symbolic constants.
 *
 * copyright (c) 2004 - Russell C. Bjork
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GPATester extends JFrame implements ActionListener
{
	/** Main method for program
	 */
	public static void main(String [] args)
	{
		new GPATester().setVisible(true);
	}
	
	/** Constructor
	 */
	public GPATester()
	{
		getContentPane().setLayout(new GridLayout(0, 2));
		
		// Create the components for entering credits earned.  Pressing
		// enter in the field will invoke the actionPerformed method
		
		getContentPane().add(new JLabel("Credits earned"));
		creditsField = new JTextField(20);
		getContentPane().add(creditsField);
		creditsField.addActionListener(this);
		
		// Create the components for entering gpa.  Pressing
		// enter in the field will invoke the actionPerformed method
		
		getContentPane().add(new JLabel("GPA"));
		gpaField = new JTextField(20);
		getContentPane().add(gpaField);
		gpaField.addActionListener(this);
		
		// Create the components for displaying results
		
		classLabel = new JLabel("");
		getContentPane().add(classLabel);
		probationLabel = new JLabel("");
		getContentPane().add(probationLabel);
		
		pack();
	}
	
	public void actionPerformed(ActionEvent event)
	{
		int credits;
		double gpa;
		int standing;
		
		// Get the credits and gpa entered.  If there are any problems
		// reading them, report a problem and exit this listener
		
		try
		{
			credits = Integer.parseInt(creditsField.getText());
			gpa = Double.parseDouble(gpaField.getText());
		}
		catch(NumberFormatException e)
		{
			probationLabel.setText("Please enter numbers for credits and GPA");
			return;
		}
		
		// Figure out the student's class year
		
		if (credits < SOPHOMORE_CREDITS)
			standing = FRESHMAN;
		else if (credits < JUNIOR_CREDITS)
			standing = SOPHOMORE;
		else if (credits < SENIOR_CREDITS)
			standing = JUNIOR;
		else
			standing = SENIOR;
			
		// Set the labels reporting class standing and status
		
		switch(standing)
		{
			case FRESHMAN:
			
				classLabel.setText("You are a freshman");
				if (gpa < FRESHMAN_MINIMUM_GPA)
					probationLabel.setText("You face probation");
				else if (gpa < ULTIMATE_MINIMUM_GPA)
					probationLabel.setText("OK for now, but ...");
				else
					probationLabel.setText("GPA is adequate");
				break;
				
			case SOPHOMORE:
			
				classLabel.setText("You are a sophomore");
				if (gpa < SOPHOMORE_MINIMUM_GPA)
					probationLabel.setText("You face probation");
				else if (gpa < ULTIMATE_MINIMUM_GPA)
					probationLabel.setText("OK for now, but ...");
				else
					probationLabel.setText("GPA is adequate");
				break;
				
			case JUNIOR:
			
				classLabel.setText("You are a junior");
				if (gpa < ULTIMATE_MINIMUM_GPA)
					probationLabel.setText("You face probation");
				else
					probationLabel.setText("GPA is adequate");
				break;
				
			case SENIOR:
			
				classLabel.setText("You are a senior");
				if (gpa < ULTIMATE_MINIMUM_GPA)
					probationLabel.setText("You face probation");
				else
					probationLabel.setText("GPA is adequate");
				break;
		}
	}
	
	// Minimum credit cutoffs for various standings
	
	public static final int SOPHOMORE_CREDITS = 27;
	public static final int JUNIOR_CREDITS = 56;
	public static final int SENIOR_CREDITS = 86;
	
	// Minimum GPA's for lower division students
	
	public static final double FRESHMAN_MINIMUM_GPA = 1.6;
	public static final double SOPHOMORE_MINIMUM_GPA = 1.8;
	
	// Minimum GPA for graduation
	
	public static final double ULTIMATE_MINIMUM_GPA = 2.0;
	private JTextField creditsField, gpaField;
	private JLabel classLabel, probationLabel;
	
	private static final int FRESHMAN = 1;
	private static final int SOPHOMORE = 2;
	private static final int JUNIOR = 3;
	private static final int SENIOR = 4;
}
				
			