import java.util.TreeSet;
import java.util.Scanner;
import java.io.File;

/**
 * Class for reading ISBN codes from a file and classifying them.
 *   @author davereed
 *   @version 9/09/11
 */
public class ISBNChecker {
    public TreeSet<ISBN13> validCodes;
    public TreeSet<ISBN13> invalidCodes;

    /**
     * Constructs an ISBNChecker object.
     */
    public ISBNChecker() {
        this.validCodes = new TreeSet<ISBN13>();
        this.invalidCodes = new TreeSet<ISBN13>();
    }

    /**
     * Reads and classifies the codes from a file.
     * @param filename the name of the file to be processed.
     */
    public void readFile(String filename) {
        try {
            Scanner infile = new Scanner(new File(filename));
            while (infile.hasNextLine()) {
                ISBN13 code = new ISBN13(infile.nextLine());
                if (code.isValid()) {
                    validCodes.add(code);
                }
                else {
                    invalidCodes.add(code);
                }
            }
        }
        catch (java.io.FileNotFoundException e) {
            System.out.println("FILE NOT FOUND");
        }
    }

    /**
     * Displays all of the classified codes, grouped into VALID and INVALID.
     */
    public void displayAll() {
        System.out.println("VALID");
        for (ISBN13 code : validCodes) {
            System.out.println(code);
        }
        System.out.println();
        System.out.println("INVALID");
        for (ISBN13 code : invalidCodes) {
            System.out.println(code);
        }
    }

    /**
     * Main method for the project - prompts the user for the file name and 
     * constructs an ISBNChecker to read and process the codes.
     *   @param args command-line arguments (ignored)
     */
    public static void main(String[] args) {
        System.out.println("Enter the file name: ");
        Scanner input = new Scanner(System.in);
        String filename = input.next();
        
        ISBNChecker checker = new ISBNChecker();
        checker.readFile(filename);
        checker.displayAll();
    }
}
