/*******************************************************************************
 * 
 * Lecture4 program
 * 
 * Purpose:  This program provides useful sample and examples that correspond
 * 			 to Lecture 4.
 * 
 * @author Thomas Rogers
 *
 * @version 1.1 (Feb 8, 2018)      
 * 
 ******************************************************************************/
import java.util.Scanner;

public class Lecture4 {

	/**=========================================================================
	 * CLASS CONSTANTS
	 * 
	 * Constants used throughout this program and their comments.
	 *========================================================================*/
	public static final int DAYS_IN_YEAR = 365; 	// Number of days in a year
	
	
	/**=========================================================================
	 * CLASS VARIABLES
	 * 
	 * Program public variables are listed below along with their comments.
	 *========================================================================*/
	
	
	/**=========================================================================
	 * CLASS METHODS
	 * 
	 * Program methods are listed below in alphabetical order.
	 *========================================================================*/

	/**
	 * Determines if supplied integer is a prime number.
	 * 
	 * @param n		number to check if prime or not
	 * 
	 * @return		true if number is prime else false
	 */
	public static boolean isPrime(int n) {
	    //check if n is a multiple of 2
	    if (n%2==0) return false;
	    //if not, then just check the odds
	    for(int i=3;i*i<=n;i+=2) {
	        if(n%i==0)
	            return false;
	    }
	    return true;
	}
	
	/**
	 * Prints a prompt for user input.
	 */
	public static void printPrompt() {
		System.out.print("\nEnter an integer only or -1 to exit:  ");
	}
	
	/**
	 * Reads the next integer from the console
	 * 
	 * @return int 	The integer entered by the user
	 */
	public static int getInteger( Scanner stdin ) {
		int x = stdin.nextInt();
		return x;
	}
	
	/**
	 * Prints out "Good Bye" message
	 */
	public static void printGoodBye() {
		System.out.println("\nGood bye!");
		
	}
	
	/**=========================================================================
	 * CLASS main
	 * 
	 * @param args - The command-line arguments passed to the running program.
	 *========================================================================*/
	public static void main(String[] args) {
		
		// Is Prime testing
		System.out.println("IsPrime:");
		System.out.println("\t29 = " + isPrime(29));
		System.out.println("\tDAYS_IN_YEAR = " + isPrime(DAYS_IN_YEAR));
		System.out.println("\t12212 = " + isPrime(12212));
		
		// while example - uses methods that return values, conditions, break...
		String str;
		int number;
		Scanner stdin = new Scanner(System.in);
		
		// Get the initial value
		printPrompt();
		number = getInteger( stdin );
		
		// While not equal to -1 
		while (number != -1) {
			
			// Indicate if prime or not
			if ( isPrime(number) ) {
				System.out.println("\nYou entered " + number + " and that number *IS* prime!");
			} else {
				System.out.println("\nYou entered " + number + " and that number *IS NOT* prime!");
			}
			
			// Get number again
			printPrompt();
			number = getInteger( stdin );
						
		} // while

		printGoodBye();
	}

}