import java.util.*;
import java.io.*;
/**
 *  Inventory class uses arrays to track multiple types of products
 *
 * @author Kathryn
 * @version March 2007
 * 
 * Today: 
 * 1) Review files
 * 2) Try / Catch for errors
 * 3) other File actions (getAbsolutePath, isDirectory, length, canRead, canWrite)
 * 
 */


public class Inventory
{
    public static void main(String[] args) throws Exception
    {
        Scanner stdin = new Scanner (System.in);
        Scanner gFile = null;
        File f = null;
        // File format: first tells us the number of different Items
        // Each name and amount are on a line by themselves
        
        while (gFile == null) {
            try {
                System.out.print("Enter the input file name: ");
                f = new File(stdin.nextLine());
                gFile = new Scanner(f);
            } catch (FileNotFoundException e) {
                System.out.println("File not found.");   
            }
        }
        System.out.println("Absolute path name is: " + f.getAbsolutePath());
        System.out.println("File is readable? " + f.canRead());     
        System.out.println("File is a directory? " + f.isDirectory());  

        int size = gFile.nextInt();
        gFile.nextLine();
        Items[] apples = new Items[size];
        
        // Read in new items
        for (int i = 0; i < apples.length; ++i) {
            String aName = Items.getName(gFile, "apples");
            int amount = Items.getPositiveInteger(gFile, aName);
            apples[i] = new Items(aName, amount);
        }
        // Close the Scanner object when you are through with it
        gFile.close();
        
        // Try deleting the file! and then see if it exists
        // f.delete(); 
        
        // Print out what we read in
        for (int i = 0; i < apples.length; i++) {
            System.out.println(apples[i].toString());
            
        }

        
    }
}
