Results 1 to 3 of 3
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default is Polymorphism the tool to use?

    I'm implementing some DAO stuff using java(forgive me for misuse of terms/concepts this assignment is 3 hours old and fresh knowledge) and I have an find method, given a set of parameters, will find a customer within my database. There are 4 search parameters(customerNumber, name, address, city) but the user should be able to use any number/combination of these parameters to do their search(ie. Find customers named Steve who live in Houston). Should I go about this like this

    Java Code:
    public Customer findCustomer(int customerNumber){
    //Select SQL with 1 parameter
    }
    public Customer findCustomer(int customerNumber, String name){
    //Select SQL with 2 parameters
    }
    public Customer findCustomer(int customerNumber, String address){
    //select sql with 2 parameters
    }
    //....etc
    or is there something...more reasonable to attain this?

    I guess since 3 parameters are all strings I can have a
    find(int), find(int, string), find(int, string, string), find(int, string,string,string) because there really isnt a way to determine if the string entered is a name or city or address and just compare each string to the result sets for each column.(If that made sense to anyone aside from me)
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Isn't customerNumber your primary key? Adding any other fields to the search won't make any difference.

    You'd need another argument that says what the fields are.
    Java Code:
    public Customer findCustomer(String value, int field);
    
    public Customer findCustomer(Map<Integer, String> searchTerms);
    
    public Customer findCustomer(SearchTerm... searchTerms);
    Alternatively create a class that encapsulates a search query and pass instances of that. Depends on where you want your abstractions.
    Last edited by OrangeDog; 05-02-2009 at 11:08 AM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    ya customerNumber is the primary key but the user won't necessarily know that. They might want to just search for all customers in Houston and Austin or something. I'm not really sure why the search method returns just a single Customer object if the search is supposed to be set up for multiple results :/

    Side Note: A transfer object is like Object.clone()? Something that allows me to manipulate object a(or something that resembles object a) without having to mess with the object within the DB directly correct? ie this
    Java Code:
    rst = statement.executeQuery("Select * From sampleDAO Where customerNumber = "+customerNumber+"");
    return new Customer(rst.getInt(1), rst.getString(2), rst.getString(3), rst.getString(4));
    Edit***
    findCustomer does in fact return 1 customer. Someone neglected to tell me I had 2 extra search methods, selectCustomersRS() returns a RowSet, and selectCustomersTO() that returns a Collection, and these are the methods I'm supposed to be writing the multi-parameter searches for.
    Last edited by xcallmejudasx; 05-04-2009 at 06:05 PM.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. Polymorphism Help
    By AWPtic in forum New To Java
    Replies: 5
    Last Post: 04-06-2009, 05:13 PM
  2. question about polymorphism
    By becky in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 11:59 PM
  3. inheritance and polymorphism
    By tester in forum Eclipse
    Replies: 1
    Last Post: 12-21-2008, 05:58 AM
  4. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 04:14 AM
  5. what's polymorphism?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 11:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •