This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
i think that's not an appropriate question. no one will give you his/her algorithm. you should ask more detailed questions than this. because no one will give you his/her algorithm.
So just say what you have problem with and we'll be glad to help you
findByCriteria;
Seems straight forward from the name;
If criteria is true
return found record(s)
else
-Do what your requirements say.
Like Roel wrote, post what you presently have...
Rajesh Moorthy
Ranch Hand
Joined: Sep 23, 2008
Posts: 30
posted
0
Thanks for your response. I am giving the details below.
Method signature:
public long[] findByCriteria(String[] criteria) {....}
Requirement:
This method should return 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".)
Problem:
This method should return an array of record numbers (not the records). However, the GUI expects the actual records itself to be displayed. Due to the signature of this method, we are forced to return only the matching record numbers from a database search. Please note that for sending the record numbers, we already have to read the records and compare it with the input criteria. After we send the record numbers, we will have to read the database (or cache) again, when the GUI wants to display the records.
How do we avoid this second read, which is redundant?
There is even more. The gui is expecting exact matches (and not "starting with" ones), so your gui has to do some filtering.
If you really want to avoid the redundant read (like you call it) the only thing you can do is, extend your given interface and add an extra method which returns you a Map of ints and String-arrays. But that's all up to you.
Searching for records is one of the points of the certification that are supposed to be ambiguous and put doubts in your head. Please take a look at this thread. If you still have doubts after reading it, please let us know and we'll be glad to help you again!
Roel De Nijs wrote:There is even more. The gui is expecting exact matches (and not "starting with" ones), so your gui has to do some filtering.
Requirement:
This method should return 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".)
Roel's quote of "expecting exact matches" doesn't quite right because the last sentence of the requirement "(For example, "Fred" matches "Fred" or "Freddy".)"
Exact match uses equals method. The requirement example looks to me startsWith method. Other way will depend on how you present your search GUI. This will affect your implementation a bit.
Roel De Nijs wrote:The gui is expecting exact matches (and not "starting with" ones), so your gui has to do some filtering.
As you can see I said that the gui was expecting "exact matches", so that you have to do in your gui some filtering because (like you said) the requirement of your database-findByCriteria method is to implement a method that returns startsWith-matches
Regards,
Roel
Fola Fadairo
Ranch Hand
Joined: Feb 16, 2004
Posts: 35
posted
0
What K means by ".... way will depend on how you present your search ..." is: Some people do not allow input text, they display the possible data and the user chooses...
Fola Fadairo wrote:What K means by ".... way will depend on how you present your search ..." is: Some people do not allow input text, they display the possible data and the user chooses...
Agreed. But still you have to do the extra filtering
Let say my gui has 2 comboboxes: one containing hotel names, one containing locations. First combo contains: Ritz, Hilton. The second combo contains: Londen, Paris, Brussels
User selects Ritz + Paris and hit search. The search of your Data-class will result in all hotels with name = Ritz or location = Paris (because they are OR'ed). Your GUI should only show the hotels with name = Ritz and location = Paris (because they should be AND'ed)
And if i am wrong on this (which i think i am not), i'm doing too much
Fola Fadairo
Ranch Hand
Joined: Feb 16, 2004
Posts: 35
posted
0
Sure Roel, you are right. Extra filtering is required.
Rajesh Moorthy
Ranch Hand
Joined: Sep 23, 2008
Posts: 30
posted
0
Could you please explain why the search of the Data-class should result in all hotels with name = Ritz or location = Paris? Why should they be OR'ed? Why shouldn't they be AND'ed in the Data class itself? Could you please indicate where is such a requirement stated that the search should be OR'ed in the Data class and should be AND'ed in the GUI?
If you pay attention to what the find method of your Data class is supposed to do, you'll verify that it must return any record where "a non-null value in criteria[n] matches any field value that begins with criteria[n]", and that the search you're supposed to provide in your GUI "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".
Rajesh Moorthy
Ranch Hand
Joined: Sep 23, 2008
Posts: 30
posted
0
The requirement says just that. It means that we should use startsWith() in the Data class and equals() in the GUI. It does not mean that they should be OR'ed in the Data class.
Rajesh Moorthy wrote:It does not mean that they should be OR'ed in the Data class.
Partner, this is kind of implicit. Since it says that "a non-null value in criteria[n] matches any field value that begins with criteria[n]", then if only one value matches this rule, it is enough for that record to be returned.
Rajesh Moorthy wrote:The requirement says just that. It means that we should use startsWith() in the Data class and equals() in the GUI. It does not mean that they should be OR'ed in the Data class.
That would be just to easy for this assignment.
in db following 3 records:
a) Ritz, Antwerp,...
b) Ritz, Paris,...
c) Hilton, Antwerp,...
user enters hotelName=Ritz and location=Antwerp (expected result in your table would be 1 record, namely record a). Your findByCriteria will (or should) return all 3 records. so you see (like roberto said) the OR'ing is implicit. you loop over your records:
- record a: it's a match because hotelName matches the specified one (no need to check location)
- record b: it's a match because hotelName matches the specified one (no need to check location)
- record c: hotelName doesn't match the specified one, so the location is checked and this is a match too
Rajesh Moorthy wrote:The requirement says just that. It means that we should use startsWith() in the Data class and equals() in the GUI. It does not mean that they should be OR'ed in the Data class.
That would be just to easy for this assignment.
in db following 3 records:
a) Ritz, Antwerp,...
b) Ritz, Paris,...
c) Hilton, Antwerp,...
user enters hotelName=Ritz and location=Antwerp (expected result in your table would be 1 record, namely record a). Your findByCriteria will (or should) return all 3 records. so you see (like roberto said) the OR'ing is implicit. you loop over your records:
- record a: it's a match because hotelName matches the specified one (no need to check location)
- record b: it's a match because hotelName matches the specified one (no need to check location)
- record c: hotelName doesn't match the specified one, so the location is checked and this is a match too
Kind regards,
Roel
Hi Roel,
I have a question : what is the String[] array given as parameter to findByCriteria() method ? Is it {Ritz, Antwerp, null,null,null,null} ?
In this case b) and c ) will not be matched in my opinion by findByCriteria method because Paris does not start with Antwerp and Hilton does not start with Ritz. Please correct me if I'm wrong ...
My String[] array will be (like you said): {Ritz, Antwerp, null, null, null, null, null} (i believe urlybird has 7 fields in db-file)
That's your understanding: you will perform an AND between the different criteria (and so record b and c will not match), my understanding is to OR them and then both records are a match.
My approach is more flexible to enhancements in the future: if they want to search in the gui on name, location, name and location, name or location i will keep my code as is (filter the records returning from find-method from database) and for the last one i just return records from find-method (without doing any filtering in the gui).
you will have to do a whole lot more.