// herbruikbare package, D&D 5, blz 871,...

package be.hogent.h17;

import java.io.*;

public class H17_RandomAccessAccountRecord extends H17_AccountRecord {
	
	public static final int SIZE = 72;
	
	public H17_RandomAccessAccountRecord()
	{
		this (0,"","",0.0);
	}
	
	public H17_RandomAccessAccountRecord(int account, String firstName, String lastName, double balance) {
		super (account, firstName, lastName, balance);
	}
	
	public void read(RandomAccessFile file) throws IOException {
		setAccount ( file.readInt() );
		setFirstName ( readName( file ) );
		setLastName ( readName( file ) );
		setBalance( file.readDouble() );
	}
	
	private String readName(RandomAccessFile file) throws IOException {
		char name[] = new char[15], temp;
		
		for (int count = 0; count < name.length; count++) {
			temp = file.readChar();
			name[count] = temp;
		}
		
		return new String(name).replace('\0',' ');
	}
	
	public void write (RandomAccessFile file) throws IOException {
		file.writeInt(getAccount());
		writeName(file, getFirstName());
		writeName(file, getLastName());
		file.writeDouble(getBalance());
	}
	
	private void writeName(RandomAccessFile file, String name) throws IOException {
		StringBuffer buffer = null;
		
		if (name != null)
			buffer = new StringBuffer(name);
		else
			buffer = new StringBuffer(15);
		
		buffer.setLength(15);
		file.writeChars(buffer.toString());
	}
	
}
