Java Reference
In-Depth Information
the size of an individual record. Consequently, the number of records in fi le ranAccts
at any given time = ranAccts.length()/48 .
Now for the code…
import java.io.*;
import java.util.*;
public class RanFile1
{
private static fi nal int REC_SIZE = 48;
private static fi nal int SURNAME_SIZE = 15;
private static fi nal int NUM_INITS = 3;
private static long acctNum = 0;
private static String surname, initials;
private static fl oat balance;
public static void main(String[] args)
throws IOException
{
RandomAccessFile ranAccts =
new RandomAccessFile("accounts.dat", "rw");
Scanner input = new Scanner(System.in);
String reply = "y";
do
{
acctNum++;
System.out.println(
"\nAccount number " + acctNum + ".\n");
System.out.print("Surname: ");
surname = input.nextLine();
System.out.print("Initial(s): ");
initials = input.nextLine();
System.out.print("Balance: ");
balance = input.nextFloat();
//Now get rid of carriage return(!)…
input.nextLine();
writeRecord(ranAccts); //Method defi ned below.
System.out.print(
"\nDo you wish to do this again (y/n)? ");
reply = input.nextLine();
}while (reply.equals("y")||reply.equals("Y"));
System.out.println();
showRecords(ranAccts); //Method defi ned below.
Search WWH ::




Custom Search