import java.sql.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.util.*;

public class ratings {
	
	
	public static void main (String[] args) throws Exception
       {
       	
       	
       	String voti,rank,movie,year;
        FileWriter fi = new FileWriter("G:\\Documents and Settings\\cristiano\\Desktop\\tesina seminario\\tesina\\finalScripts\\ratings.sql");
		PrintWriter out=new PrintWriter(fi);
	     
       	FileReader f = new FileReader("G:\\Documents and Settings\\cristiano\\Desktop\\tesina seminario\\tesina\\file IMDB\\ratings.txt");
        BufferedReader filebuf = new BufferedReader(f); 
      

        String nextStr;
        nextStr = filebuf.readLine();
        
        while (!nextStr.equals("New  Distribution  Votes  Rank  Title")){
        	//System.out.println(nextStr);
        	nextStr = filebuf.readLine();
        }
        
       
        
        nextStr = filebuf.readLine();
        
       
            
        
       
        
        while (nextStr!=null){
        	
        	if (nextStr.equals("")){
        		nextStr=filebuf.readLine();
        	}
        	
        	 if (nextStr.equals("------------------------------------------------------------------------------")){
        			System.out.println("Ratings inseriti nel db");
					break;
        		}
        
            if (nextStr.equals("BOTTOM 10 MOVIES (650+ VOTES)")){
        			nextStr = filebuf.readLine();
       				nextStr = filebuf.readLine();
       				nextStr = filebuf.readLine();
            	
       			 }
        	 if (nextStr.equals("MOVIE RATINGS REPORT")){
        			nextStr = filebuf.readLine();
       				nextStr = filebuf.readLine();
       				nextStr = filebuf.readLine();
            	
        	}
         
      
   	            
        
 		 
 		 voti=nextStr.substring(16,24);
 		 rank = nextStr.substring(25,30);
 		 movie= nextStr.substring(32);
 		 
 		 rank=rank.replace(" ","");
 		 voti=voti.replace(" ","");
 		 
 		  if (movie.contains("(")){
    		int inizio2=movie.indexOf("(");	
    		int fine2=movie.indexOf(")");
    		year = movie.substring(inizio2+1,fine2);     
   			movie=movie.substring(0,inizio2);
    	
   		 }
   		 else {
   		 	year = null;
   		 } 
   		
		 /*		 
   		 System.out.println(movie);
   		 System.out.println(year);
   		 System.out.println(voti);
  		 System.out.println(rank);
  		 System.out.println("*****************");
 		 */
 		 
 		 		movie=movie.replace("'","");
 		 
 				out.println("UPDATE movies SET votes='"+voti+"',rank='"+rank+"' WHERE title='"+movie+"' and year='"+year+"';");
		       
		     nextStr = filebuf.readLine();// legge una riga del file 
   	
           }
           
           filebuf.close();  // chiude il file 
       		out.close();
       }
		
		
		
		
	}
	
	
	
