home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-20 | 4.9 KB | 194 lines |
- import java.io.*;
-
- /**
- * Record format is:
- * <num records>
- * <name> <seat #>
- * repeat as necessary
- */
- public class DB {
- public static final String FILE_NAME = "Reservations.db";
- public static final int MAX_SEATS = 5;
-
- private static final int ACTIVE_REC = 0;
- private static final int DELETED_REC = 1;
-
- private boolean open;
- private RandomAccessFile file;
-
- public DB() throws IOException {
- try {
- File test = new File(FILE_NAME);
- boolean alreadyExisted = test.exists();
- file = new RandomAccessFile(FILE_NAME, "rw");
- open = true;
-
- if (!alreadyExisted) {
- FileHeader header = new FileHeader();
- writeHeader(header);
- }
-
- } catch (IOException x) {
- close();
- throw x;
- }
- }
-
- public synchronized String[] getPassengerList() throws IOException {
- String[] reservations = null;
-
- // PLACE YOUR CODE HERE.
-
- return reservations;
- }
-
- public synchronized int getSeat(String passenger)
- throws IOException, SeatNotFoundException
- {
- int seat = 0;
-
- // PLACE YOUR CODE HERE.
-
- return seat;
- }
-
- public synchronized void reservePassenger(String passenger, int seat)
- throws IOException, SeatTakenException
- {
-
- // PLACE YOUR CODE HERE.
-
- }
-
- public synchronized int[] getOpenSeats() throws IOException {
- PassengerRecord record;
-
- FileHeader header = readHeader();
- // Do the case where there are no seats:
- if (header.numPassengers == MAX_SEATS)
- return new int[0];
-
- // Do the case where all seats are still available.
- if (header.numPassengers == 0) {
- int[] open = {1, 2, 3, 4, 5};
- return open;
- }
-
- // Determine the open seats from the reserved seats.
- boolean[] reserved = new boolean[MAX_SEATS];
- String passenger;
- for (int i = 0; i < header.numPassengers; i++) {
- record = readNextRecord();
- reserved[record.getSeat() - 1] = true;
- }
-
- // Make the int array only as large as it needs to be.
- int[] open = new int[MAX_SEATS - header.numPassengers];
- int next = 0;
- for (int i = 0; i < MAX_SEATS; i++) {
- if (reserved[i] == false)
- open[next++] = i + 1;
- }
-
- return open;
- }
-
- public synchronized void deletePassenger(String name)
- throws IOException
- {
- FileHeader header = readHeader();
-
- boolean found = false;
- PassengerRecord record = null; // make the compiler happy
- for (int i = 0; i < header.numPassengers && !found; i++) {
- record = readNextRecord();
- if (name.equals(record.getName()))
- found = true;
- }
-
- // Delete at the position before we read the record to be deleted.
- if (found) {
- file.seek(record.getPtr());
- file.write(DELETED_REC);
-
- header.numPassengers--;
- writeHeader(header);
- }
- }
-
- public synchronized void close() {
- if (open) {
- try {
- file.close();
- } catch (IOException x) {
- System.out.println(x.getMessage());
- } finally {
- open = false;
- }
- }
- }
-
- public void finalize() throws Throwable {
- close();
- super.finalize();
- }
-
- private synchronized PassengerRecord readNextRecord()
- throws IOException
- {
- boolean looking = true;
- PassengerRecord record = new PassengerRecord();
-
- try {
- while (looking) {
- record.setPtr(file.getFilePointer());
- int type = file.readInt();
- record.setName(file.readUTF());
- record.setSeat(file.readInt());
- if (type == ACTIVE_REC)
- looking = false;
- }
- } catch (EOFException x) {
- System.out.println("read past the end of the file");
- }
-
- return record;
- }
-
- private synchronized void writeRecord(PassengerRecord record)
- throws IOException
- {
- FileHeader header = readHeader();
- header.totalRecords++;
- header.numPassengers++;
- writeHeader(header);
-
- file.seek(file.length());
- file.writeInt(ACTIVE_REC);
- file.writeUTF(record.getName());
- file.writeInt(record.getSeat());
- }
-
- private synchronized FileHeader readHeader() throws IOException {
- FileHeader header = new FileHeader();
- file.seek(0);
- header.totalRecords = file.readInt();
- header.numPassengers = file.readInt();
- return header;
- }
-
- private synchronized void writeHeader(FileHeader header)
- throws IOException
- {
- file.seek(0);
- file.writeInt(header.totalRecords);
- file.writeInt(header.numPassengers);
- }
-
- }
-
- class FileHeader {
- int totalRecords;
- int numPassengers;
- }
-