import java.io.*;


class Customer
{
	public static final int MaxNameLength=40;
	public static final int CustomerRecordLength=MaxNameLength+4+4;//both int and float occupies 4 bytes.
	public String name;
	public int account;
	public float balance;

	public void display()
	{
		System.out.println("name="+name);
		System.out.println("account="+account);
		System.out.println("balance="+balance+"\n");
	}

};


class CustomerWrapper
{
	
	protected static byte[] buffer=new byte[Customer.MaxNameLength];

	public static Customer readCustomer(RandomAccessFile file)
	{
		Customer result=new Customer();
		try
		{
			file.readFully(buffer, 0, Customer.MaxNameLength);
			result.name= new String(buffer,0,Customer.MaxNameLength);
			result.account=file.readInt();
			result.balance=file.readFloat();	
		}
		catch (Exception er)
		{
			//if (er instanceof EOFException)
			{
				return null;
			}
			// System.out.println(er.getMessage());
		}
		return result;
	}
	protected static void copyString(String str, byte[] buffer)
	{
		int len=str.length();
		
		for (int i=0; i<Customer.MaxNameLength; i++)
		{
			if (i<len)
			{
				buffer[i]=(byte)str.charAt(i);
			}
			else
			{
				buffer[i]=0;
			}
		}
	}

	public static void writeCustomer(RandomAccessFile file, Customer cust)
	{
		try
		{
			copyString(cust.name, buffer);
			file.write(buffer);
			file.writeInt(cust.account);
			file.writeFloat(cust.balance);
		}
		catch (Exception er)
		{
			System.out.println(er.getMessage());
		}

	}		
}

public class RecordDemo
{	
	public static void main(String argv[])
	{		
		String names[]={"ABC", "Dell", "IBM"};
		int numbers[]={1000, 2000, 300};
		float balances[]={100.5f, 3000f, 23004.2f};
		try
		{
			Customer cust[]=new Customer[3];
			for (int i=0; i<3; i++)
			{
				cust[i]=new Customer();
				
				cust[i].name=names[i];
				cust[i].account=numbers[i];
				cust[i].balance=balances[i];
			}
			RandomAccessFile file=new RandomAccessFile("test1.txt", "rw");
			for (int i=0; i<3; i++)
			{
				CustomerWrapper.writeCustomer(file, cust[i]);
			}
			//file.flush();

			//let us read them out one by one
			Customer ptr=null;
			while (true)
			{
				ptr=CustomerWrapper.readCustomer(file);

				if (ptr==null)
				{
					break;	
				}
				ptr.display();
				
			}

			//now lets read them again and modify the name, 
			file.seek(0);
			int counter=0;
		
			while (true)
			{
				ptr=CustomerWrapper.readCustomer(file);

				if (ptr==null)
				{
					break;	
				}
				ptr.display();
				
				//ptr.name=ptr.name.concat("modified ");
				ptr.name= "modified  "+ptr.name;
				//System.out.println("now name is "+ptr.name);
		
				ptr.account+=100;
				ptr.balance+=200.1;
				//please note that we must go back to the previous position to "overwrite" the record.

				file.seek(counter*Customer.CustomerRecordLength);
				CustomerWrapper.writeCustomer(file, ptr);
				counter++;
				
			}

			//now let's check again if writting is correct!
			file.seek(0);
			while (true)
			{
				ptr=CustomerWrapper.readCustomer(file);

				if (ptr==null)
				{
					break;	
				}
				ptr.display();
				
			}

			
		}
		catch (Exception er)
		{
			System.out.println(er);
		}
	}
}

