473,418 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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(Key k)
{ int result = NOT_FOUND;
boolean found = false;
int i = 0;
while ( !found && i != base.length )
{
if ( base[i] != null && base[i].getKey().equals(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_place = false;
int i = 0;
while ( !found_empty_place && i != base.length )
// so far, all of base[0]..base[i-1] are occupied
{
if ( base[i] == null ) // is this element empty?
{ found_empty_place = true; }
else { i = i + 1; }
}
if ( found_empty_place )
{ 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_date;
/** 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_date = 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_date; }
}


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.println(r);
Database library = new Database(5000);
System.out.println("Inserting is :" + (library.insert(r) ? " good" : "bad"));
Key k1 = new Key("QA", 78.8);
= library.find(k1);
System.out.println(r);

}
}
Jun 19 '10 #1
0 1325

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: rozrabiak | last post by:
Hi:) I need to dump data from database in to *.sql file. I try it using DUMP DATABASE but it not work that I need it. So, how can I dump data from database in to *.sql file and then run it...
0
by: Tommy Christian | last post by:
Hi! Anyone who knows about saving serialized data to database, coz I have a problem with that. If I just serialize my session data and then deserialize it, it works. But when I save it...
4
by: Steve Kallal | last post by:
I seem to remember that VB 6 had a way to bind a data class redirectly to an ADO recordset. Simply put, a the class could be populated with the recordset data without a lot of coding simply by using...
2
by: Booster | last post by:
Hi I am a beginner working with VB.net Ho to: find data in another column using the index number "ParamID", also the possibility to updata the data in "ParamVal". This is a Parameter table ...
5
by: Tony | last post by:
Ok. im new to programming. and sorry for fuzzy discription :) I am using a program called leechget. this is download manager that i use on the net. When i add a dowload , i can see the information...
0
by: =?Utf-8?B?U3JpZGhhcg==?= | last post by:
Hi, Usually in Visual studio 2003 when we include a crystal report it creates a class file for that report. And we will use that class to dynamically load the data. But I am not able to find...
1
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: I made web site using asp.net 2.0 Vb.Net. The project depends on database in the local machine. The web site has • Create User Wizard and • Login form When the user is...
5
by: Brad Pears | last post by:
Hi guys!!! Thanks for all your input on previous OO posts. I know it will be all the same people responding again and I really appreciate your insight etc.. as you all appear to know what you are...
1
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: I have probelm that I hung with for three weeks, My problem is I want to read Binary data from database. I will told you what I did : first I declear an array byte and I cast the...
1
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: I have probelm that I hung with for three weeks, My problem is I want to read Binary data from database. I will told you what I did : first I declear an array byte and I cast the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.