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

/**
 * MoneyReadFile reads money information stored in a text file
 * @author Fred Kral
 * @version 1.0
 */

public class MoneyReadFile {
	
	private String fileName = null;	
	private File file;
	
	/* Constructor */
	
	/**
	 * Constructor and set the file name.
	 * @param inputFileName The file name to read, a String.
	 */
	public MoneyReadFile(String inputFileName) {
		fileName = inputFileName;
		System.out.println("MoneyReadFile fileName = " + fileName);
		file = new File(fileName);
	}
	
	/**
	 * Use the input file to read the money records.
	 * The records should match the BankAccount, Wallet, etc. classes.
	 *
	 */
	public void readMoneyRecords() {
				
		/* Read each line in a file and store tokens in variables */
		/* This assumes a specified format */
		try {
			
			Scanner scanner = new Scanner(file);
			
			/* Scan first line, if it exists */
			if (scanner.hasNextLine()) {
				String firstline = scanner.nextLine();
				System.out.println("Title = " + firstline);
			}
			else {
				System.out.println("File is Empty");
			}
			
			/* Scan subsequent lines in the file */
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine();
				
				/* Scan contents of each line with a new Scanner */
				Scanner scanstr = new Scanner(line);
				
				/* Variables expected to be in file */
				String first = "";
				String last = "";
				String accountnum = "";
				double balance = 0.0;
				
				boolean scanOK = true; // true if all tokens found
				
				if (scanstr.hasNext()) 
					first = scanstr.next();
				else
					scanOK = false;
				if (scanstr.hasNext()) 
					last = scanstr.next();
				else
					scanOK = false;
				if (scanstr.hasNext()) 
					accountnum = scanstr.next();
				else
					scanOK = false;
				if (scanstr.hasNextDouble())
					balance = scanstr.nextDouble();
				else
					scanOK = false;
				
				/* This is where you do something with the variables */
				
				if (scanOK) {
					System.out.println("scanned " + first + " " + last +
									   " " + accountnum + " " + balance);
				}
				else {
					System.out.println("Error scanning " + line);
				}
				
				
				scanstr.close();
				
			}
			scanner.close();
		} catch (FileNotFoundException e) {
			System.out.println("ReadMoneyRecords - File Not Found");
			e.printStackTrace();
		}
	}
	
}