import java.io.*;

public class GoogleQuestion2 {
    
    public static void main(String[] args) {
        // Setup rules
        Rule[] rule = new Rule[2];
        rule[0] = new Rule(4, "foo", "js");
        rule[1] = new Rule(4, "vwx", "xml");
        
        // Search through files...
        for (int i = 0; i < rule.length; i++)
            search(new File("c:\\google_q2"), rule[i]);
        
        // calculate total
        int total = rule[0].getScore(); // set to first score
        for (int i = 1; i < rule.length; i++) // then multiply by rest
            total *= rule[i].getScore();
        
        // Display the total value
        System.out.println(total);
    }
    
    // recursively search through the directory structure
    public static void search(File f, Rule r) {
        if (f.isDirectory()) { // its a directory,.. run each child through this function
            File[] kids = f.listFiles(r);
            for (int i = 0; i < kids.length; i++)
                search(kids[i], r);
        } else { // now its a file
            r.score(f); // try score on this file
        }
    }
    
}

// abstraction template of the rules. These rules also keep track of their score.
class Rule implements java.io.FileFilter {
    
    private int line;
    private String contains;
    private String endsIn;
    
    private int value; // the score that is added to when a match is found
    
    public Rule(int line, String contains, String endsIn) {
        this.line = line;
        this.contains = contains;
        this.endsIn = endsIn;
        value = 0;
    }
    
    // from FileFilter
    public boolean accept(java.io.File f) {
        if (f.isDirectory()) // always accept directories
            return true;
        else // now we have the full file path,.. search for two parameters specified by rule
            return ((f.getAbsolutePath().indexOf(contains) > -1) && (f.getAbsolutePath().endsWith("." + endsIn)));
    }
    
    // try get the value from the file. update local value and return it.
    public int score(java.io.File f) {
        int temp = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            String read = null; // null because it might not have been set otherwise,... rofl.
            for (int c = 0; c < line; c++)
                read = br.readLine();
            if (read != null) // if there were in fact required number of lines
                temp = Integer.parseInt(read);
        } catch (IOException ioe) {
            System.out.println("darn. i think i err'd.");
        }
        value += temp; // increment local counter
        return temp; // return to caller,.. just incase they want it.
    }
    
    public int getScore() {
        return value; // duh
    }
    
}