// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public int[] find(String[] criteria);
It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.
It must present search results in a JTable.
Your understanding of the logic of requirement is technically correct. 1 and 2 are relatively easy to implement. Implementing
3 is difficult to do in a single search. Many people (maybe most people) are not supporting 3 in a single search. Instead
they are supporting 3 as two independent searches.
3a: records where the name field exactly matches values:
(criteria[] name null null null null null), and
3b: records where the location field exactly matches values:
(criteria[] null, location, null, null, null, null)
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public int[] find(String[] criteria);
//synchronized(raf)
//loop read each record in datafile
//check recNo validity
//check whether specified record conform to criteria[]
//if conform to criteria[]
//put recNo to Collection
//end synchronized block.
//invert Collection to int[]
//return int[]
Originally posted by liqun chang:
1: i will not use any methods that in Data class in find method.Am i right?
my purpose is only return the recNos which conform to criteria[].Am i right?
Yes and Yes. The find method has no reason to call any public methods in Data, it may however, call private methods to help it do it's job (for example, method to check record validity, method to check whether specified record conform to criteria[], etc.)
2: But in this situation if i create ServicesImpl class(a business layer)and create search method in it,if i invoke find() and read() and return the
Vectors(one for column names another for records) within the search method,the values of record will be part dated.Whehter this is in reason?Please you give me some detail suggestions?
I'm not sure I completely understand this question, but let me say this. I don't think your search method needs to return column names for the record every time it's called. What I'm suggesting is a separate method, maybe called getColumnNames, that gets the column names for the record. You do this the first time you start up your GUI. You shouldn't need to get the column names more than once, since you can safely assume that the column names (which come out of the database file) will stay the same throught any one run of the application. So, your search method would simply return the values of the records (without bothering about the column names). Now we get to the part of your question that I don't think I understand. The records that are returned (because they were returned without the benefit of locking) may not be the latest versions of the records. I think it's OK to display possibly out-of-date records to the user in your JTable, if you make sure that when a user tries to book a record you do a read on that record before doing the update (both the read and update should occur in a locked context).
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
3: I will use switch sentence in find method for branches.i will only make criteria[] that only contains two Strings(name and location).So do,i can simply finish the find method with small codes.Is this in reason? please you give me some comments?
I think you can make your implementation of find more general. If your find method can handle search criteria for all the fields in a database record then it will handle the fields that are of interest to your client (name and location). That is, your client could call your find method with ("Dogs With Tools", "Hobbitton", null, null, null, null). In my opinion this is better than calling your find method with ("Dogs With Tools", "Hobbitton").
In your method that checks whether the specified record conforms to criteria[]:
A) if the criteria is null then the record field matches by definition,
B) if the criteria is non-null then the record field must match the crtieria field for the record field to match.
So, I don't think your getCriteria method is really needed.
4: Whether i can create any method in Data class what i want? Certainly i must be implement the method specificed in DB interface from instruction.Whether i must not change any method signature inherits from DB interface except remove throws exception?
Yes and Yes. You can implement any method you want in the Data class (in addition to the methods you have to implement from the DB interface). You shouldn't change the method signatures of any of the methods specified in the DB interface (because if you did, Data would no longer implement the DB inteface).
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
5: From the find method,i only use two fields of datafile(name and location) for simple reason.Am i right?
As I suggested earlier I think you are better off writing the code to handle all the fields of a record (even though you may only actually every give non-null values to name and location).
6: From client GUI,i only create two textfields,one for name filed,another for location filed.the probable case is 4
kinds: null null,name null,null location,name location.Am i right for this section?
My suggestion is that you use non-editable combo boxes instead of textfields. If you go with the combo boxes you guarantee that a user can only ever attempt exact-match searches and therefore you don't need to be concerned with the fact that your find method in Data does starts-with matching rather than exact-matching. Of course now you have to write methods that get all the possible values for the name and location field so that you can populate the drop-down lists associated with the combo boxes. The four cases you can get from two search combo boxes are:
1) null , null, null, null, null, null
2) "Name", null, null, null, null, null
3) null , "Location", null, null, null, null
4) "Name", "Location", null, null, null, null
7: Because when i find in datafile,another client may be creating the new record,so i use Collection instead of directly use int[].Am i right?
Using a collection is correct, but not because another client may be creating a new record, but because you don't know from the beginning what size to dimension the int[].
8: In the search method that i will discuss in next part,i first get the recNos from find method,and then get the Vector of records through read() method,but i am afraid if in this here,a record aleady had been modified.How could i do?
Yes, this is a possibility. You may return out-of-date records for display in the JTable. But as I've said before, I don't think this is a problem. It's only a problem if you allow a user to book a record without doing a read before the doing an update (both should occur in a locked context).
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Now we get to the part of your question that I don't think I understand. The records that are returned (because they were
returned without the benefit of locking) may not be the latest versions of the records. I think it's OK to display possibly
out-of-date records to the user in your JTable, if you make sure that when a user tries to book a record you do a read on
that record before doing the update (both the read and update should occur in a locked context).
It must be composed exclusively with components from the Java Foundation Classes (Swing components).
It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match
values specified by the user.
It must present search results in a JTable.
It must allow the user to book a selected record, updating the database file accordingly.
// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public int[] find(String[] criteria);
Originally posted by liqun chang:
1: I will create List object(or DataInfo class that contains List) which for storing the records that used to display to client by recNo.Am i right?
This is an interesting implementation that didn't occur to me. The find method is only required to return an array of matching record numbers (not the actual records themselves). Your implementation returns the array of matching records numbers, but it also stores an array of the actual records.
A less complicated implementation of find would simply return an array of matching record numbers. The client would then have to call read on each of those matching record numbers to get the actual records to display in the JTable.
In your approach the client would call find and would receive an array of matching record numbers, but the client would not really have any need for this array of matching record numbers. Instead the client would call getRecords which would return the array of records created as a side-effect of the last find call.
Assuming that the client always wants an array of records (rather than an array of record numbers) when he calls find, your implementation is probably more efficient than the typical implementation. But the cost of this increase in efficiency is that you have introduced state into the Data class. Now the Data class "remembers" the array of matching records returned by the last call of the find method. If this array of matching records is shared among all instances of the Data class, you now need to be concerned about simultaneous access to this array of matching records. You also need to be concerned about what getRecords returns before the first find method is called.
So if you pursue this implementation (which may be more efficient and therefore arguably superior to the typical solution) it does introduce some complications that have to be addressed. The typical implementation which would have the client call find and then as many reads as necessary, is more straightforward and in keeping with the spirit of the Sun-supplied interface. I'm not saying your implementation is wrong, but that it's not typical and because of this you will have to think about how the concerns identified above are addressed in your solution.
2: I use the more general find for all fields of records.Whehter my algorithm is right?(only general thinking)?
Yes
3: I will create public getRecord(recNo) in the Data that used for returning all records that conforms to criteria.Am i right?
but if i use this method,then perhaps it returns the dated values.is this
reasonable?
Would this getRecord method really take a recNo argument? It seems to me that the getRecord method would take no arguments since it would return an array of all the matching records for the last find call. So your client would call find and then getRecords (neither of these calls taking a record number argument).
I don't doubt that you could get this solution to work, but it is more complicated than the typical solution suggested by the Sun-supplied database interface. If you successfully handle the concerns I raised above then this could be a good solution (it's clearly a more efficient solution and results in less network traffic).
I don't think you need to be more worried about the stale record problem with your implemenation than you would with the more typical implementation. Stale records are not a problem when it comes to displaying records in the JTable. Stale records are a problem when it comes to updating a record, but as discussed before, this is easily solved by locking, reading, updating, and unlocking.
4: In the find method,i already put all records that conform to criteria into List(a member variable of Data)Am i right?
Given your solution, this is the right thing to do. If you went with the simpler, typical solution, it would not be necessary to have this member variable. If each of your clients has it's own instance of the Data class then it is appropriate to make this List a member variable of Data. If all your clients share the same instance of the Data class, then you need to be concerned with simultaneous access to this shared variable by different clients.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
8: From instruction1 and instruction2,i find that whether client UI require the exactly match,but find method within Data
requires startWith() of String?
also exactly match search is sub-set of find search?say the scope of find is bigger than exactly match?
The find search is not an exact match search. However, if you constrain the values the user can enter as search criteria you can effectively achieve an exact match search. For example, if you make the combo boxes non-editable and populate the drop-down lists with all the unique values for the field found in the database, then you only allow the user to ask for search criteria that are effectively exact matches. If the combo boxes were editable, then the user could enter a name of "F" which would return all the records where the contractor name starts with an "F". This would clearly not be an exact match. But if you only let the user pick from ("Fred the Contractor", "Frederick Contractors", "Freddy and Son") then you will be effectively be doing an exact match.
The only possible problem I see is if you have contractor names like ("Fred", "Fred and Sons"). Then the user could pick "Fred" from the combo-box drop-down list, but he would get back records with the name "Fred and Sons" as well as the expected records with the name "Fred". This problem could be handled by right-padding the fields with blank spaces. So the database would have fields like:
This would only return "Fred" records, no "Fred and Sons" records would be returned if this blank right-padding scheme were followed.
9: as below,whether the instrutction2 means that criteria[0] must be compared to values of name field.
for example:
RecNo name location ......
0 Ahotel
1 Bhotel
2 Chotel
the criteria[0] only be compared to Ahotel and Bhotel and Chotel?and return appropriate int[]?
If you did a find(criteria) where criteria[0] = {"Bhotel", null, null, null, null, null}, the the find method would return an int[], say recNos, where recNos[0] = 1.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
5: Whether you agree that i can display part of dated records in client GUI as long as ensuring book a latest record?
Yes.
6: From your suggestion previous,i will create two JComboBoxs,one for name another for location.because of exactly match,i
will
provide drop down list for specified item.Am i right?
Yes.
7: If i use JCombo boxes,whether i must be first read all of names of hotel names and display to drop down list?
or i must be read all of locations of hotel and display to drop down list?
Not sure I completely understand your question here.
This is one way to get the name and location values. The first time the client wants to call the find method (to get the initial values to display in the JTable), the client doesn't actually call the find method. Instead the client calls an additional method, initialFind. initialFind does everything the find method does plus it builds a list of unique field values for the name and location field. Another method, getUniqueValues, will return these lists of unique values.
So, for example, the client would call initialFind and then getUniqueValues. After doing this the client would have all the records in the database (from the initialFind call) and all the unique values for the name and location comboboxes (from the getUniqueValues call). From this point on it would not be necessary to call either initialFind or getUniqueValues again. From this point on the client would only call find since it is no longer necessary to get the unique values for name and location. Hope this makes sense.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
1: My find method algorithm is right,i will let it only return int[] that represents record numbers of conform to criteria.Am i right?(find method only do single work).
Yes.
2: In find method,i will use startWith() of String for non exactly match.Am i right?
Yes.
3: I will create search() method(public Vector search(String[] criteria))that return the Collection of records.This Collection for creating TableModel.Am i right?
Yes.
4: In search of Services(business tier),it will invoke find() then read() of Data class.Am right?
Yes.
5: But you suggest me to create initialFind() and getUniqueValues() for creating JComboBox's drop down list. I want to know where i place the two methods? and how should i use the two methods?
My suggestion is only one way of doing this. You may have other (better) ideas that may accomplish the same thing. I would place these methods in the GUI controller on the client side. The initialFind method makes calls on the find and read database operations. When you start your client GUI there will be a JTable displayed. You could use the initialFind to get the initial set of records to display in the JTable. Remember, that as a side-effect, the initialFind also accumulates the set of existing values for the name and location fields. Therefore, the getUniqueValues method can retrieve the set of existing values for the purpose of assigning the drop-down lists for the name and location combo boxes.
6: My original thinking is create two fields in client UI,but you suggest me to create two JComboBoxs instead of two fields.but how can i get the items of drop down list of the JComboBoxs?
If you create two fields, then you allow the user to type in a search criterion of "F" for the name field, which would result in returning all the records with a name starting with "F", for example "Fred and Sons", "Frederick Plumbing", "Foster Tools", etc. It seems this is hardly an exact match. On the other, if you have non-editable combo boxes the user would have to choose "Fred and Sons", or "Frederick Plumbing", or "Foster Tools" from the name combo box, and then the application would only return exact matches (since the contents of the drop-down list exactly match the contents of the database).
In order to get the contents of the drop-down lists you could call the initialFind method (which in addition to getting the records to display in the JTable also builds the sets of unique values for name and location), and then call the getUniqueValues method to retrieve the sets of unique values for name and location which can be used to assign to the drop-down lists.
7: If i know the items of drop down list of JComboBox,then i have already known the part(names and locations) of all records .Am i right? Whether this make sense?
I guess this is true. I've tried to respond to this question in 5 and 6.
8: To me, the key of the questions is:find the items(all of unique name fields and all of unique location fields) of drop down lists.How could i do in the situation of network transmission? please you help me and give me a general suggestion?
The initialFind method I spoke of previously is my attempt to do this. The initialFind method makes use of the database operations (find and read) to access all the existing values for name and location. You can accumulate all the unique values you discover for name and location (a Set works very well for this purpose). initialFind is built from find and read database calls. Find and read database calls can be accessed over the network (if you design your system to do this). The examples I've given and the suggestions made reflect the way I approached the problem. It's possible to do this many different ways.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
My suggestion is only one way of doing this. You may have other (better) ideas that may accomplish the same thing. I would
place these methods in the GUI controller on the client side. The initialFind method makes calls on the find and read
database operations. When you start your client GUI there will be a JTable displayed. You could use the initialFind to get
the initial set of records to display in the JTable. Remember, that as a side-effect, the initialFind also accumulates the
set of existing values for the name and location fields. Therefore, the getUniqueValues method can retrieve the set of
existing values for the purpose of assigning the drop-down lists for the name and location combo boxes.
Find and read database calls can be accessed over the network (if you design your system to do this). The examples I've given
and the suggestions made reflect the way I approached the problem. It's possible to do this many different ways.
//synchronized(raf)
//loop read each record in datafile
//check recNo validity
//check whether specified record conform to criteria[]
//if conform to criteria[]
//put recNo to Collection
//end synchronized block.
//invert Collection to int[]
//return int[]
--------------------------------------------------------------------------------
1: i will not use any methods that in Data class in find method.Am i right?
Originally posted by liqun chang:
1: I will create initialFind() method in UI controller of client side.and then invoke search() method(in RemoteServices remote interface) that return Vector that contains the all of records. Am i right?
Yes, it sounds like that would work.
2: The Vector that contains all of appropriate records.and it has two actions, one is for JTable display,another is for drop down list of JComboBoxs,before populate it to JComboxs i will use getUniqueValues() in UI controller to get the name fields and location fields.Am i right?
Yes.
3: I will not directly invoke the find and read in Data class,but will invoke search method in RemoteServicesImpl(remote client use) or ServicesImpl(for alone mode)this seems more reasonable.Am i right?
Yes.
4: Whether the search method in Services or RemoteServices has two actions one for JTable,another for JComboBox.Am i right?
Yes. To clarify you will have two search methods. The first search method returns an array of records that match the search criteria, and in addition it processes the array of records that match the search criteria in order to store the sets of unique values for the name and location fields. The second search methods returns an array of records that match the search criteria, and that's all it does. The first search method is called once when the JTable is first displayed to the user, and from that point on it is never called again. From that point on the second search method is called whenever the user executes a search. The first search method is basically the same as the "initialFind" method I was talking about.
5: If i invoke initialFind mothod in UI controller then it will invoke search({null,null,null,null,null,null}) get all validated records ,and the second invoke find mothod in UI controller then it will invoke serarch(String[] criteria),the criteria comes from the items of JComboBoxs.Am i right?
Yes, that's right.
you say:
quote:
Find and read database calls can be accessed over the network (if you design your system to do this). The examples I've given
and the suggestions made reflect the way I approached the problem. It's possible to do this many different ways.
6: Whether you suggest me the initialFind method in client side directly invokes the find and read methods of Data class.or create a new remote interface to encapsulate the two functions of Data.How did you made reflect the way and approached the problem?
We have slightly different architectures: I implemented a 2-tier solution that makes the database operations directly available to the client, you are implementing a 3-tier solution that hides the database operations but instead makes business methods like search and book available directly to the client. You can continue with your 3-tier solution by making search and book available to the client GUI controller. The GUI controller can implement the two search methods we were just discussing above. One of these search methods just calls your search business method. The other search method calls your search business method, but also does the work of storing the sets of unique values for the name and location fields. Your solution is fine.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Yes. To clarify you will have two search methods. The first search method returns an array of records that match the search
criteria, and in addition it processes the array of records that match the search criteria in order to store the sets of
unique values for the name and location fields. The second search methods returns an array of records that match the search
criteria, and that's all it does. The first search method is called once when the JTable is first displayed to the user, and
from that point on it is never called again. From that point on the second search method is called whenever the user executes
a search. The first search method is basically the same as the "initialFind" method I was talking about.
Originally posted by liqun chang:
Whether you suggest me create initialFind and find method in client UI controller.initialFind pass string array({null,null,null,null,null,null}) to search method(Service),and find method pass string[] array(String[]criteria) to
search method(Service).Am i right?
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Those are the largest trousers in the world! Especially when next to this ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|