// Oefening CreateRandomFile, D&D 5, blz 872,..

import java.io.*;
import javax.swing.*;

import be.hogent.h17.H17_RandomAccessAccountRecord;

public class H17_CreateRandomFile {
	
	private static final int NUMBER_RECORDS = 100;
	
	private void createFile() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		
		int result = fileChooser.showSaveDialog(null);
		
		if (result == JFileChooser.CANCEL_OPTION)
			return;
		
		File fileName = fileChooser.getSelectedFile();
		
		if (fileName == null || fileName.getName().equals(""))
			JOptionPane.showMessageDialog(null, "Ongeldige bestandsnaam","Ongeldige bestandsnaam", JOptionPane.ERROR_MESSAGE);
		else {
			try {
				RandomAccessFile file = new RandomAccessFile(fileName, "rw");
				
				H17_RandomAccessAccountRecord blankRecord = new H17_RandomAccessAccountRecord();
				
				for (int count=0; count < NUMBER_RECORDS; count ++)
					blankRecord.write(file);
				
				file.close();
				
				JOptionPane.showMessageDialog(null, "Bestand " + fileName + " aangemaakt", "Status", JOptionPane.INFORMATION_MESSAGE);
				
				System.exit(0);
			}
			catch (IOException ioException) {
				JOptionPane.showMessageDialog(null, "Fout bij het verwerken","Error", JOptionPane.ERROR_MESSAGE);
				
				System.exit(1);
			}
		}
	}
	
	public static void main(String args[]) {
		H17_CreateRandomFile application = new H17_CreateRandomFile();
		application.createFile();
	}
}
