Copyright © T. H. Merrett

COMP 612       Principles of Database Programming  	Week 1		3 of 4

/* Program using the Cars database to answer the query: WHAT IS THE COLOUR, */
/* YEAR, MAKE AND OWNER'S NAME, ADDRESS AND PHONE NUMBER OF THE CAR WITH     */
/* REGISTRATION NUMBER ...........?                                         */
import java.io.*;
public class week1j1
{ public static void main(String[] args)
  { int cmpr= 0, faptr= 0, brptr= 0, soptr= 0, kpos= 0, indx;
    byte fill;
    try
    { DataInputStream dbptrd = new DataInputStream(new BufferedInputStream(
        new FileInputStream("/course/cs612/carsdata/dbptr"),2100));
      int[] regptr= new int[1000];
      int[] colptr= new int[1000];
      int[] yrptr= new int[1000];
      int[] makptr= new int[1000];
      int[] ownptr= new int[1000];
      for(int enumerate= 0; enumerate<1000; enumerate++)	// read dbptr
      { regptr[enumerate]= dbptrd.readInt();
	colptr[enumerate]= dbptrd.readInt();
	yrptr[enumerate]= dbptrd.readInt();
	makptr[enumerate]= dbptrd.readInt();
	ownptr[enumerate]= dbptrd.readInt();
        fill= dbptrd.readByte();	// need this:
	// Note. Failing to read the byte (\n) ending each record caused the
	// for(indx..) loop below to suspend in the 2nd iteration, probably
	// at seek(regptr..): no message.
      }
      dbptrd.close();

      DataInputStream colourd = new DataInputStream(new BufferedInputStream(
	new FileInputStream("/course/cs612/carsdata/colour"),201));
      String colour[]= new String[20];
      for(int enumerate= 0; enumerate<20; enumerate++)
      { colour[enumerate]= strRead(colourd, 10, false);
      }
      colourd.close();

      DataInputStream yeard = new DataInputStream(new BufferedInputStream(
	new FileInputStream("/course/cs612/carsdata/years"),341));
      int years[]= new int[85];
      for(int enumerate= 0; enumerate<85; enumerate++)
      { years[enumerate]= yeard.readInt();
      }
      yeard.close();

      DataInputStream ownptrd = new DataInputStream(new BufferedInputStream(
        new FileInputStream("/course/cs612/carsdata/ownptr"),1770));
      int[] ownptr0= new int[885];
      for(int enumerate= 0; enumerate<885; enumerate++)		// read ownptr 0
      { ownptr0[enumerate]= ownptrd.readInt();
      }
      ownptrd.close();

      DataInputStream makptrd = new DataInputStream(new BufferedInputStream(
	new FileInputStream("/course/cs612/carsdata/makptr"),2389));
      int dsize= makptrd.readInt(), psize= makptrd.readInt();	// sizes
      int[] makptr0= new int[psize];
      for(int enumerate= 0; enumerate<psize; enumerate++)
      { makptr0[enumerate]= makptrd.readInt();
	// don't need the rest, but must read past them:
	faptr= makptrd.readInt();
	brptr= makptrd.readInt();
	soptr= makptrd.readInt();
	kpos= makptrd.readInt();
      }
      makptrd.close();

      RandomAccessFile ownerd=
	 new RandomAccessFile("/course/cs612/carsdata/owner", "r");
      RandomAccessFile makesd=
	 new RandomAccessFile("/course/cs612/carsdata/makes", "r");

      RandomAccessFile regisd=
	 new RandomAccessFile("/course/cs612/carsdata/regis", "r");
      // :using direct access to regis, owners
      String findRegis= args[0].trim();	// supplied as argument to call
      // sequential search looking up dbptr: could use binary search:
      for(indx= 0; indx<1000; indx++)
      { regisd.seek(7*regptr[indx]);
	cmpr= findRegis.compareTo(strRead(regisd, 7, true).trim());
	// compare Strings:
	if (cmpr==0) break;				// success
	else if (cmpr<0) {indx= 1000; break;}	// end unsuccessfully
      }
      if (indx==1000) System.out.println(findRegis + " not found");
      else
      { makesd.seek(41*(long)makptr0[makptr[indx]]);	// indirect ref
	ownerd.seek(81*(long)ownptr0[ownptr[indx]]);	// indirect ref
	System.out.println(findRegis + " " + colour[colptr[indx]].trim() + " "
	 + years[yrptr[indx]] + " " + strRead(makesd, 41, true).trim() + " " + 
	 strRead(ownerd, 40, false).trim() + " " +
	 strRead(ownerd, 33, false).trim() + " " +
	 strRead(ownerd, 8, true).trim());
      }
    }
    catch(IOException e)
    { System.out.print("Error: " + e);
      System.exit(1);
    }
  }
  private static String strRead(DataInput filed, int size, boolean crlf)
   throws IOException
  { byte[] record= new byte[size];
    filed.readFully(record, 0, size);
    return bytechars.byte2string(record).substring(0, size - (crlf?1:0));
    // if reading whole record, it will include crlf; field will not
  }
} // class week1j1