How to find data into the class database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • electrical
    New Member
    • Jun 2010
    • 4

    #1

    How to find data into the class database?

    /** Database implements a database of records */
    public class Database
    { private Record[] base; // the collection of records
    private int NOT_FOUND = -1; // int used to denote when a record not found
    /** Constructor Database initializes the database
    * @param initial size - the size of the database */
    public Database(int initial_size)

    {
    if ( initial_size > 0 )
    { base = new Record[initial_size]; }
    else { base = new Record[1]; }
    }
    /** findLocation is a helper method that searches base for a record
    * whose key is k. If found, the index of the record is returned,
    * else NOT FOUND is returned. */
    private int findLocation(Ke y k)
    { int result = NOT_FOUND;
    boolean found = false;
    int i = 0;
    while ( !found && i != base.length )
    {
    if ( base[i] != null && base[i].getKey().equal s(k) )
    { found = true;
    result = i;
    }
    else { i = i + 1; }
    }
    return result;
    }
    /** find locates a record in the database based on a key
    * @param key - the key of the desired record
    * @return (the address of) the desired record;
    * return null if record not found. */
    public Record find(Key k)
    { Record answer = null;
    int index = findLocation(k) ;
    if ( index != NOT_FOUND )
    { answer = base[index]; }
    return answer;
    }

    /** insert inserts a new record into the database.
    * @param r - the record
    * @return true, if record added; return false if record not added because
    * another record with the same key already exists in the database */
    public boolean insert(Record r)
    { boolean success = false;
    if ( findLocation(r. getKey()) == NOT_FOUND ) // r not already in base?
    { // find an empty element in base for insertion of r:
    boolean found_empty_pla ce = false;
    int i = 0;
    while ( !found_empty_pl ace && i != base.length )
    // so far, all of base[0]..base[i-1] are occupied
    {
    if ( base[i] == null ) // is this element empty?
    { found_empty_pla ce = true; }
    else { i = i + 1; }
    }
    if ( found_empty_pla ce )
    { base[i] = r; }
    else { // array is full! So, create a new one to hold more records:
    Record[] temp = new Record[base.length * 2];
    for ( int j = 0; j != base.length; j = j + 1 )
    { temp[j] = base[j]; } // copy base into temp
    temp[base.length] = r; // insert r in first free element
    base = temp; // change base to hold address of temp
    }
    success = true;
    }
    return success;
    }
    /** delete removes a record in the database based on a key
    * @param key - the record's key (identification )
    * @return true, if record is found and deleted; return false otherwise */
    public boolean delete(Key k)
    { boolean result = false;
    int index = findLocation(k) ;
    if ( index != NOT_FOUND )
    { base[index] = null;
    result = true;
    }
    return result;
    }
    }




    Other class
    =============== =============== =============== =========



    /** Record models a Library Book */
    public class Record
    { // the names of the fields describe their contents:
    private Key catalog_number;
    private String title;
    private String author;
    private int publication_dat e;
    /** Constructor Record constructs the book.
    * @param num - the book's catalog number
    * @param a - the book's author
    * @param t - the book's title */
    public Record(Key num, String a, String t, int date)
    { catalog_number = num;
    title = t;
    author = a;
    publication_dat e = date;
    //is_borrowed_by_ someone = false;
    }
    /** getkey returns the key that identifies the record
    * @return the key */
    public Key getKey() {
    return catalog_number; }
    /** getTitle returns the book's title
    * @return the title */
    public String getTitle() {
    return title; }
    /** getAuthor returns the book's author
    * @return the author */
    public String getAuthor() {
    return author; }
    /** getDate returns the book's publication date
    * @return the date */
    public int getDate() {
    return publication_dat e; }
    }


    Other class
    =============== =============== =============== ===========
    /** Key models a Library-of-Congress-style id number,
    * consisting of a letter code concatenated to a decimal number */
    public class Key
    { private String letter_code; // the letter code, e.g., "QA"
    private double number_code; // the number code, e.g., 76.884
    /** Constructor Key constructs a catalog number
    * @param letters - the letter code, e.g., "QA"
    * @param num - the decimal number code, e.g., 76.884 */
    public Key(String letters, double num)
    { letter_code = letters;
    number_code = num;
    }
    /** equals returns whether the catalog number held within this object
    * is identical to the catalog number held within c
    * @param c - the other catalog number
    * @return true, if this catalog number equals c; return false, otherwise */
    public boolean equals(Key c)
    { String s = c.getLetterCode ();
    double d = c.getNumberCode ();
    return ( s.equals(letter _code) && d == number_code );
    }
    /** getLetterCode returns the letter code part of this catalog number
    * @return the letter code, e.g., "QA" */
    public String getLetterCode() {
    return letter_code; }
    /** getNumberCode returns the number code part of this catalog number
    * @return the number code, e.g., "76.884" */
    public double getNumberCode() {
    return number_code; }
    }



    And the main method (test method)
    =============== =============== =============== ==========

    public class Great
    {
    public static void main(String[] args)
    { Record r = new Record(new Key("QA", 78.8),"Charles Dicens","Great Expectations", 1860);
    System.out.prin tln(r);
    Database library = new Database(5000);
    System.out.prin tln("Inserting is :" + (library.insert (r) ? " good" : "bad"));
    Key k1 = new Key("QA", 78.8);
    = library.find(k1 );
    System.out.prin tln(r);

    }
    }
Working...