//Daniel Fajardo
// this is a program that list a unique random set of numbers from 0-99 in an array of 99
// there are no doubles
import java.util.Random;
import java.util.Scanner;
// Array find number class
public class ArrayFindNum {
	//random number to be generated, and value to be found
	private static double randomNum, toFind;
	//array to hold values
	private static double[] value;
	//constructor
	private ArrayFindNum (double myRandom, double myFind)
	{
		//constructor values
		randomNum = myRandom; 
		toFind = myFind;
		value = new double[100];
	}
	/**
	 * @param args
	 */
	//function to set a random number
	private void setmyRandom()
	{
		randomNum = new Random().nextInt(100);
	}
	//function to get a random number
	private double getMyRandom() 
	{
		return randomNum;
	}
	//function to set user input to search array with
	private void setmyFind()
	{
		String newValue = null;
		System.out.println("please input a number to search for");
		newValue = getInput(newValue);
		toFind = (Double.parseDouble(newValue));
	}
	//function to get the user's input to use
	private double getMyFind()
	{
		return toFind;
	}
	//copy from last assignment
	//function to check if input is a numeric value
	 private static boolean isNumeric(String str)  
		{  
		  try  
		  {  
		    Double.parseDouble(str);  
		  }  
		  catch(NumberFormatException nfe)  
		  {  
		    return false;  
		  }  
		  //if is a numeric value then true, otherwise false
		  return true;  
		}
	 //function to check if number already exists in array
	 //input value is tested only with input amount
	 private double searchArray(double searchValue, double mylength)
	 {
		 //initial value to identify found
		 double foundValue = 0;
		 //if value is found, stop loop and record position found
		 for(int i = 0; i < mylength; i++)
		 {
			 if(value[i] == searchValue)
			 {
				 foundValue = i+1;
				 break;
			 }
			 // if loop reaches length without finding value then found value is 0
			 else if (i == (mylength-1))
			 {
				 foundValue = 0;
			 }
		 }
		 //return found value
		 return foundValue;
	 }
	 //copied from last assignment
	 //function to get user input
	private static String getInput(String newValue)
	 {
		 Scanner input = new Scanner(System.in);
		 newValue = input.nextLine();
		 //call function to check if input is numeric
		 while (isNumeric(newValue) == false)
			{
			 //if input is not, them prompt the user to input a valid number
				System.out.print("Error\n please input valid number \n");
				newValue = input.nextLine();
			}
		return newValue;
	 }
	//maind function
	public static void main(String[] args) {
		//explain purpose of program
		System.out.println("This is a program that generates a unique/random set of numbers from 0-99");
		System.out.println("Then takes a users entry and identifies its position within the array");
		//Create ArrayFindNum Daniel
		ArrayFindNum Daniel = new ArrayFindNum(randomNum, toFind);
		//loop through array length to add values to array
		for (int i = 0; i < Daniel.value.length; i++)
		{
			//generate random number
			Daniel.setmyRandom();
			//if random number already exists within array, set count back one
			if (Daniel.searchArray(Daniel.getMyRandom(), i) != 0)
			{
				//System.out.println("found " + Daniel.getMyRandom() + " @ " + i); <- test for unique-ness
				i--;
			}
			//if random number does not exist within array add to array
			else
			{
			Daniel.value[i] = Daniel.getMyRandom();
			//print value and position
			System.out.printf("%d: %.0f \n", i+1, Daniel.value[i]);
			}
		}
		//while unreachable number 100 is not entered, get selection and find within array
		while (Daniel.getMyFind() != 100)
		{
		System.out.println("Enter 100 to end");
		//get user input
		Daniel.setmyFind();
		//print position input is found @
		System.out.printf("%.0f\n", Daniel.searchArray(Daniel.getMyFind(), Daniel.value.length));
		}
		//if 100 is entered then end program
		System.out.println("Complete");
	}
}
