import java.io.*;
import java.util.*;
public class chemicalReactionData
{
String s[];
double N[][];
Gas g[];
int natom,ngas;
int nrt,npt;
String reacName;
Atom atomList[];
chemicalReactionData(String si[],double Ni[][]) throws IOException
{
ngas=s.length;
s=new String[ngas];
N=new double[ngas][2];
g=new Gas[ngas];
for(int i=0;i<ngas;i++)
{  s[i]=si[i];
   g[i]=new Gas(s[i]);
}
for(int i=0;i<ngas;i++)
{N[i][0]=Ni[i][0];N[i][1]=Ni[i][1];}
     arrange_atoms();
}

chemicalReactionData(String name) throws IOException
{ File reacFile=new File("Reaction.txt");
  int ngas;
  try
     {    
     BufferedReader cfin=new BufferedReader(new FileReader(reacFile));
     int ierror=1;
     natom=0;
     try
       {
       while(cfin!=null)
         {
         reacName=Text.readString(cfin);
         if(reacName.equals(name)) {ierror=0;break;}
         }
       }
       catch(EOFException e_eof)
         {
         System.out.println("error required gas mixture "+name+" is not found");
         cfin.close();return;
         }
       ngas=Text.readInt(cfin);
       g=new Gas[ngas];
       N=new double[ngas][2];
       nrt=0;
       npt=0;
       String tempGasName;
       Double n1,n2;
       for(int i=0;i<ngas;i++)
         {
         tempGasName=Text.readString(cfin);
         N[i][0]=Text.readDouble(cfin);
         N[i][1]=Text.readDouble(cfin);
         nrt+=N[i][0];
         npt+=N[i][1];
         g[i]=new Gas(tempGasName);
         }
       }
     catch(FileNotFoundException fnfe) {System.out.println("File Not Found");}
     arrange_atoms();   //end of constructor
}
     public void arrange_atoms() throws IOException
     {
     // atomic structure of the reactants
     int i,j;
        for(i=0;i<ngas;i++)
        {for(j=0;j<g[i].natom;j++)
	     {add_atom(i,j);}
        }
     }

     public int  add_atom(int i,int j) throws IOException
     {
     //determine and order the atomic structure
     int k;
       for(k=0;k<natom;k++)
       {
       if(g[i].atomList[j].symbol.equals(atomList[k].symbol))
         {
         atomList[k]=new Atom(atomList[k].symbol,atomList[k].N+g[i].atomList[j].N*N[i][0]);
         return 1;
         }
       }
     Atom atomL[];
     atomL=new Atom[natom+1];
     for(k=0;k<natom;k++)
       atomL[k]=new Atom(atomList[k]);
     atomL[natom]=new Atom(g[i].atomList[j].symbol,g[i].atomList[j].N*N[i][0]);
     atomList=atomL;
     natom+=1;
     return 2;
     }
     public String toString()
     {
       // writes chemical reaction in chemistry norm
       String s="";
       for(int i=0;i<ngas;i++)
       {
       if(N[i][0]==1)
           { s+=g[i].toString(); }
       else if(N[i][0]!=0)
           { s+=N[i][0]+" "+g[i].toString(); }
       if(i!=(ngas-1) && N[i][0]!=0 ) s+=" + ";
       }
       if(s.endsWith(" + "))
         s=s.substring(0,s.length()-3);
       s+=" --> ";
       for(int i=0;i<ngas;i++)
       {
       if(N[i][1]==1)
           { s+=g[i].toString(); }
       else if(N[i][1]!=0)
           { s+=N[i][1]+" "+g[i].toString(); }
       if(i!=(ngas-1) && N[i][1]!=0 ) s+=" + ";
       }
       if(s.endsWith(" + "))
         s=s.substring(0,s.length()-3);
     return s;
     }
}
