 /**
 * User: Alex Shpiler
 * CS 3313 - Prog. Languages
 * Java Projext #2
 * Date: Nov 25, 2006
 * Time: 9:13:59 AM
 * Compiled with: 1.5.0_08
 */

import java.io.*;
import java.util.Vector;
import java.util.HashSet;
import static java.util.Collections.*;
import static java.lang.Integer.*;
import static java.lang.System.*;
import java.text.MessageFormat;



 public class Proj2{

     private static Vector<Integer> v1 = new Vector<Integer>();
     private static Vector<Integer> v2 = new Vector<Integer>();

     public Vector<Integer> getV1() {
         return v1;
     }

     public Vector<Integer> getV2() {
         return v2;
     }

     Proj2(){}

     public static void main(String args[])
     {

         Proj2 a = new Proj2();
         String file1;
         String file2;
         String file3;

         BufferedReader br = new BufferedReader(new InputStreamReader(in));

         try {
             out.print("Enter first file: ");
             file1 = br.readLine();
             out.print("Enter second file: ");
             file2 = br.readLine();

             a.openFile1(file1,a);
             a.openFile2(file2,a);

             Vector<Integer> v3 = getIntersection(a);

             out.print("Enter output file: ");
             file3 = br.readLine();
             a.outputToFile(v3,file3);

             br.close();
             in.close();

         } catch (IOException e) {
             e.printStackTrace();

         } finally
         {
             try {
                 br.close();
                 in.close();
             } catch (IOException e) {
                 out.println("Exception: " + e.toString());
             }
         }
     }




     private void outputToFile(Vector<Integer> v3, String file3)
     {
         File f = new File(file3);
         try
         {
             PrintStream s = new PrintStream(f);
             sort(v3);

             for(Integer aV3 : v3) {
                 s.println(aV3);
             }

         } catch (FileNotFoundException e) {
             out.println(MessageFormat.format("File: {0} was not found in the current directory", file3));
         }

     }


     private static Vector<Integer> getIntersection(Proj2 a)
     {
         HashSet<Integer> hash = new HashSet<Integer>();
         for(int x = 0; x < a.getV1().size(); x++)
         {
             for(int y = 0; y < a.getV2().size(); y++)
             {
                if(a.getV1().elementAt(x).equals(a.getV2().elementAt(y)))
                {
                    hash.add(a.getV1().elementAt(x));
                }
             }
         }
         return new Vector<Integer>(hash);
     }


     void openFile1(String file1, Proj2 a)
     {
         File f = new File(file1);

         try
         {
             BufferedReader in = new BufferedReader(new FileReader(f));
             String mark;
             int x;

             while((mark = in.readLine()) != null)
             {
                 if (mark.equals(""))
                     break;
                 x = parseInt(mark);
                 a.getV1().add(x);
             }

             in.close();

         } catch (FileNotFoundException e)
         {
             out.println(MessageFormat.format("Filename: {0} does not exist the correct directory.", file1));

         } catch(IOException e)
         {
             out.println("Error reading input");
         }
     }


     void openFile2(String file2, Proj2 a)
     {
         File f = new File(file2);

         try
         {
             BufferedReader in = new BufferedReader(new FileReader(f));
             String mark;
             int x;

             while((mark = in.readLine()) != null)
             {
                 if (mark.equals(""))
                     break;
                 x = parseInt(mark);
                 a.getV2().add(x);
             }
             in.close();
         } catch (FileNotFoundException e)
         {
             out.println(MessageFormat.format("Filename: {0} does not exist the correct directory.", file2));
         } catch(IOException e)
         {
             out.println("Error reading input");
         }
     }

 }




