import java.util.Scanner;
import java.io.File;
import java.util.List;
import java.util.ArrayList;

/**
 * Class for reading, storing, and accessing valid codes.
 *   @author Dave Reed
 *   @version 9/19/16
 */
public class ValidCodesA2 {
    private List<String> codes;
    
    /**
     * Constructs a list of valid codes, read in from a file
     *   @param filename the file containing the valid codes, one per line
     */
    public ValidCodesA2(String filename) {
        this.codes = new ArrayList<String>();
        try {
            Scanner infile = new Scanner(new File(filename));
            while (infile.hasNext()) {
                String code = infile.next();
                this.codes.add(code.trim());
            }
            infile.close();
        }
        catch (java.io.FileNotFoundException e) {
            System.out.println("File not found.");
        }        
    }
    
    /**
     * Determines whether the specified code is contained in the valid list.
     *   @param code the code to be searched for
     *   @return true if code is contained in the list of valid codes
     */
    public boolean isValid(String code) {
        return this.codes.contains(code);
    }
}
