data
Class Database

java.lang.Object
  extended by data.Database
All Implemented Interfaces:
DAO

public class Database
extends Object
implements DAO

The database implements all the database functionality such as adding updating, and deleting records


Field Summary
 
Fields inherited from interface
AND, OR
 
Constructor Summary
Database()
          Upon the initialization of the database, it will load the content from the file
 
Method Summary
 int addRecord(String[] data)
          Creates a new record in the database (possibly re-using a deleted record identifier).
 void deleteRecord(int recNo)
          Deletes a record, making the record identifier and database file position available for re-use.
 int[] findRecords(String[] criteria, int bool)
          Returns an array of record identifiers that map to records that match the specified search criteria.
 int[] getAllRecords()
          Query all the record number of all data stored
 String[] readRecord(int recNo)
          Returns record data from the database specified by the record identifier supplied.
 void save()
          Save the database to a file
 void updateRecord(int recNo, String[] data)
          Modifies the fields of a record.
 
Methods inherited from class
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Database

public Database()
Upon the initialization of the database, it will load the content from the file

Method Detail

addRecord

public int addRecord(String[] data)
              throws DuplicateIndexException
Creates a new record in the database (possibly re-using a deleted record identifier). Inserts the given data, and returns the record identifier of the new record. When generating the new record id, a check is made that it is unique within the database, and if not a custom DuplicateIndexException is generated and thrown. It is expected that this exception must be handled in program logic, ultimately resulting in a report to the user without exiting the application.

Specified by:
addRecord in interface DAO
Parameters:
data - the values for the new record
Returns:
recNo
Throws:
DuplicateIndexException

deleteRecord

public void deleteRecord(int recNo)
                  throws RecordNotFoundException
Deletes a record, making the record identifier and database file position available for re-use. Will throw a custom RecordNotFoundException if the supplied record identifier does not correctly map to a unique record index. It is expected that this exception will be handled in program logic, ultimately resulting in a report to the user without exiting the application.

Specified by:
deleteRecord in interface DAO
Parameters:
recNo - the record to be deleted
Throws:
RecordNotFoundException

findRecords

public int[] findRecords(String[] criteria,
                         int bool)
Returns an array of record identifiers that map to records that match the specified search criteria. The array argument should contain a set of string search patterns. The integer argument should be set to either AND or OR as defined by the integer constants defined above in this interface. So, if a logical AND is specified then the data records that match 'all' string patterns in the array should be returned. If a logical OR is specified then the data records that match on any one of the string patterns in the array should be returned.

Specified by:
findRecords in interface DAO
Parameters:
criteria - the values of the search criteria
bool - indicates an AND or OR search
Returns:
list of record numbers that matches the result

getAllRecords

public int[] getAllRecords()
Query all the record number of all data stored

Returns:
list of record numbers

readRecord

public String[] readRecord(int recNo)
                    throws RecordNotFoundException
Returns record data from the database specified by the record identifier supplied. Returns an array where each element is a record field value. If the specified record is not located in the database, then a custom RecordNotFoundException is raised. It is expected that this exception will be handled in program logic, ultimately resulting in a report to the user without exiting the application.

Specified by:
readRecord in interface DAO
Parameters:
recNo - an index to directly access the list of record
Throws:
RecordNotFoundException

save

public void save()
Save the database to a file


updateRecord

public void updateRecord(int recNo,
                         String[] data)
                  throws RecordNotFoundException
Modifies the fields of a record. The new value for field n appears in data[n]. Will throw a custom RecordNotFoundException if the supplied record identifier does not correctly map to a unique record index. It is expected that this exception will be handled in program logic, ultimately resulting in a report to the user without exiting the application.

Specified by:
updateRecord in interface DAO
Parameters:
recNo - the record to be updated
data - the new values of the record
Throws:
RecordNotFoundException