import javax.swing.JOptionPane;

public class FactCalculator {

    public static void main(String [] args) {

	String argument, result;

	while (true) {

	    result = ""; 
	    argument = JOptionPane.showInputDialog(null, "Enter an integer");

	    try {

		// throws NoArgException
		if (argument == null ) throw new NoArgException();

		// throws NumberFormatException
		int n = Integer.parseInt(argument);

		// throws IllegalArgumentException
		result = n + "! = " + Factorial.factorial(n);

	    } catch (NumberFormatException e) {
		JOptionPane.showMessageDialog(null,
			"The argument you specify must be an integer",
			"Warning", JOptionPane.WARNING_MESSAGE);

	    } catch (IllegalArgumentException e) {
		JOptionPane.showMessageDialog(null,
			"Bad argument: " + e.getMessage(),
			"Warning", JOptionPane.WARNING_MESSAGE);
	    } catch (NoArgException e) {
		// ignore
	    }

	    int choice = JOptionPane.showConfirmDialog(null,
		    result + "\nDo you wish to continue?", "Result",
		    JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

	    if (choice == JOptionPane.NO_OPTION) {
		break;
	    }

	} 
	System.exit(0);
    }
}


class NoArgException extends Exception {
    public NoArgException(String s) {super(s);}
    public NoArgException() {super();}
}
