import java.io.*;
import java.util.StringTokenizer;
public class MiniDrugs
{
public File drugList = new File("drugs.txt");
public File patientList = new File("patients.txt");
public StringTokenizer tokenizer; 

public int Scores[] = new int[100];
public String Drugs[] = new String[100];
public int sizeOfDrugList=0;

public int x=0; // generic looper
public String lname, fname, drug1, drug2;

public static void main(String args [] )
   {
   new MiniDrugs(); 
   }

public MiniDrugs()
   {
	createMasterDrugList();
	calculatePatientScores();
   listDrugUsers();
   }

public void createMasterDrugList()
{
try
   {
	BufferedReader drugInput = new BufferedReader(  new FileReader( drugList ) );
	while( drugInput.ready() )
	   { 
      tokenizer = new StringTokenizer(drugInput.readLine());
      Drugs[x] = tokenizer.nextToken();
      Scores[x] = Integer.parseInt( tokenizer.nextToken() );
      x++; 
      } // end while drugs
   drugInput.close();
   } // end try
catch( IOException e)
   {
	System.out.println("Bad File");
   System.exit(0);
   }  // end catch 	 

sizeOfDrugList = x;

// --------------------------------------------------------------------
boolean found1=false, found2=false;
try
   {
   BufferedReader patientInput = new BufferedReader(  new FileReader( patientList ) );
		
	while( patientInput.ready() )
	   { 
		tokenizer = new StringTokenizer(patientInput.readLine());      
	   lname = tokenizer.nextToken();
      fname = tokenizer.nextToken();
	   drug1 = tokenizer.nextToken();
	   drug2 = tokenizer.nextToken();
	
	   // is either drug1 or drug2 in the master drug list?
	   for( x=0; x<sizeOfDrugList; x++ )
	      {
			if ( drug1.equals( Drugs[x] ) )
			   {
				found1 = true;
	         }
			if ( drug2.equals( Drugs[x] ) )
			   {
				found2 = true;
	         }
			} // end for	
		
		// if not, add them to Drugs[] and Scores[]
		if (found1 == false)
		   {
			Drugs[ sizeOfDrugList ]= drug1;
			Scores[ sizeOfDrugList ] = 10;
		   sizeOfDrugList++;
			}		 
		if (found2 == false)
		   {
			Drugs[ sizeOfDrugList ]= drug2;
			Scores[ sizeOfDrugList ] = 10;
		   sizeOfDrugList++;
			}		 
		found1 = false;
		found2 = false;
      } // end while patients

	patientInput.close();
   } // end try 
catch( IOException e)
   {
	System.out.println("Bad File");
	System.exit(0);
   }  // end catch 	     patientInput.close(); 

// print the results
System.out.println(sizeOfDrugList + " drugs / scores: " );
for( x=0; x<sizeOfDrugList; x++)
   {
	System.out.println( "  " + Drugs[x] + " / " + Scores[x] );
   }
System.out.println("****************************************\n");
	 	  
} // end createMasterDrugList


public void calculatePatientScores()
{
int patientScore = 0;
try
   {
   BufferedReader patientInput = new BufferedReader(  new FileReader( patientList ) );
	
	while( patientInput.ready() )
	   { 
		tokenizer = new StringTokenizer(patientInput.readLine());      
		lname = tokenizer.nextToken();
      fname = tokenizer.nextToken();
	   drug1 = tokenizer.nextToken();
	   drug2 = tokenizer.nextToken();
      patientScore = 0;
		
      for( x=0; x<sizeOfDrugList; x++ )
         {
    	   if ( Drugs[x].equals( drug1 ) || Drugs[x].equals( drug2  ) ) 
			   {
            patientScore = Scores[x] + patientScore;			
				} 				
		   } // end while drugs		
        
		 System.out.println( fname + " " + lname + " score: " + patientScore);
		 } // end while patients

    patientInput.close();
    } // end try
catch( IOException e)
    {
	 System.out.println("Bad File");
	 System.exit(0);
    }  // end catch 	 
System.out.println("****************************************\n");

} // end listDrugUsers


public void listDrugUsers()
{
try
   {	
   for( x=0; x< sizeOfDrugList; x++)
	   { 
		System.out.println( Drugs[x] );
      BufferedReader patientInput = new BufferedReader(  new FileReader( patientList ) );
		
		while( patientInput.ready() )
	      { 
		   tokenizer = new StringTokenizer(patientInput.readLine());      
		   lname = tokenizer.nextToken();
         fname = tokenizer.nextToken();
	      drug1 = tokenizer.nextToken();
	      drug2 = tokenizer.nextToken();
			if ( Drugs[x].equals( drug1 ) || Drugs[x].equals( drug2  ))
			   {
            System.out.println("    " + fname + " " + lname );				
				} 
		   } // end while patients		
        
	    patientInput.close(); 
       } // end while drugs
    } // end try
catch( IOException e)
    {
	 System.out.println("Bad File");
	 System.exit(0);
    }  // end catch 	 
System.out.println("****************************************\n");	 
} // end listDrugUsers


} // end class
