import javax.swing.JOptionPane; //to view have thai uses front CordiaUPC 14 
class  DeadLock_Holy
{
	public static void main(String[] args) 
	{
		int ch,resauce,process,i,ch_ar_stop,ch_AR,j,k,jj,kk;
		String chStr[] = new String[20],resauceStr,processStr,reStr[] = new String[20],prStr[] = new String[20],AR[] = new String[20],begin;
		Boolean ch_ar_stop99;

		System.out.println("This program to Check DeadLock\nWrite by  HolyTyrael\nConcept : Input and Check the String\n\nInput 0 to Exit!\n");
		JOptionPane.showMessageDialog(null,"This is program to Check the DeatLock");

/*
//input how many resauce and view in terminol
		resauceStr = JOptionPane.showInputDialog("How many Resause");
		resauce = Integer.parseInt(resauceStr);
		if (resauce==0)
		{
			System.out.println("\n\nProgram is Exit!\n");
			System.exit (0);
		}
		System.out.print("Create Resauce : ");
		for (i=1;i<=resauce ;i++ )
		{
			reStr[i] = "R" + i;
			System.out.print(reStr[i]+" ");
		}
		System.out.println("");

//input  how many process and view in terminol
		processStr = JOptionPane.showInputDialog("How many Process");
		process = Integer.parseInt(processStr);
		if (process==0)
		{ 
			System.out.println("\n\nProgram is Exit!\n");
			System.exit (0);
		}
		System.out.print("Create Procrss : ");
		for (i=1;i<=process ;i++ )
		{
			prStr[i] = "P" + i;
			System.out.print(prStr[i]+" ");
		}
		System.out.println("");
*/

//input Allocation and Requase, 99 to stop input, 0 to Exit!
System.out.println("\nInput Allocation and Reqause\nFormat : r1p2 , p7r8\nInput 99,999,9999,99999,999999 to Stop Input");
ch_ar_stop = 999;
i = 0;
while (ch_ar_stop != 99)
{
	i=i+1;
	AR[i] = JOptionPane.showInputDialog("Input Allocation or Request\nPlease input in format, The format is show in terminol");
	ch_AR = AR[i].indexOf("99");
	//System.out.println(ch_AR);
	if (ch_AR == 0)
	{
		ch_ar_stop = 99;
	}
	ch_AR = AR[i].indexOf("0");
	if (ch_AR == 0)
	{
		System.out.println("\n\nProgram is Exit!\n");
		System.exit (0);
	}
	//System.out.println(AR[1]);
}//end while

i=i-1;//num of line : AR[]

SubDeadlock_Holy point = new SubDeadlock_Holy();

	for (int ss=1;ss<=i ;ss++ )
	{
	point.rcvARStr(AR[ss]);
	}

String Result = point.findDeadlock();

if ( (SubDeadlock_Holy.selectResult == 1) /*&& ( (SubDeadlock_Holy.Str_of_DeadLock).length() >= 19)*/ )//Have DeadLock
{
JOptionPane.showMessageDialog(null,Result+"\nDeadLock is : "+SubDeadlock_Holy.Str_of_DeadLock);
}

if (SubDeadlock_Holy.selectResult == 0)
{
JOptionPane.showMessageDialog(null,Result);
}
/*
//find deadlock
int chForj = 1;

	for (j=chForj;j<=9999 ;j++ )//find p1...pn
	{
	if (AR[j].indexOf("p")==0 || AR[j].indexOf("P")==0)//find p begin
	{
		begin = AR[j].substring(0,2);
		System.out.println(begin);
		break;
	}
	}//end for j
	//System.out.println(j);
		for (k=1;k<=i ;k++ )
		{
		if ((AR[j].substring(2,4)).equalsIgnoreCase(AR[k].substring(0,2)))
		{
			System.out.println("Find Link");//k = point to find
			break;
		}

}//end for k
*/

System.out.println("\n\nProgram is Exit!\n");
System.exit (0);
	}//main
}//class
